TeensyNanoExoCode
Loading...
Searching...
No Matches
I2CHandler.h
Go to the documentation of this file.
1/*
2 This class is using the singleton pattern. To use you must first get a referance to the singleton instance.
3 Do this with the line 'I2C* instance = I2C::get_instance();'. Then you can read with 'instance->read_i2c(...)'
4
5 Chance Cuddeback 2022
6*/
7
8#ifndef I2CHANDLER_H
9#define I2CHANDLER_H
10
11#include <Arduino.h>
12
13#if defined(ARDUINO_ARDUINO_NANO33BLE)
14
15#include <Wire.h>
16
17class I2C
18{
19 public:
20 static I2C* get_instance()
21 {
22 static I2C* instance = new I2C();
23 return instance;
24 }
25
26 void read_i2c(uint8_t* ret, uint8_t addr, uint8_t reg, uint8_t len)
27 {
28 // logger::print("Reading from I2C device: ");
29 // logger::print(addr);
30 // logger::print(" at register: ");
31 // logger::print(reg);
32 // logger::print(" with length: ");
33 // logger::println(len);
34
35 Wire.beginTransmission(addr);
36 Wire.write(reg);
37 Wire.endTransmission();
38 Wire.requestFrom(addr, 2, false);
39 for (uint8_t i=0; i<len; i++)
40 {
41 ret[i] = Wire.read();
42 }
43 Wire.endTransmission();
44 }
45
46 void write_i2c(uint8_t addr, uint8_t reg, uint8_t val)
47 {
48 Wire.beginTransmission(addr);
49 Wire.write(reg);
50 Wire.write(val);
51 Wire.endTransmission();
52 }
53
54 private:
55 I2C()
56 {
57 Wire.begin();
58 }
59};
60
61#endif
62
63
64namespace i2c_cmds
65{
66 namespace smart
67 {
68 namespace get_battery_voltage
69 {
70 const uint8_t addr = 0x40;
71 const uint8_t reg = 0x02;
72 const uint8_t len = 2;
73 }
74 namespace get_battery_soc
75 {
76 const uint8_t addr = 0x40;
77 const uint8_t reg = 0x0e;
78 const uint8_t len = 2;
79 }
80 }
81
82 namespace rc
83 {
84 namespace calibrate
85 {
86 const uint8_t addr = 0x40;
87 const uint8_t reg = 0x05;
88 const uint16_t val = 0x5000;
89 }
90 namespace get_battery_voltage
91 {
92 const uint8_t addr = 0x40;
93 const uint8_t reg = 0x02;
94 const uint8_t len = 2;
95 }
96 }
97
98 namespace thigh_imu
99 {
100 const uint8_t left_addr = 0x01;
101 const uint8_t right_addr = 0x02;
102
103 namespace handshake
104 {
105 const uint8_t reg = 0x01;
106 const uint8_t len = 1;
107 }
108 namespace get_angle
109 {
110 const uint8_t reg = 0x02;
111 const uint8_t len = 1;
112 }
113 }
114
115 namespace ankle_angles
116 {
117 const uint8_t addr = 0x04; // Confirm that peripheral MCU has the same address
118 namespace handshake
119 {
120 const uint8_t reg = 0x01;
121 const uint8_t len = 1;
122 }
123 namespace get_left_angle
124 {
125 const uint8_t reg = 0x02;
126 const uint8_t len = 2;
127 }
128 namespace get_right_angle
129 {
130 const uint8_t reg = 0x03;
131 const uint8_t len = 2;
132 }
133 }
134}
135
136// int INA219_ADR = 0x40; // Address of INA219 for writing defined in 7 bits. The 8th bit is automatically included by Wire.read() or Wire.write()
137// int INA219_CONFIG = 0x00; // All-register reset, bus voltage range, PGA gain, ADC resolution/averaging. Typically does not need modification
138// int INA219_SHUNT = 0x01; // Shunt voltage measurement - use this to get the shunt resistor voltage
139// int INA219_BUS = 0x02; // Bus voltage measurement - use this to get the battery voltage relative to ground
140// int INA219_PWR = 0x03; // Power measurement - use this to get calibrated power measurements
141// int INA219_CUR = 0x04; // Current measurement - use this to get the current flowing through the shunt
142// int INA219_CAL = 0x05; // Set full scale range and LSB of current/power measurements. Needed for power and current measurements
143// int CurrentLSB = 1; // mA/bit. This value is used to multiply the current reading from the INA219 to obtain actual current in mA
144// int PowerLSB = 20 * CurrentLSB; // mW/bit. This value is used to multiply to power reading from the INA219 to obtain actual power in mW
145// int ShuntLSB = 0.01; // mV. This is the default multiplier for the shunt voltage reading from the INA219.
146// int BusLSB = 4; // mV. This is the multiplier for the bus (battery) voltage reading from the INA219.
147// int Cal = 0x5000; // Calibration value in hex. Cal = 0.04096/(CurrentLSB*ShuntResistance). Shunt resistance on Rev3/4 is 2mOhm.
148
149#endif
const uint8_t len
Definition I2CHandler.h:126
const uint8_t reg
Definition I2CHandler.h:125
const uint8_t reg
Definition I2CHandler.h:130
const uint8_t len
Definition I2CHandler.h:131
const uint8_t len
Definition I2CHandler.h:121
const uint8_t reg
Definition I2CHandler.h:120
const uint8_t addr
Definition I2CHandler.h:117
const uint8_t reg
Definition I2CHandler.h:87
const uint16_t val
Definition I2CHandler.h:88
const uint8_t addr
Definition I2CHandler.h:86
const uint8_t len
Definition I2CHandler.h:94
const uint8_t addr
Definition I2CHandler.h:92
const uint8_t reg
Definition I2CHandler.h:93
const uint8_t addr
Definition I2CHandler.h:76
const uint8_t len
Definition I2CHandler.h:78
const uint8_t reg
Definition I2CHandler.h:77
const uint8_t len
Definition I2CHandler.h:72
const uint8_t reg
Definition I2CHandler.h:71
const uint8_t addr
Definition I2CHandler.h:70
const uint8_t reg
Definition I2CHandler.h:110
const uint8_t len
Definition I2CHandler.h:111
const uint8_t reg
Definition I2CHandler.h:105
const uint8_t len
Definition I2CHandler.h:106
const uint8_t left_addr
Definition I2CHandler.h:100
const uint8_t right_addr
Definition I2CHandler.h:101
Definition I2CHandler.h:65