-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
1908 lines (1626 loc) · 50.6 KB
/
main.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include "stm8.h"
#include "settings.h"
#include "system.h"
#include "systemtimer.h"
#include "flash.h"
#include "uart.h"
#include "displays.h"
#include "button.h"
#include "encoder.h"
#include "encoderbutton.h"
#include "beep.h"
#include "fan.h"
#include "load.h"
#include "adc.h"
// --------------------------------------------------------------------------------------------------------------------
#define VERSION 0x00010000
#define LED_V 0x01
#define LED_AH 0x02
#define LED_WH 0x04
#define LED_A 0x08
#define LED_RUN 0x10
#define LED_1 0x20
#define LED_0 0x40
#define ADC_CH_TEMP 0
#define ADC_CH_MAIN 1
#define ADC_CH_SENSE 2
#define ADC_CH_SUP 3
#define ADC_N 250
#define ADC_N_FAST 16
enum Mode {
Mode_Booting,
Mode_MenuFun,
Mode_MenuBeep,
Mode_MenuCalV,
Mode_MenuCalI,
Mode_CalV1,
Mode_CalV2,
Mode_CalI1r,
Mode_CalI1v,
Mode_CalI2r,
Mode_CalI2v,
Mode_Fun1,
Mode_Fun1Run,
Mode_Fun2,
Mode_Fun2Pre,
Mode_Fun2Run,
Mode_Fun2Warn,
Mode_Fun2Res,
};
static enum Mode mode;
enum AdcMode {
AdcMode_None,
AdcMode_Normal,
AdcMode_Sup,
AdcMode_Cal1First,
AdcMode_Cal1Sec,
AdcMode_Cal2First,
AdcMode_Cal2Sec,
};
static volatile enum AdcMode adcMode;
#define ERROR_POLARITY (1 << 0)
#define ERROR_SUPPLY (1 << 1)
#define ERROR_OUP (1 << 2)
#define ERROR_OTP (1 << 3)
#define ERROR_ERT (1 << 4)
#define ERROR_CAL (1 << 5)
static uint8_t error;
static volatile uint32_t uMainRaw;
static volatile uint32_t uSenseRaw;
static volatile uint16_t uSupRaw;
static volatile uint16_t tempRaw;
static volatile uint8_t tickCount;
static uint16_t uSet; // mV
static uint16_t iSet; // mA
static uint16_t iSetDisp; // in units: 100uA or 1mA
static uint32_t powLimit; // uW
static uint16_t uMain; // mV
static uint16_t uSense; // mV
static uint32_t cycleBeginMs;
static bool uiSetModified;
const struct ValueCoef* uCoef;
static volatile bool conn4;
static volatile uint8_t sCount;
static uint8_t sCountCopy;
static volatile uint32_t sSum; // raw
static uint32_t sSumCopy; // raw
static uint16_t cal1Disp;
static uint32_t cal1First;
static uint32_t cal1Sec;
static uint16_t cal2Disp;
static uint32_t cal2First;
static uint32_t cal2Sec;
struct MenuState {
uint8_t fun;
bool beep;
};
static struct MenuState menuState;
struct Fun1State {
bool warning;
bool flip;
uint32_t lastBeep;
};
static struct Fun1State fun1State;
enum DisplayedValue {
DisplayedValue_V,
DisplayedValue_Ah,
DisplayedValue_Wh
};
struct Fun2State {
enum DisplayedValue disp;
uint32_t lastDisp;
uint16_t u;
uint32_t ah; // 1 unit = 1 mA*h
uint64_t sAh; // 1 unit = 2 mA*ms
uint32_t wh; // 1 unit = 1 mW*h
uint64_t sWh; // 1 unit = 2 uW*ms, raw
bool flip;
uint32_t lastBeep;
};
static struct Fun2State fun2State;
enum EncoderMode {
EncoderMode_I1,
EncoderMode_I0,
EncoderMode_U1,
EncoderMode_U0
};
static enum EncoderMode encoderMode;
enum FanState {
FanState_Override,
FanState_Off,
FanState_Low,
FanState_Mid,
FanState_Full
};
static enum FanState fanState;
struct ValueCoef {
uint16_t offset;
uint16_t mul;
uint16_t div;
};
struct Config {
struct ValueCoef iSetCoef;
struct ValueCoef uMainCoef;
struct ValueCoef uSenseCoef;
uint16_t uSupMin; // raw
uint16_t tempThreshold;
uint16_t tempFanLow;
uint16_t tempFanMid;
uint16_t tempFanFull;
uint16_t tempLimit;
uint16_t tempDefect;
uint16_t iSetMin; // mA
uint16_t iSetMax; // mA
uint16_t uSetMin; // mV
uint16_t uSetMax; // mV
uint16_t uSenseMin; // mV
uint16_t uNegative; // raw
uint16_t uMainLimit; // mV
uint32_t powLimit; // mW
uint32_t ahMax; // mAh
uint32_t whMax; // mWh
uint8_t fun;
uint8_t beepOn;
uint16_t uSet; // mV
uint16_t iSet; // mA
uint8_t curUnit; // 0=mA, 1=100uA
};
static_assert(sizeof(struct Config) <= 128, "Config is bigger than EEPROM");
#define CFG ((struct Config*)0x4000) // begin of the EEPROM
#define INPUT_DISABLE_BUTTON 0x01
#define INPUT_DISABLE_ENCODER 0x02
#define INPUT_DISABLE_ENCODER_BUTTON 0x04
static bool displayOverride;
static uint8_t display[8];
static uint8_t inputDisable;
static uint16_t flowInterval = 1000; // ms
static uint32_t lastFlow;
static uint8_t commReply[18];
enum Command {
Command_Reboot = 0x01,
Command_GetVersion,
Command_ReadConfig,
Command_WriteConfig,
Command_Display,
Command_Beep,
Command_Fan,
Command_InputDisable,
Command_ReadSettings,
Command_WriteSettings,
Command_SetMode,
Command_GetState,
Command_ResetState,
Command_FlowState,
Command_ReadRaw,
Command_WriteRaw,
Command_Bootloader,
};
enum CommandState {
CommandState_Request = 0x00,
CommandState_Response = 0x40,
CommandState_Event = 0x80,
CommandState_Error = 0xC0
};
// --------------------------------------------------------------------------------------------------------------------
// this functions are called in interrupt context
// ~200 us
static uint32_t countsToValue(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
uint32_t s1 = 0;
uint16_t s2 = 0;
int16_t i;
(void)countMax;
if(countValue == 0 || countValue == ADC_COUNTS_SIZE-1) return 0;
i = countValue-1;
s1 += (uint32_t)counts[i] * i;
s2 += counts[i];
i = countValue;
s1 += (uint32_t)counts[i] * i;
s2 += counts[i];
i = countValue+1;
s1 += (uint32_t)counts[i] * i;
s2 += counts[i];
s1 <<= 8;
s1 /= s2;
return s1;
/*
uint32_t s1 = 0;
uint16_t s2 = 0;
int16_t i;
(void)countMax;
for(i = (int16_t)countValue - INTEGRATE_WIDTH/2; i <= countValue + INTEGRATE_WIDTH/2; ++i) {
if(i < 0 || i >= ADC_COUNTS_SIZE) continue;
s1 += (uint32_t)counts[i] * i;
s2 += counts[i];
}
s1 <<= 8;
s1 /= s2;
return s1;
*/
}
static void onResult_temp(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
(void)counts; (void)countMax;
// with small quasi-FIR-filter
tempRaw = (tempRaw + 1) / 2 + countValue;
}
static void onResult_mainFast(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
(void)counts; (void)countMax;
uMainRaw = (uMainRaw + 1) / 2 + ((uint32_t)countValue << 8);
ADC_start(ADC_CH_TEMP, ADC_N_FAST, &onResult_temp);
}
static void onResult_senseFast(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
(void)counts; (void)countMax;
uSenseRaw = (uSenseRaw + 1) / 2 + ((uint32_t)countValue << 8);
ADC_start(ADC_CH_TEMP, ADC_N_FAST, &onResult_temp);
}
static void onResult_main(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
uMainRaw = (uMainRaw + 1) / 2 + countsToValue(counts, countMax, countValue);
ADC_start(ADC_CH_SENSE, ADC_N_FAST, &onResult_senseFast);
}
static void onResult_sense(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
uSenseRaw = (uSenseRaw + 1) / 2 + countsToValue(counts, countMax, countValue);
ADC_start(ADC_CH_MAIN, ADC_N_FAST, &onResult_mainFast);
}
static void onResult_uSup(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
(void)counts; (void)countMax;
uSupRaw = (uSupRaw + 1) / 2 + countValue;
}
static void onResult_cal1First(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
cal1First = (cal1First + 1) / 2 + countsToValue(counts, countMax, countValue);
}
static void onResult_cal1Sec(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
cal1Sec = (cal1Sec + 1) / 2 + countsToValue(counts, countMax, countValue);
}
static void onResult_cal2First(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
cal2First = (cal2First + 1) / 2 + countsToValue(counts, countMax, countValue);
}
static void onResult_cal2Sec(const uint8_t* counts, uint8_t countMax, uint16_t countValue) {
cal2Sec = (cal2Sec + 1) / 2 + countsToValue(counts, countMax, countValue);
}
// ~13 us
void SYSTEMTIMER_onTick(void) {
BEEP_process();
ENCODERBUTTON_cycle();
BUTTON_cycle();
++sCount;
sSum += (conn4 ? uSenseRaw : uMainRaw);
}
// 517 us in worst case
void SYSTEMTIMER_onTack(void) {
if(++tickCount == 50) {
// ADC-cycle duration: ~25 ms from start until return from last onResult.
// Start here the chain precision->fast->temp
// Do it 10 times per second
//GPIOD->ODR |= GPIO_ODR_2;
tickCount = 0;
if(AdcMode_Normal == adcMode) {
if(conn4)
ADC_start(ADC_CH_SENSE, ADC_N, &onResult_sense);
else
ADC_start(ADC_CH_MAIN, ADC_N, &onResult_main);
}
else {
switch(adcMode) {
case AdcMode_Sup:
ADC_start(ADC_CH_SUP, ADC_N_FAST, &onResult_uSup);
break;
case AdcMode_Cal1First:
ADC_start(ADC_CH_MAIN, ADC_N, &onResult_cal1First);
adcMode = AdcMode_Cal1Sec;
break;
case AdcMode_Cal1Sec:
ADC_start(ADC_CH_SENSE, ADC_N, &onResult_cal1Sec);
adcMode = AdcMode_Cal1First;
break;
case AdcMode_Cal2First:
ADC_start(ADC_CH_MAIN, ADC_N, &onResult_cal2First);
adcMode = AdcMode_Cal2Sec;
break;
case AdcMode_Cal2Sec:
ADC_start(ADC_CH_SENSE, ADC_N, &onResult_cal2Sec);
adcMode = AdcMode_Cal2First;
break;
default:
;
}
}
//GPIOD->ODR &= ~GPIO_ODR_2;
}
}
// --------------------------------------------------------------------------------------------------------------------
static uint16_t recalcValue(uint32_t raw, const struct ValueCoef* coef) {
uint32_t v;
if(raw < coef->offset) return 0;
v = raw - coef->offset;
v *= coef->mul;
v >>= coef->div;
if(v > UINT16_MAX) v = UINT16_MAX;
return (uint16_t)v;
}
static void recalcValues(void) {
uMain = recalcValue(uMainRaw, &CFG->uMainCoef);
uSense = recalcValue(uSenseRaw, &CFG->uSenseCoef);
}
static void updateISet(void) {
LOAD_set(recalcValue(iSetDisp, &CFG->iSetCoef));
}
static void iSetToDisp(void) {
iSetDisp = (CFG->curUnit == 0 ? iSet : iSet * 10);
}
static void iSetFromDisp(void) {
iSet = (CFG->curUnit == 0 ? iSetDisp : iSetDisp / 10);
}
static void saveMenuSettings(void) {
FLASH_unlockData();
CFG->fun = menuState.fun;
CFG->beepOn = menuState.beep;
FLASH_waitData();
FLASH_lockData();
}
static void loadMenuSettings(void) {
menuState.fun = CFG->fun;
menuState.beep = CFG->beepOn;
}
static inline void beepButton(void) {
if(CFG->beepOn) BEEP_beep(BEEP_freq_1k, 100);
}
static inline void beepEncoder(void) {
if(CFG->beepOn) BEEP_beep(BEEP_freq_1k, 2);
}
static inline void beepEncoderButton(void) {
if(CFG->beepOn) BEEP_beep(BEEP_freq_1k, 10);
}
static inline void beepError(void) {
if(CFG->beepOn) BEEP_beep(BEEP_freq_4k, 500);
}
static void prepareFun(void) {
// init with minimal allowed values to avoid error message just after start
uMainRaw = CFG->uNegative;
uSenseRaw = CFG->uNegative;
tempRaw = CFG->tempLimit;
mode = (CFG->fun == 0) ? Mode_Fun1 : Mode_Fun2;
adcMode = AdcMode_Normal;
}
static void startMenu(void) {
if(CFG->beepOn) BEEP_beep(BEEP_freq_1k, 500);
mode = Mode_MenuFun;
loadMenuSettings();
}
static void stopMenu(void) {
saveMenuSettings();
prepareFun();
}
static void startBooting(void) {
mode = Mode_Booting;
adcMode = AdcMode_Sup;
fanState = FanState_Override;
FAN_set(100);
if(CFG->beepOn) BEEP_beep(BEEP_freq_1k, 250);
}
static void stopBooting(void) {
FAN_set(0);
fanState = FanState_Off;
if(BUTTON_isPressed())
startMenu();
else
prepareFun();
}
static void doBooting(void) {
if(cycleBeginMs >= 500) {
if(uSupRaw < CFG->uSupMin) { // cannot be reset
error |= ERROR_SUPPLY;
}
}
if(cycleBeginMs >= 2000) {
stopBooting();
}
}
static void saveSettings() {
if(iSet != CFG->iSet || uSet != CFG->uSet) {
FLASH_unlockData();
CFG->uSet = uSet;
CFG->iSet = iSet;
FLASH_waitData();
FLASH_lockData();
}
}
static void copyActualValues(void) {
// Cannot avoid 'sim' here
disable_irq();
sCountCopy = sCount;
sSumCopy = sSum;
sCount = 0;
sSum = 0;
enable_irq();
}
static void startFun1(void) {
saveSettings();
mode = Mode_Fun1Run;
if(encoderMode == EncoderMode_U1 || encoderMode == EncoderMode_U0)
encoderMode = EncoderMode_I0;
iSetFromDisp();
updateISet();
LOAD_start();
conn4 = false;
fun1State.warning = false;
fun1State.flip = false;
fun1State.lastBeep = 0;
}
static void stopFun1(void) {
mode = Mode_Fun1;
LOAD_stop();
}
static void doFun1(void) {
if(error) {
stopFun1();
return;
}
if((int32_t)uMain * iSet >= powLimit) {
iSet = (powLimit / uMain / 10) * 10;
uiSetModified = true;
iSetToDisp();
updateISet();
}
if((uMain < uSet) || !LOAD_isStable()) {
if((!fun1State.warning) || (cycleBeginMs - fun1State.lastBeep >= 500)) {
if(CFG->beepOn) BEEP_beep(BEEP_freq_1k, 52);
fun1State.warning = true;
fun1State.flip = !fun1State.flip;
fun1State.lastBeep = cycleBeginMs;
}
}
else {
fun1State.warning = false;
fun1State.flip = false;
fun1State.lastBeep = 0;
}
}
static void startFun2(void) {
saveSettings();
mode = Mode_Fun2Pre;
if(encoderMode == EncoderMode_U1 || encoderMode == EncoderMode_U0)
encoderMode = EncoderMode_I0;
iSetFromDisp();
updateISet();
conn4 = (uSense > CFG->uSenseMin);
uCoef = (conn4 ? &CFG->uSenseCoef : &CFG->uMainCoef);
fun2State.lastDisp = cycleBeginMs;
}
static void startFun2Run(void) {
mode = Mode_Fun2Run;
LOAD_start();
fun2State.disp = DisplayedValue_V;
fun2State.lastDisp = cycleBeginMs;
fun2State.u = 0;
copyActualValues();
}
static void resetFun2(void) {
fun2State.ah = 0;
fun2State.sAh = 0;
fun2State.wh = 0;
fun2State.sWh = 0;
}
static void stopFun2(void) {
mode = Mode_Fun2;
LOAD_stop();
}
static void startFun2Warn(void) {
mode = Mode_Fun2Warn;
LOAD_stop();
fun2State.flip = false;
fun2State.lastBeep = cycleBeginMs;
}
static void startFun2Res(void) {
mode = Mode_Fun2Res;
fun2State.disp = DisplayedValue_V;
}
static void stopFun2Res(void) {
resetFun2();
mode = Mode_Fun2;
conn4 = false;
}
static void doFun2Pre(void) {
if(error) {
stopFun2();
return;
}
if(cycleBeginMs - fun2State.lastDisp >= 500) {
startFun2Run();
}
}
static void doFun2(void) {
if(error) {
stopFun2();
return;
}
fun2State.u = (conn4 ? uSense : uMain);
if((fun2State.u < uSet) || !LOAD_isStable()) {
startFun2Warn();
return;
}
if((int32_t)uMain * iSet >= powLimit) {
iSet = (powLimit / uMain / 10) * 10;
uiSetModified = true;
iSetToDisp();
updateISet();
}
if(cycleBeginMs - fun2State.lastDisp >= 2500) {
fun2State.disp = (enum DisplayedValue)(((uint8_t)fun2State.disp + 1) % 3);
fun2State.lastDisp = cycleBeginMs;
}
copyActualValues();
{
uint64_t wh;
uint32_t off = sCountCopy * uCoef->offset;
uint64_t sWh = (uint64_t)(sSumCopy > off ? sSumCopy - off : 0) * iSet;
fun2State.sAh += sCountCopy * iSet;
fun2State.ah = fun2State.sAh / (3600uL * 1000 / 2);
fun2State.sWh += sWh;
wh = fun2State.sWh / (3600uL * 1000 * 1000 / 2);
wh *= uCoef->mul;
wh >>= uCoef->div;
if(wh > UINT32_MAX) wh = UINT32_MAX;
fun2State.wh = (uint32_t)wh;
if(fun2State.ah > CFG->ahMax || fun2State.wh > CFG->whMax) {
startFun2Warn();
return;
}
}
}
static void doFun2Warn(void) {
if(cycleBeginMs - fun2State.lastBeep >= 100) {
if(CFG->beepOn) BEEP_beep(BEEP_freq_1k, 40);
fun2State.flip = !fun2State.flip;
fun2State.lastBeep = cycleBeginMs;
}
}
static inline void checkErrors(void) {
uint16_t temp = tempRaw;
uint32_t v1 = uMainRaw, v2 = uSenseRaw;
uint8_t exist = error;
if(uMainRaw < CFG->uNegative || uSenseRaw < CFG->uNegative) {
error |= ERROR_POLARITY;
}
else {
error &= ~ERROR_POLARITY;
}
if(uMain >= CFG->uMainLimit) {
error |= ERROR_OUP;
}
else {
error &= ~ERROR_OUP;
}
if(temp >= CFG->tempDefect) {
error |= ERROR_ERT;
}
if(temp < CFG->tempLimit) {
error |= ERROR_OTP;
}
else {
error &= ~ERROR_OTP;
}
if(!exist && error)
beepError();
}
static inline void controlFan(void) {
uint16_t temp = tempRaw;
switch(fanState) {
case FanState_Override:
break;
case FanState_Off:
if(temp < CFG->tempFanLow) {
fanState = FanState_Low;
FAN_set(33);
}
break;
case FanState_Low:
if(temp < CFG->tempFanMid) {
fanState = FanState_Mid;
FAN_set(66);
}
if(temp >= CFG->tempFanLow + CFG->tempThreshold) {
fanState = FanState_Off;
FAN_set(0);
}
break;
case FanState_Mid:
if(temp < CFG->tempFanFull) {
fanState = FanState_Full;
FAN_set(100);
}
if(temp >= CFG->tempFanMid + CFG->tempThreshold) {
fanState = FanState_Low;
FAN_set(33);
}
break;
case FanState_Full:
if(temp >= CFG->tempFanFull + CFG->tempThreshold) {
fanState = FanState_Mid;
FAN_set(66);
}
break;
}
}
void BUTTON_onRelease(bool longpress) {
if(error) return;
if(inputDisable & INPUT_DISABLE_BUTTON) return;
switch(mode) {
case Mode_Booting:
break;
case Mode_MenuFun:
mode = Mode_MenuBeep;
beepButton();
break;
case Mode_MenuBeep:
mode = Mode_MenuCalV;
beepButton();
break;
case Mode_MenuCalV:
mode = Mode_MenuCalI;
beepButton();
break;
case Mode_CalV1:
case Mode_CalV2:
case Mode_CalI1r:
case Mode_CalI1v:
case Mode_CalI2r:
case Mode_CalI2v:
adcMode = AdcMode_None;
LOAD_stop();
LOAD_set(0);
// fallthrough
case Mode_MenuCalI:
stopMenu();
beepButton();
break;
case Mode_Fun1:
startFun1();
beepButton();
break;
case Mode_Fun1Run:
stopFun1();
beepButton();
break;
case Mode_Fun2:
if(longpress) resetFun2();
else startFun2();
beepButton();
break;
case Mode_Fun2Run:
stopFun2();
beepButton();
break;
case Mode_Fun2Warn:
startFun2Res();
beepButton();
break;
case Mode_Fun2Res:
stopFun2Res();
beepButton();
break;
}
}
static void updateValue(uint16_t* v, int16_t d, uint16_t min, uint16_t max) {
if(d < 0) {
if(*v < min - d)
*v = min;
else
*v += d;
}
else {
if(*v > max - d)
*v = max;
else
*v += d;
}
}
static void updateCalValue(uint16_t* v, int16_t d, uint16_t min, uint16_t max) {
switch(encoderMode) {
case EncoderMode_I1: updateValue(v, 100*d, min, max); break;
case EncoderMode_I0: updateValue(v, d, min, max); break;
case EncoderMode_U1: updateValue(v, 100*d, min, max); break;
case EncoderMode_U0: updateValue(v, d, min, max); break;
}
beepEncoder();
}
void ENCODER_onChange(int8_t d) {
uint16_t t;
if(error) return;
if(inputDisable & INPUT_DISABLE_ENCODER) return;
switch(mode) {
case Mode_Booting:
break;
case Mode_MenuFun:
menuState.fun = (menuState.fun + 1) % 2;
beepEncoder();
break;
case Mode_MenuBeep:
menuState.beep = !menuState.beep;
beepEncoder();
break;
case Mode_MenuCalV:
case Mode_MenuCalI:
break;
case Mode_CalV1:
updateCalValue(&cal1Disp, d, CFG->uSetMin, CFG->uMainLimit);
break;
case Mode_CalV2:
updateCalValue(&cal2Disp, d, CFG->uSetMin, CFG->uMainLimit);
break;
case Mode_CalI1r:
t = cal1First;
updateCalValue(&t, d, 0, LOAD_MAX);
cal1First = t;
LOAD_set(t);
break;
case Mode_CalI1v:
updateCalValue(&cal1Disp, d, CFG->iSetMin, CFG->iSetMax);
break;
case Mode_CalI2r:
t = cal2First;
updateCalValue(&t, d, 0, LOAD_MAX);
cal2First = t;
LOAD_set(t);
break;
case Mode_CalI2v:
updateCalValue(&cal2Disp, d, CFG->iSetMin, CFG->iSetMax);
break;
case Mode_Fun1:
case Mode_Fun1Run:
case Mode_Fun2:
case Mode_Fun2Run:
switch(encoderMode) {
case EncoderMode_I1: updateValue(&iSetDisp, 100*d, CFG->iSetMin, CFG->iSetMax); break;
case EncoderMode_I0: updateValue(&iSetDisp, 10*d, CFG->iSetMin, CFG->iSetMax); break;
case EncoderMode_U1: updateValue(&uSet, 1000*d, CFG->uSetMin, CFG->uSetMax); break;
case EncoderMode_U0: updateValue(&uSet, 100*d, CFG->uSetMin, CFG->uSetMax); break;
}
beepEncoder();
uiSetModified = true;
iSetFromDisp();
updateISet();
break;
case Mode_Fun2Warn:
break;
case Mode_Fun2Res:
fun2State.disp = (enum DisplayedValue)(((uint8_t)fun2State.disp + 3 + d) % 3);
break;
}
}
static void startCalV1() {
cal1Disp = CFG->uSetMin;
mode = Mode_CalV1;
encoderMode = EncoderMode_U1;
adcMode = AdcMode_Cal1First;
}
static void startCalV2() {
cal2Disp = CFG->uSetMax;
mode = Mode_CalV2;
encoderMode = EncoderMode_U1;
adcMode = AdcMode_Cal2First;
}
static void startCalI1r() {
cal1First = 0;
mode = Mode_CalI1r;
encoderMode = EncoderMode_I1;
FAN_set(100);
LOAD_set(cal1First);
LOAD_start();
}
static void startCalI1v() {
cal1Disp = 0;
mode = Mode_CalI1v;
encoderMode = EncoderMode_I1;
}
static void startCalI2r() {
cal2First = 0;
mode = Mode_CalI2r;
encoderMode = EncoderMode_I1;
FAN_set(100);
LOAD_set(cal2First);
LOAD_start();
}
static void startCalI2v() {
cal2Disp = 0;
mode = Mode_CalI2v;
encoderMode = EncoderMode_I1;
}
static bool calcCal(uint32_t r1, uint32_t r2, uint16_t v1, uint16_t v2, struct ValueCoef* coef) {
int32_t t1, t2, o;
uint32_t m;
int32_t d = (int32_t)v1 - (int32_t)v2;
if(d == 0) return false;
t1 = r2*v1;
t2 = r1*v2;
o = (t1 - t2) / d;