-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeaker-emulator.ino
221 lines (177 loc) · 5.56 KB
/
speaker-emulator.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
@file speaker-emulator.ino
This program periodically sends commads to the battery pack gas gauging device BQ28Z610:
- 12.1.4 0x06/07 Temperature()
- 12.1.5 0x08/09 Voltage()
- 12.1.23 0x2C/2D RelativeStateOfCharge()
@see Texas Instruments Technical Reference Manual https://www.ti.com/lit/ug/sluua65e/sluua65e.pdf
@see BQ28Z610 product: https://www.ti.com/product/BQ28Z610
Commands Voltage() and RelativeStateOfCharge() are sent in pair once per 5 seconds.
Command Temperature() is sent ones per 10s.
There is could be any time shift between them.
Connection
Arduino Nano:
- A4 = SDA -> white
- A5 = SCL -> yellow
AtTiny85:
- 5 = SDA -> white
- 7 = SCL -> yellow
<pre>
-|1* 8|- VCC
-|2 7|- C (SCL, yellow)
-|3 6|-
GND -|4 5|- D (SDA, white)
</pre>
MIT License
Copyright (c) 2024 Oleksii Sylichenko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Wire.h>
/**
@brief Standard BQ28Z610 Device I2C Address
@see 12.1 Standard Data Commands
*/
#define DEVICE_ADDR 0x55
/**
@brief 12.1 Standard Data Commands
- 12.1.4 0x06/07 Temperature()
- 12.1.5 0x08/09 Voltage()
- 12.1.23 0x2C/2D RelativeStateOfCharge()
@see https://www.ti.com/lit/ug/sluua65e/sluua65e.pdf
*/
class Commands {
public:
static const byte TEMP = 0x06; // 12.1.4 0x06/07 Temperature()
static const byte VOLT = 0x08; // 12.1.5 0x08/09 Voltage()
static const byte RSOC = 0x2C; // 12.1.23 0x2C/2D RelativeStateOfCharge()
};
/**
Delay between sending pair of commands Voltage() and RelativeStateOfCharge().
Standard value is 5 seconds.
*/
const int VOLT_RSOC_DELAY = 5000;
/**
Delay between sending command Temperature().
Standard value is 10 seconds.
*/
const int TEMP_DELAY = 10000;
/**
Offset between sending pair of cammands Voltage() and RelativeStateOfCharge() and sending command Temperature().
This value may change.
*/
const int TIME_OFFSET_VOLT_RSOC_TEMP = 1000;
void printByte(byte val) {
if (val < 0xF) Serial.print('0');
Serial.print(val, HEX);
}
void printData(byte data[]) {
Serial.print("[ ");
printByte(data[0]);
Serial.print(' ');
printByte(data[1]);
Serial.print(" ]");
}
/**
@brief Compose word value from two bytes in Little-endian.
[0x34, 0x12] -> 0x1234
*/
word composeWord(byte data[]) {
const byte LSB = data[0];
const byte MSB = data[1];
return (MSB << 8) | LSB;
}
void setup() {
Wire.begin(); // join i2c bus as a master (address for master is optional)
Serial.begin(9600); // start serial for output
while (!Serial) {};
Serial.println();
Serial.println("============ START: [JBL XTREAME 2 Emulator] ============");
Serial.println("License: MIT License");
Serial.println("Copyright (c) 2024 Oleksii Sylichenko");
Serial.println("=========================================================");
}
void sendCommand(byte command) {
Serial.print("sendCommand: 0x");
printByte(command);
Serial.print("; ");
Wire.beginTransmission(DEVICE_ADDR);
Wire.write(command);
Wire.endTransmission();
}
int requestData() {
Serial.print("respondData: ");
byte data[2];
Wire.requestFrom(DEVICE_ADDR, sizeof(data));
for (int i = 0; Wire.available() && i < sizeof(data); i++) { // slave may send less than requested
data[i] = Wire.read();
}
printData(data);
Serial.print(" -> ");
const word retval = composeWord(data);
Serial.print(retval);
return retval;
}
/**
12.1.5 0x08/09 Voltage()
*/
void requestVoltage() {
sendCommand(Commands::VOLT);
float volt = requestData();
volt = volt / 1000;
Serial.print(" = ");
Serial.print(volt, 3); // 0.001V
Serial.print(" V");
Serial.println();
}
/**
12.1.23 0x2C/2D RelativeStateOfCharge()
*/
void requestRsoc() {
sendCommand(Commands::RSOC);
int soc = requestData();
Serial.println(" = " + String(soc) + " %");
}
/**
12.1.4 0x06/07 Temperature()
*/
void requestTemperature() {
static const float ABS_ZERO = -273.15;
sendCommand(Commands::TEMP);
float temp = requestData();
temp = temp / 10 + ABS_ZERO;
Serial.print(" = ");
Serial.print(temp, 1); // 0.1°
Serial.print("°C");
Serial.println();
}
void loop() {
static long voltRsocTime = millis(),
tempTime = millis();
const long voltRsocDelay = (millis() + TIME_OFFSET_VOLT_RSOC_TEMP) - voltRsocTime;
if (voltRsocDelay < 0 || VOLT_RSOC_DELAY <= voltRsocDelay) {
voltRsocTime = millis();
requestVoltage();
requestRsoc();
Serial.println();
}
const long tempDelay = millis() - tempTime;
if (tempDelay < 0 || TEMP_DELAY <= tempDelay) {
tempTime = millis();
requestTemperature();
Serial.println();
}
}