-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
412 lines (376 loc) · 9.97 KB
/
main.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <Arduino.h>
#include <avr/eeprom.h>
#include "MyPin.h"
#include "MyTWI.h"
#include "INA219.h"
#include "SSD1306.h"
// #define DEBUG
#ifdef DEBUG
/*Serial.print(...)*/
#define DBG(...) Serial.print(__VA_ARGS__)
/*Serial.println(...)*/
#define DBG_L(...) Serial.println(__VA_ARGS__)
#else
#define DBG(...)
#define DBG_L(...)
#endif
float curPerBit;
#define ROW0_Y 0
#define ROW1_Y 8
#define ROW2_Y 16
#define ROW3_Y 24
#define MAX_CHAR_COLUMN 128 / 6
#define BIT_VAL(v, b) ((uint8_t)((v)&_BV(b))) >> (b)
#define PIN_b0 2
#define PIN_b1 3
#define SCAN_BTNS() (((~PIND) >> 2) & 0b11)
#define B0 0
#define B1 1
#define MAX_INDEX 2
#define SLEEP_TIME_BR_DOWN 3 * 60 * 1000UL //自动降低亮度时间
#define SLEEP_TIME_SCREEN_SAVE 10 * 60 * 1000UL //自动屏保时间
#define SLEEP_TIME_SCREEN_SAVE_MAX 5 * 60 * 1000UL //自动屏保时间
unsigned long t0;
unsigned long st;
unsigned long lastPressTime;
uint8_t isSleeping;
float ph = 0, ih = 0;
uint8_t mode;
uint8_t br;
uint8_t screenDir;
uint8_t preBtns;
int8_t index;
MySSD1306 lcd;
void reset()
{
ph = 0;
ih = 0;
t0 = millis();
st = t0;
lastPressTime = t0;
isSleeping = 0;
index = 0;
preBtns = SCAN_BTNS();
mode = 0;
}
void setup()
{
// Serial.begin(250000);
initMyTWI(400000);
// TWIScanDevices();
INA219_Init();
lcd.init();
curPerBit = INA219_MyDefaultConfig();
MYPIN_READ_MODE_PULLUP(PIN_b0);
MYPIN_READ_MODE_PULLUP(PIN_b1);
// MCUCR &= ~(_BV(ISC11) | _BV(ISC10) | _BV(ISC01) | _BV(ISC00));
// MCUCR |= _BV(ISC10) | _BV(ISC00);
// GICR |= _BV(INT0) | _BV(INT1);
// struct INA219_Config config;
// config = INA219_ReadConfig();
// uint16_t v = *(uint16_t *)&config;
// uint8_t mode = (v >> 0) & 0b111;
// uint8_t sadc = (v >> 3) & 0b1111;
// uint8_t badc = (v >> 7) & 0b1111;
// uint8_t pg = (v >> 11) & 0b11;
// uint8_t brng = (v >> 13) & 0b1;
// DBG("INA219 mode: ");
// DBG_L(mode, BIN);
// DBG("INA219 sadc: ");
// DBG_L(sadc, BIN);
// DBG("INA219 badc: ");
// DBG_L(badc, BIN);
// DBG("INA219 pg: ");
// DBG_L(pg, BIN);
// DBG("INA219 brng: ");
// DBG_L(brng, BIN);
// uint16_t cal = INA219_ReadCalibration();
// DBG("INA219 calibration: ");
// DBG_L(cal);
screenDir = eeprom_read_byte(0);
if (screenDir != 255)
lcd.setScreenDir(screenDir);
else
screenDir = lcd.getScreenDir();
br = eeprom_read_byte((uint8_t *)1);
if (br != 255)
lcd.setContrast(br);
else
br = lcd.getContrast();
reset();
}
float convertUnit(float v, float precision, char *unit)
{
uint8_t isNeg = 0;
if (v < 0.0)
{
v = -v;
isNeg = 1;
}
if (v < precision)
{
*unit = ' ';
v = 0.0;
}
else if (v < 0.001)
{
*unit = 'u';
v *= 1E6;
}
else if (v < 1)
{
*unit = 'm';
v *= 1E3;
}
else
{
*unit = ' ';
}
return isNeg ? -v : v;
}
uint8_t getFloatCharLen(float v)
{
uint8_t n = 0;
if (v < 0.0)
{
n = 1;
v = -v;
}
if (v >= 1000.0)
return n + 7;
if (v >= 100.0)
return n + 6;
if (v >= 10.0)
return n + 5;
return n + 4;
}
uint8_t animoffset;
void loop()
{
animoffset++;
animoffset %= 11;
unsigned long t1 = millis();
uint32_t dt = t1 - t0;
// TWIScanDevices();
float busValue, shuntValue, powValue, currentValue;
if (INA219_ReadValueF(curPerBit, &busValue, &shuntValue, &powValue, ¤tValue))
{
float dth = ((float)dt / (1000.0 * 3600.0));
ih += currentValue * dth; //Ah
ph += powValue * dth; //Wh
t0 = t1;
lcd.clear(0);
if (!isSleeping || (isSleeping && (t1 - lastPressTime) % (SLEEP_TIME_SCREEN_SAVE_MAX + SLEEP_TIME_SCREEN_SAVE) < (SLEEP_TIME_SCREEN_SAVE)))
{
lcd.setCursor(0, 0);
char unit;
busValue = convertUnit(busValue, 0.001, &unit);
lcd.print(" V:");
lcd.setCursorX(max(10 - getFloatCharLen(busValue), 3));
lcd.print(busValue);
lcd.println(unit);
currentValue = convertUnit(currentValue, 0.000001, &unit);
lcd.print(" A:");
lcd.setCursorX(max(10 - getFloatCharLen(currentValue), 3));
lcd.print(currentValue);
lcd.println(unit);
powValue = convertUnit(ph, 0.000001, &unit);
lcd.print("Wh:");
lcd.setCursorX(max(10 - getFloatCharLen(powValue), 3));
lcd.print(powValue);
lcd.println(unit);
powValue = convertUnit(ih, 0.000001, &unit);
lcd.print("Ah:");
lcd.setCursorX(max(10 - getFloatCharLen(powValue), 3));
lcd.print(powValue);
lcd.println(unit);
char ss[] = "00:00:00";
unsigned long ttt = (t1 - st) / 1000;
uint8_t tttt = (ttt / 3600); // % 24;
ss[0] = '0' + tttt / 10;
ss[1] = '0' + tttt % 10;
tttt = (ttt / 60) % 60;
ss[3] = '0' + tttt / 10;
ss[4] = '0' + tttt % 10;
tttt = ttt % 60;
ss[6] = '0' + tttt / 10;
ss[7] = '0' + tttt % 10;
lcd.setCursor(13, 0);
lcd.print(ss);
if (mode == 0)
{
lcd.setCursor(13, index + 1);
lcd.print((char)animoffset);
}
uint8_t xx[] = {13, 13, 13};
xx[index] = 14;
lcd.setCursor(xx[0], 1);
//重置
lcd.print("Reset");
lcd.setCursor(xx[1], 2);
//设置屏幕旋转
lcd.print("Rotate");
lcd.setCursor(xx[2], 3);
//设置亮度
lcd.print("\x0f:");
if (mode == 1)
{
if (br < 254)
// lcd.print((char)24);
lcd.print((char)(animoffset + 0x14));
else
lcd.print('_');
}
lcd.print(br);
if (mode == 1)
{
if (br > 1)
lcd.print((char)(animoffset + 128));
else
lcd.print('_');
}
}
else
{
//屏保模式
static uint8_t offset = 0;
offset++;
offset %= SSD1306_LCD_WIDTH;
static uint8_t offset2 = 0;
offset2 += 2;
offset2 %= SSD1306_LCD_WIDTH;
uint8_t ind = 0;
for (uint8_t i = 0; i < SSD1306_LCD_WIDTH; i++)
{
uint8_t y = lcd.getFontByte((128 + 11) * 5 + ind);
if (i > 64)
{
y = y < 30 ? 30 - y : 0;
}
lcd.drawPixel((i + offset) % SSD1306_LCD_WIDTH, y, WHITE);
lcd.drawPixel(SSD1306_LCD_WIDTH - 1 - (i + offset2) % SSD1306_LCD_WIDTH, y, WHITE);
if ((i / 32) & 0b01)
ind--;
else
ind++;
}
}
// lcd.setCursor(11, 3);
// lcd.print(dt);
lcd.display();
}
else
{
// lcd.println("Measure overflow!");
}
uint8_t btns = SCAN_BTNS();
if (btns != preBtns)
{
lastPressTime = t1;
if (btns == 0b11)
{
switch (index)
{
case 0:
reset();
break;
case 1:
if (screenDir)
{
screenDir = 0;
lcd.setScreenDir(0);
eeprom_write_byte(0, 0);
}
else
{
screenDir = 2;
lcd.setScreenDir(2);
eeprom_write_byte(0, 2);
}
break;
case 2:
mode = !mode;
if (mode == 0)
eeprom_write_byte((uint8_t *)1, br);
break;
default:
break;
}
}
else if (mode == 0 && preBtns == 0b01 && (!(btns & 0b01)))
{
if (screenDir == 0)
{
index++;
if (index > MAX_INDEX)
index = MAX_INDEX;
}
else
{
index--;
if (index < 0)
index = 0;
}
}
else if (mode == 0 && preBtns == 0b10 && (!(btns & 0b10)))
{
if (screenDir == 2)
{
index++;
if (index > MAX_INDEX)
index = MAX_INDEX;
}
else
{
index--;
if (index < 0)
index = 0;
}
}
preBtns = btns;
}
else if (mode == 1)
{
if ((btns == 0b01 && screenDir == 0) || (btns == 0b10 && screenDir == 2))
{
if (br > 0)
br--;
lcd.setContrast(br);
}
else if ((btns == 0b10 && screenDir == 0) || (btns == 0b01 && screenDir == 2))
{
if (br < 254)
br++;
lcd.setContrast(br);
}
}
//n分钟无按键按下,进入休眠
static uint8_t saveBr;
if ((t1 - lastPressTime) > (SLEEP_TIME_BR_DOWN) && btns == 0b00)
{
//刚进入休眠
if (!isSleeping)
{
isSleeping = 1;
saveBr = br;
//如果处于编辑亮度模式,保存亮度,退出编辑模式
if (mode == 1)
{
eeprom_write_byte((uint8_t *)1, br);
mode = 0;
}
}
//休眠亮度降低到0
if (br > 0)
br--;
lcd.setContrast(br);
}
else if (isSleeping)
{
//退出休眠
br = saveBr;
lcd.setContrast(br);
isSleeping = 0;
}
// delay(10);
}