Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
i_ic.h
Go to the documentation of this file.
1
5#ifndef MOCK_I_IC_HPP
6#define MOCK_I_IC_HPP
7
13#include <cstdint>
14
15#include <memory>
16
17namespace simulator {
18
20{
21public:
22 // run an update of the object properties
23 virtual void run_electrical_update() = 0;
24
25 // the i2c adress of the device
26 virtual uint8_t get_i2c_address() const = 0;
27
28 // write data to this device
29 virtual int i2c_write_data(const uint8_t registerAddress, const uint8_t dataSize, const uint8_t* dataBuffer)
30 {
31 if (_registerMap[registerAddress] != nullptr)
32 {
33 uint16_t data = dataBuffer[0];
34 if (dataSize > 1)
35 data |= (dataBuffer[1] << 8);
36 return _registerMap[registerAddress]->write(data);
37 }
38
39 // failure
40 return 1;
41 }
42
43 // read data from a register
44 virtual int i2c_read_data(const uint8_t registerAddress, const uint8_t dataSize, uint8_t* dataBuffer)
45 {
46 if (_registerMap[registerAddress] != nullptr)
47 {
48 const uint16_t d = _registerMap[registerAddress]->read();
49
50 dataBuffer[0] = d & 0xff;
51 if (dataSize > 1)
52 dataBuffer[1] = (d >> 8) & 0xff;
53 return 0;
54 }
55
56 // failure
57 return 1;
58 }
59
60 // simultaneous read write
61 virtual int i2c_xfer_data(const int outSize, const uint8_t* out, const int inSize, uint8_t* in)
62 {
63 // fail
64 return 1;
65 }
66
67 // register entry point
68 struct Register
69 {
70 uint16_t _data = 0;
71
72 virtual uint16_t read() { return _data; }
73 virtual int write(uint16_t data)
74 {
75 _data = data;
76 return 0;
77 }
78 };
79
80protected:
81 std::unique_ptr<Register> _registerMap[255];
82
83private:
84};
85
86} // namespace simulator
87
88#endif
Simulator dedicated namespace.
Definition: default_simulation.h:8