forked from lincomatic/open_evse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_evse.ino
2199 lines (1968 loc) · 51.1 KB
/
open_evse.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
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
// -*- C++ -*-
/*
* Open EVSE Firmware
*
* Copyright (c) 2011-2015 Sam C. Lin <[email protected]>
* Copyright (c) 2011-2014 Chris Howell <[email protected]>
* timer code Copyright (c) 2013 Kevin L <[email protected]>
* portions Copyright (c) 2014-2015 Nick Sayer <[email protected]>
* portions Copyright (c) 2015 Craig Kirkpatrick
Revised Ver By Reason
6/21/13 20b3 Scott Rubin fixed LCD display bugs with RTC enabled
6/25/13 20b4 Scott Rubin fixed LCD display bugs, CLI fixes, when RTC disabled
6/30/13 20b5 Scott Rubin added LcdDetected() function, prevents hang if LCD not installed
7/06/13 20b5 Scott Rubin rewrote power detection in POST function for 1 or 2 relays
7/11/13 20b5 Scott Rubin skips POST if EV is connected, won't charge if open ground or stuck relay
8/12/13 20b5b Scott Rubin fix GFI error - changed gfi.Reset() to check for constant GFI signal
8/26/13 20b6 Scott Rubin add Stuck Relay State delay, fix Stuck Relay state exit (for Active E)
9/20/13 20b7 Chris Howell updated/tweaked/shortened CLI messages
10/25/14 Craig K add smoothing to the Amperage readout
3/1/15 Craig K add TEMPERATURE_MONITORING
3/7/15 Craig K add KWH_RECORDING
* This file is part of Open EVSE.
* Open EVSE is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
* Open EVSE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Open EVSE; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <pins_arduino.h>
#include "./Wire.h"
#include "./RTClib.h"
#include "open_evse.h"
// if using I2CLCD_PCF8574 uncomment below line and comment out LiquidTWI2.h above
//#include <LiquidCrystal_I2C.h>
#ifdef TEMPERATURE_MONITORING
#ifdef MCP9808_IS_ON_I2C
#include "./Adafruit_MCP9808.h" // adding the ambient temp sensor to I2C
#endif
#ifdef TMP007_IS_ON_I2C
#include "./Adafruit_TMP007.h" // adding the TMP007 IR I2C sensor
#endif
#endif // TEMPERATURE_MONITORING
SettingsMenu g_SettingsMenu;
SetupMenu g_SetupMenu;
SvcLevelMenu g_SvcLevelMenu;
MaxCurrentMenu g_MaxCurrentMenu;
DiodeChkMenu g_DiodeChkMenu;
#ifdef RGBLCD
BklTypeMenu g_BklTypeMenu;
#endif // RGBLCD
#ifdef GFI_SELFTEST
GfiTestMenu g_GfiTestMenu;
#endif
VentReqMenu g_VentReqMenu;
#ifdef ADVPWR
GndChkMenu g_GndChkMenu;
RlyChkMenu g_RlyChkMenu;
#endif // ADVPWR
ResetMenu g_ResetMenu;
// Instantiate additional Menus - GoldServe
#if defined(DELAYTIMER_MENU)
RTCMenu g_RTCMenu;
RTCMenuMonth g_RTCMenuMonth;
RTCMenuDay g_RTCMenuDay;
RTCMenuYear g_RTCMenuYear;
RTCMenuHour g_RTCMenuHour;
RTCMenuMinute g_RTCMenuMinute;
DelayMenu g_DelayMenu;
DelayMenuEnableDisable g_DelayMenuEnableDisable;
DelayMenuStartHour g_DelayMenuStartHour;
DelayMenuStopHour g_DelayMenuStopHour;
DelayMenuStartMin g_DelayMenuStartMin;
DelayMenuStopMin g_DelayMenuStopMin;
#endif // DELAYTIMER_MENU
#ifdef CHARGE_LIMIT
ChargeLimitMenu g_ChargeLimitMenu;
#endif // CHARGE_LIMIT
#ifdef TIME_LIMIT
TimeLimitMenu g_TimeLimitMenu;
#endif // TIME_LIMIT
Menu *g_SettingsMenuList[] = {
#ifdef TIME_LIMIT
&g_TimeLimitMenu,
#endif // TIME_LIMIT
#ifdef CHARGE_LIMIT
&g_ChargeLimitMenu,
#endif // CHARGE_LIMIT
#ifdef DELAYTIMER_MENU
&g_DelayMenu,
#endif // DELAYTIMER_MENU
&g_SetupMenu,
&g_ResetMenu,
NULL
};
Menu *g_SetupMenuList[] = {
#ifdef DELAYTIMER_MENU
&g_RTCMenu,
#endif // DELAYTIMER_MENU
#ifdef RGBLCD
&g_BklTypeMenu,
#endif // RGBLCD
&g_SvcLevelMenu,
&g_MaxCurrentMenu,
&g_DiodeChkMenu,
&g_VentReqMenu,
#ifdef ADVPWR
&g_GndChkMenu,
&g_RlyChkMenu,
#endif // ADVPWR
#ifdef GFI_SELFTEST
&g_GfiTestMenu,
#endif // GFI_SELFTEST
NULL
};
#ifdef BTN_MENU
BtnHandler g_BtnHandler;
#endif // BTN_MENU
#ifdef DELAYTIMER
#define g_sHHMMfmt "%02d:%02d"
#endif// DELAYTIMER
//-- begin global variables
char g_sTmp[TMP_BUF_SIZE];
OnboardDisplay g_OBD;
// Instantiate RTC and Delay Timer - GoldServe
#ifdef RTC
RTC_DS1307 g_RTC;
DateTime g_CurrTime;
#if defined(RAPI)
void SetRTC(uint8_t y,uint8_t m,uint8_t d,uint8_t h,uint8_t mn,uint8_t s) {
g_RTC.adjust(DateTime(y,m,d,h,mn,s));
}
void GetRTC(char *buf) {
g_CurrTime = g_RTC.now();
sprintf(buf,"%d %d %d %d %d %d",g_CurrTime.year()-2000,g_CurrTime.month(),g_CurrTime.day(),g_CurrTime.hour(),g_CurrTime.minute(),g_CurrTime.second());
}
#endif // RAPI
#endif // RTC
#ifdef DELAYTIMER
DelayTimer g_DelayTimer;
#ifdef DELAYTIMER_MENU
// Start variables to support RTC and Delay Timer - GoldServe
uint16_t g_year;
uint8_t g_month;
uint8_t g_day;
uint8_t g_hour;
uint8_t g_min;
uint8_t sec = 0;
#endif // DELAYTIMER_MENU
#endif // DELAYTIMER
#ifdef TEMPERATURE_MONITORING
TempMonitor g_TempMonitor;
#endif // TEMPERATURE_MONITORING
#ifdef KWH_RECORDING
unsigned long g_WattHours_accumulated;
unsigned long g_WattSeconds;
#endif // KWH_RECORDING
//-- end global variables
static inline void wiresend(uint8_t x) {
#if ARDUINO >= 100
Wire.write((uint8_t)x);
#else
Wire.send(x);
#endif
}
static inline uint8_t wirerecv(void) {
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
// if digits > 0, zero pad w/ to # digits
// WARNING: This function uses the *end* of g_sTmp as its buffer
char *u2a(unsigned long x,int8_t digits)
{
int8_t d = digits;
char *s = g_sTmp + sizeof(g_sTmp);
*--s = 0;
if (!x) {
*--s = '0';
d--;
}
else {
for (; x; x/=10) {
*--s = '0'+ x%10;
d--;
}
}
for (;d > 0;d--) {
*--s = '0';
}
return s;
}
void EvseReset();
// wdt_init turns off the watchdog timer after we use it
// to reboot
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));
void wdt_init(void)
{
MCUSR = 0;
wdt_disable();
return;
}
#ifdef TEMPERATURE_MONITORING
void TempMonitor::Init()
{
m_Flags = 0;
m_MCP9808_temperature = 230; // 230 means 23.0C Using an integer to save on floating point library use
m_DS3231_temperature = 230; // the DS3231 RTC has a built in temperature sensor
m_TMP007_temperature = 230;
#ifdef MCP9808_IS_ON_I2C
m_tempSensor.begin();
#endif // MCP9808_IS_ON_I2C
#ifdef TMP007_IS_ON_I2C
m_tmp007.begin();
#endif // TMP007_IS_ON_I2C
}
void TempMonitor::Read()
{
#ifdef TMP007_IS_ON_I2C
m_TMP007_temperature = m_tmp007.readObjTempC10(); // using the TI TMP007 IR sensor
#endif
#ifdef MCP9808_IS_ON_I2C
m_MCP9808_temperature = m_tempSensor.readTempC10(); // for the MCP9808
if (m_MCP9808_temperature == 2303) {
m_MCP9808_temperature = 0; } // return 0 if the sensor is not present on the I2C bus
#endif
#ifdef RTC
// This code chunck below reads the DS3231 RTC's internal temperature sensor
Wire.beginTransmission(0x68);
wiresend(uint8_t(0x0e));
wiresend( 0x20 ); // write bit 5 to initiate conversion of temperature
Wire.endTransmission();
Wire.beginTransmission(0x68);
wiresend(uint8_t(0x11));
Wire.endTransmission();
Wire.requestFrom(0x68, 2);
m_DS3231_temperature = 10 * wirerecv(); // Here's the MSB
m_DS3231_temperature = m_DS3231_temperature + (5*(wirerecv()>>6))/2; // keep the reading like 235 meaning 23.5C
if (m_DS3231_temperature == 0x09FD) m_DS3231_temperature = 0; // If the DS3231 is not present then return 0
#ifdef OPENEVSE_2
m_DS3231_temperature = 0; // If the DS3231 is not present then return 0, OpenEVSE II does not use the DS3231
#endif
#endif // RTC
}
#endif // TEMPERATURE_MONITORING
OnboardDisplay::OnboardDisplay()
#if defined(I2CLCD) || defined(RGBLCD)
#ifdef I2CLCD_PCF8574
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
: m_Lcd(LCD_I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE)
#else
: m_Lcd(LCD_I2C_ADDR,1)
#endif // I2CLCD_PCF8574
#endif // defined(I2CLCD) || defined(RGBLCD)
{
}
#if defined(DELAYTIMER)||defined(TIME_LIMIT)
const char CustomChar_0[8] PROGMEM = {0x0,0xe,0x15,0x17,0x11,0xe,0x0,0x0}; // clock
#endif
#ifdef DELAYTIMER
const char CustomChar_1[8] PROGMEM = {0x0,0x0,0xe,0xe,0xe,0x0,0x0,0x0}; // stop (cube)
const char CustomChar_2[8] PROGMEM = {0x0,0x8,0xc,0xe,0xc,0x8,0x0,0x0}; // play
#endif // DELAYTIMER
#if defined(DELAYTIMER)||defined(CHARGE_LIMIT)
const char CustomChar_3[8] PROGMEM = {0x0,0xe,0xc,0x1f,0x3,0x6,0xc,0x8}; // lightning
#endif
void OnboardDisplay::Init()
{
WDT_RESET();
#ifdef RGBLCD
m_bFlags = 0;
#else
m_bFlags = OBDF_MONO_BACKLIGHT;
#endif // RGBLCD
#ifdef GREEN_LED_REG
pinGreenLed.init(GREEN_LED_REG,GREEN_LED_IDX,DigitalPin::OUT);
SetGreenLed(0);
#endif
#ifdef RED_LED_REG
pinRedLed.init(RED_LED_REG,RED_LED_IDX,DigitalPin::OUT);
SetRedLed(0);
#endif
#ifdef LCD16X2
LcdBegin(LCD_MAX_CHARS_PER_LINE, 2);
LcdSetBacklightColor(WHITE);
#if defined(DELAYTIMER)||defined(TIME_LIMIT)
memcpy_P(g_sTmp,CustomChar_0,8);
m_Lcd.createChar(0, (uint8_t*)g_sTmp);
#endif
#ifdef DELAYTIMER
memcpy_P(g_sTmp,CustomChar_1,8);
m_Lcd.createChar(1, (uint8_t*)g_sTmp);
memcpy_P(g_sTmp,CustomChar_2,8);
m_Lcd.createChar(2, (uint8_t*)g_sTmp);
#endif //#ifdef DELAYTIMER
#if defined(DELAYTIMER)||defined(CHARGE_LIMIT)
memcpy_P(g_sTmp,CustomChar_3,8);
m_Lcd.createChar(3, (uint8_t*)g_sTmp);
#endif
m_Lcd.clear();
#ifdef OPENEVSE_2
LcdPrint_P(0,PSTR("Open EVSE II"));
#else
LcdPrint_P(0,PSTR("Open EVSE"));
#endif
LcdPrint_P(0,1,PSTR("Ver. "));
LcdPrint_P(VERSTR);
delay(1500);
WDT_RESET();
#endif //#ifdef LCD16X2
}
#ifdef LCD16X2
void OnboardDisplay::LcdPrint(int x,int y,const char *s)
{
m_Lcd.setCursor(x,y);
m_Lcd.print(s);
}
void OnboardDisplay::LcdPrint_P(const char PROGMEM *s)
{
strncpy_P(m_strBuf,s,LCD_MAX_CHARS_PER_LINE);
m_strBuf[LCD_MAX_CHARS_PER_LINE] = 0;
m_Lcd.print(m_strBuf);
}
void OnboardDisplay::LcdPrint_P(int y,const char PROGMEM *s)
{
strncpy_P(m_strBuf,s,LCD_MAX_CHARS_PER_LINE);
m_strBuf[LCD_MAX_CHARS_PER_LINE] = 0;
LcdPrint(y,m_strBuf);
}
void OnboardDisplay::LcdPrint_P(int x,int y,const char PROGMEM *s)
{
strncpy_P(m_strBuf,s,LCD_MAX_CHARS_PER_LINE);
m_strBuf[LCD_MAX_CHARS_PER_LINE] = 0;
m_Lcd.setCursor(x,y);
m_Lcd.print(m_strBuf);
}
void OnboardDisplay::LcdMsg_P(const char PROGMEM *l1,const char PROGMEM *l2)
{
LcdPrint_P(0,l1);
LcdPrint_P(1,l2);
}
// print at (0,y), filling out the line with trailing spaces
void OnboardDisplay::LcdPrint(int y,const char *s)
{
m_Lcd.setCursor(0,y);
uint8_t i,len = strlen(s);
if (len > LCD_MAX_CHARS_PER_LINE)
len = LCD_MAX_CHARS_PER_LINE;
for (i=0;i < len;i++) {
m_Lcd.write(s[i]);
}
for (i=len;i < LCD_MAX_CHARS_PER_LINE;i++) {
m_Lcd.write(' ');
}
}
void OnboardDisplay::LcdMsg(const char *l1,const char *l2)
{
LcdPrint(0,l1);
LcdPrint(1,l2);
}
#endif // LCD16X2
void OnboardDisplay::Update(int8_t updmode)
{
if (updateDisabled()) return;
int i;
uint8_t curstate = g_EvseController.GetState();
uint8_t svclvl = g_EvseController.GetCurSvcLevel();
int currentcap = g_EvseController.GetCurrentCapacity();
unsigned long curms = millis();
if (g_EvseController.StateTransition() || (updmode != OBD_UPD_NORMAL)) {
curms += 1000; // trigger periodic update code below
sprintf(g_sTmp,g_sRdyLAstr,(int)svclvl,currentcap);
switch(curstate) {
case EVSE_STATE_A: // not connected
SetGreenLed(1);
SetRedLed(0);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(GREEN);
// Display Timer and Stop Icon - GoldServe
LcdClear();
LcdSetCursor(0,0);
#ifdef DELAYTIMER
g_DelayTimer.PrintTimerIcon();
#endif //#ifdef DELAYTIMER
LcdPrint_P(g_psReady);
LcdPrint(10,0,g_sTmp);
#ifdef KWH_RECORDING
sprintf(g_sTmp,"%5luWh",(g_WattSeconds / 3600) );
LcdPrint(0,1,g_sTmp);
sprintf(g_sTmp,"%6lukWh",(g_WattHours_accumulated / 1000)); // display accumulated kWh
LcdPrint(7,1,g_sTmp);
#endif // KWH_RECORDING
#endif //Adafruit RGB LCD
// n.b. blue LED is off
break;
case EVSE_STATE_B: // connected/not charging
SetGreenLed(1);
SetRedLed(1);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(YELLOW);
LcdClear();
LcdSetCursor(0,0);
#ifdef CHARGE_LIMIT
if (g_EvseController.GetChargeLimit()) {
LcdWrite(3); // lightning
}
#endif
#ifdef TIME_LIMIT
if (g_EvseController.GetTimeLimit()) {
LcdWrite(0); // clock
}
#endif
// Display Timer and Stop Icon - GoldServe
#ifdef DELAYTIMER
g_DelayTimer.PrintTimerIcon();
#endif //#ifdef DELAYTIMER
LcdPrint_P(g_psEvConnected);
LcdPrint(10,0,g_sTmp);
#ifdef KWH_RECORDING
sprintf(g_sTmp,"%5luWh",(g_WattSeconds / 3600) );
LcdPrint(0,1,g_sTmp);
sprintf(g_sTmp,"%6lukWh",(g_WattHours_accumulated / 1000)); // Display accumulated kWh
LcdPrint(7,1,g_sTmp);
#endif // KWH_RECORDING
#endif //Adafruit RGB LCD
// n.b. blue LED is off
break;
case EVSE_STATE_C: // charging
SetGreenLed(0);
SetRedLed(0);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(TEAL);
LcdClear();
LcdSetCursor(0,0);
// Display Timer and Stop Icon - GoldServe
#ifdef CHARGE_LIMIT
if (g_EvseController.GetChargeLimit()) {
LcdWrite(3); // lightning
}
#endif
#ifdef TIME_LIMIT
if (g_EvseController.GetTimeLimit()) {
LcdWrite(0); // clock
}
#endif
#ifdef DELAYTIMER
g_DelayTimer.PrintTimerIcon();
#endif //#ifdef DELAYTIMER
LcdPrint_P(g_psCharging);
#endif //Adafruit RGB LCD
// n.b. blue LED is on
break;
case EVSE_STATE_D: // vent required
SetGreenLed(0);
SetRedLed(1);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(RED);
LcdMsg_P(g_psEvseError,g_psVentReq);
#endif //Adafruit RGB LCD
// n.b. blue LED is off
break;
case EVSE_STATE_DIODE_CHK_FAILED:
SetGreenLed(0);
SetRedLed(1);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(RED);
LcdMsg_P(g_psEvseError,g_psDiodeChkFailed);
#endif //Adafruit RGB LCD
// n.b. blue LED is off
break;
case EVSE_STATE_GFCI_FAULT:
SetGreenLed(0);
SetRedLed(1);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(RED);
if (updmode == OBD_UPD_HARDFAULT) {
LcdMsg_P(g_psEvseError,g_psGfciFault);
}
else {
// 2nd line will be updated below with auto retry count
LcdPrint_P(0,g_psGfciFault);
}
#endif //Adafruit RGB LCD
// n.b. blue LED is off
break;
#ifdef TEMPERATURE_MONITORING
case EVSE_STATE_OVER_TEMPERATURE: // overtemp message in Red on the RGB LCD
SetGreenLed(0);
SetRedLed(1);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(RED);
LcdMsg_P(g_psSvcReq,g_psTemperatureFault); // SERVICE REQUIRED OVER TEMPERATURE
#endif
break;
#endif //TEMPERATURE_MONITORING
case EVSE_STATE_NO_GROUND:
SetGreenLed(0);
SetRedLed(1);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(RED);
if (updmode == OBD_UPD_HARDFAULT) {
LcdMsg_P(g_psEvseError,g_psNoGround);
}
else {
// 2nd line will be updated below with auto retry count
LcdPrint_P(0,g_psNoGround);
}
#endif //Adafruit RGB LCD
// n.b. blue LED is off
break;
case EVSE_STATE_STUCK_RELAY:
SetGreenLed(0);
SetRedLed(1);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(RED);
LcdMsg_P(updmode == OBD_UPD_HARDFAULT ? g_psSvcReq : g_psEvseError,g_psStuckRelay);
#endif //Adafruit RGB LCD
// n.b. blue LED is off
break;
case EVSE_STATE_DISABLED:
SetGreenLed(0);
SetRedLed(1);
#ifdef LCD16X2
LcdSetBacklightColor(VIOLET);
LcdClear();
LcdSetCursor(0,0);
LcdPrint_P(g_psDisabled);
LcdPrint(10,0,g_sTmp);
#endif // LCD16X2
break;
case EVSE_STATE_GFI_TEST_FAILED:
SetGreenLed(0);
SetRedLed(1);
LcdSetBacklightColor(RED);
LcdMsg_P(g_psTestFailed,g_psGfci);
break;
case EVSE_STATE_SLEEPING:
SetGreenLed(1);
SetRedLed(1);
#ifdef LCD16X2
LcdSetBacklightColor(VIOLET);
LcdClear();
LcdSetCursor(0,0);
LcdPrint_P(g_psSleeping);
LcdPrint(10,0,g_sTmp);
#endif // LCD16X2
break;
default:
SetGreenLed(0);
SetRedLed(1);
// n.b. blue LED is off
}
}
//
// put anything that needs to be updated periodically here
// the code below will only run once per second
//
if ((curms-m_LastUpdateMs) >= 1000) {
m_LastUpdateMs = curms;
if (!g_EvseController.InHardFault() &&
((curstate == EVSE_STATE_GFCI_FAULT) || (curstate == EVSE_STATE_NO_GROUND))) {
strcpy(g_sTmp,g_sRetryIn);
int resetsec = (int)(g_EvseController.GetResetMs() / 1000ul);
if (resetsec >= 0) {
sprintf(g_sTmp+sizeof(g_sTmp)-6,g_sHHMMfmt,resetsec / 60,resetsec % 60);
strcat(g_sTmp,g_sTmp+sizeof(g_sTmp)-6);
LcdPrint(1,g_sTmp);
}
return;
}
#ifdef RTC
g_CurrTime = g_RTC.now();
#endif
#ifdef LCD16X2
#if defined(AMMETER)
if (((curstate == EVSE_STATE_C) || g_EvseController.AmmeterCalEnabled()) && AmmeterIsDirty()) {
SetAmmeterDirty(0);
uint32_t current = g_EvseController.GetChargingCurrent();
if (current >= 1000) { // display only if > 1000
int a = current / 1000;
int ma = (current % 1000) / 100;
if (ma > 9) {
ma = 0;
a++;
}
sprintf(g_sTmp,"%3d.%dA",a,ma);
}
else {
strcpy_P(g_sTmp,PSTR(" 0A"));
}
LcdPrint(10,0,g_sTmp);
}
#endif // AMMETER
if (curstate == EVSE_STATE_C) {
time_t elapsedTime = g_EvseController.GetElapsedChargeTime();
#ifdef KWH_RECORDING
uint32_t current = g_EvseController.GetChargingCurrent();
#ifdef VOLTMETER
g_WattSeconds = g_WattSeconds + (g_EvseController.GetVoltage() / 1000 * current / 1000);
#else
if (g_EvseController.GetCurSvcLevel() == 2) { // first verify L2 or L1
g_WattSeconds = g_WattSeconds + (VOLTS_FOR_L2 * current / 1000); // accumulate Watt Seconds for Level2 charging
}
else {
g_WattSeconds = g_WattSeconds + (VOLTS_FOR_L1 * current / 1000); // accumulate Watt Seconds for Level1 charging
}
#endif // VOLTMETER
sprintf(g_sTmp,"%5luWh",(g_WattSeconds / 3600) );
LcdPrint(0,1,g_sTmp);
#ifdef VOLTMETER
sprintf(g_sTmp," %3luV",(g_EvseController.GetVoltage() / 1000)); // Display voltage from OpenEVSE II
LcdPrint(11,1,g_sTmp);
#else
sprintf(g_sTmp,"%6lukWh",(g_WattHours_accumulated / 1000)); // display accumulated kWh
LcdPrint(7,1,g_sTmp);
#endif // VOLTMETER
#endif // KWH_RECORDING
#ifdef TEMPERATURE_MONITORING
if ((g_TempMonitor.OverTemperature()) || TEMPERATURE_DISPLAY_ALWAYS) {
g_OBD.LcdClearLine(1);
const char *tempfmt = "%2d.%1dC";
#ifdef MCP9808_IS_ON_I2C
if ( g_TempMonitor.m_MCP9808_temperature != 0 ) { // it returns 0 if it is not present
sprintf(g_sTmp,tempfmt,g_TempMonitor.m_MCP9808_temperature/10, g_TempMonitor.m_MCP9808_temperature % 10); // Ambient sensor near or on the LCD
LcdPrint(0,1,g_sTmp);
}
#endif
#ifdef RTC
if ( g_TempMonitor.m_DS3231_temperature != 0) { // it returns 0 if it is not present
sprintf(g_sTmp,tempfmt,g_TempMonitor.m_DS3231_temperature/10, g_TempMonitor.m_DS3231_temperature % 10); // sensor built into the DS3231 RTC Chip
LcdPrint(5,1,g_sTmp);
}
#endif
#ifdef TMP007_IS_ON_I2C
if ( g_TempMonitor.m_TMP007_temperature != 0 ) { // it returns 0 if it is not present
sprintf(g_sTmp,tempfmt,g_TempMonitor.m_TMP007_temperature/10, g_TempMonitor.m_TMP007_temperature % 10); // Infrared sensor probably looking at 30A fuses
LcdPrint(11,1,g_sTmp);
}
#endif
if (g_TempMonitor.BlinkAlarm() && g_TempMonitor.OverTemperatureShutdown()) { // Blink Red off-and-on while over the temperature shutdown limit, zero current should flow to the EV
g_TempMonitor.SetBlinkAlarm(0); // toggle the alarm flag so we can blink
SetRedLed(1);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(RED);
#endif //Adafruit RGB LCD
}
else {
g_TempMonitor.SetBlinkAlarm(1); // toggle the alarm flag so we can blink
SetRedLed(0);
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(TEAL);
#endif
}
} // (g_TempMonitor.OverTemperature()) || TEMPERATURE_DISPLAY_ALWAYS)
else {
SetRedLed(0); // restore the normal TEAL backlight in case it was left RED while last blinking
#ifdef LCD16X2 //Adafruit RGB LCD
LcdSetBacklightColor(TEAL);
#endif
}
if (!(g_TempMonitor.OverTemperature() || TEMPERATURE_DISPLAY_ALWAYS)) {
#endif // TEMPERATURE_MONITORING
#ifndef KWH_RECORDING
int h = hour(elapsedTime); // display the elapsed charge time
int m = minute(elapsedTime);
int s = second(elapsedTime);
sprintf(g_sTmp,"%02d:%02d:%02d",h,m,s);
#ifdef RTC
g_sTmp[8]=' ';
g_sTmp[9]=' ';
g_sTmp[10]=' ';
sprintf(g_sTmp+11,g_sHHMMfmt,(int)g_CurrTime.hour(),(int)g_CurrTime.minute());
#endif //RTC
LcdPrint(1,g_sTmp);
#endif // KWH_RECORDING
#ifdef TEMPERATURE_MONITORING
}
#endif // TEMPERATURE_MONITORING
} // curstate == EVSE_STATE_C
// Display a new stopped LCD screen with Delay Timers enabled - GoldServe
#ifdef DELAYTIMER
else if (curstate == EVSE_STATE_SLEEPING) {
LcdSetCursor(0,0);
g_DelayTimer.PrintTimerIcon();
// LcdPrint_P(g_DelayTimer.IsTimerEnabled() ? g_psWaiting : g_psSleeping);
LcdPrint_P(g_psSleeping);
sprintf(g_sTmp,"%02d:%02d:%02d",g_CurrTime.hour(),g_CurrTime.minute(),g_CurrTime.second());
LcdPrint(0,1,g_sTmp);
if (g_DelayTimer.IsTimerEnabled()){
LcdSetCursor(9,0);
LcdWrite(0x2);
LcdWrite(0x0);
sprintf(g_sTmp,g_sHHMMfmt,g_DelayTimer.GetStartTimerHour(),g_DelayTimer.GetStartTimerMin());
LcdPrint(11,0,g_sTmp);
LcdSetCursor(9,1);
LcdWrite(0x1);
LcdWrite(0x0);
sprintf(g_sTmp,g_sHHMMfmt,g_DelayTimer.GetStopTimerHour(),g_DelayTimer.GetStopTimerMin());
LcdPrint(11,1,g_sTmp);
} else {
sprintf(g_sTmp,g_sRdyLAstr,(int)svclvl,currentcap);
LcdPrint(10,0,g_sTmp);
}
}
#endif // DELAYTIMER
#endif // LCD16X2
}
#ifdef FT_ENDURANCE
LcdSetCursor(0,0);
sprintf(g_sTmp,"%d %d",g_CycleCnt,(int)g_EvseController.ReadACPins());
LcdPrint(g_sTmp);
#endif // FT_ENDURANCE
}
#ifdef BTN_MENU
Btn::Btn()
{
buttonState = BTN_STATE_OFF;
lastDebounceTime = 0;
vlongDebounceTime = 0;
}
void Btn::init()
{
#ifdef BTN_REG
pinBtn.init(BTN_REG,BTN_IDX,DigitalPin::INP_PU);
#endif
}
void Btn::read()
{
uint8_t sample;
unsigned long delta;
#ifdef ADAFRUIT_BTN
sample = (g_OBD.readButtons() & BUTTON_SELECT) ? 1 : 0;
#else //!ADAFRUIT_BTN
sample = btnPin.read() ? 0 : 1;
#endif // ADAFRUIT_BTN
if (!sample && (buttonState == BTN_STATE_LONG) && !lastDebounceTime) {
buttonState = BTN_STATE_OFF;
}
if ((buttonState == BTN_STATE_OFF) ||
((buttonState == BTN_STATE_SHORT) && lastDebounceTime)) {
if (sample) {
if (!lastDebounceTime && (buttonState == BTN_STATE_OFF)) {
lastDebounceTime = millis();
}
delta = millis() - lastDebounceTime;
if (buttonState == BTN_STATE_OFF) {
if (delta >= BTN_PRESS_SHORT) {
buttonState = BTN_STATE_SHORT;
}
}
else if (buttonState == BTN_STATE_SHORT) {
if (delta >= BTN_PRESS_LONG) {
buttonState = BTN_STATE_LONG;
}
}
}
else { //!sample
lastDebounceTime = 0;
}
}
#ifdef RAPI
else if (sample && vlongDebounceTime && (buttonState == BTN_STATE_LONG)) {
if ((millis() - vlongDebounceTime) >= BTN_PRESS_VERYLONG) {
vlongDebounceTime = 0;
g_ERP.setWifiMode(WIFI_MODE_AP_DEFAULT);
}
}
#endif // RAPI
}
uint8_t Btn::shortPress()
{
if ((buttonState == BTN_STATE_SHORT) && !lastDebounceTime) {
buttonState = BTN_STATE_OFF;
return 1;
}
else {
return 0;
}
}
uint8_t Btn::longPress()
{
if ((buttonState == BTN_STATE_LONG) && lastDebounceTime) {
vlongDebounceTime = lastDebounceTime;
lastDebounceTime = 0;
return 1;
}
else {
return 0;
}
}
Menu::Menu()
{
}
void Menu::init(const char *firstitem)
{
m_CurIdx = 0;
g_OBD.LcdPrint_P(0,m_Title);
g_OBD.LcdPrint(1,firstitem);
}
SettingsMenu::SettingsMenu()
{
m_Title = g_psSettings;
m_menuCnt = 0;
while (g_SettingsMenuList[m_menuCnt]) {
m_menuCnt++;
}
}
#if defined(CHARGE_LIMIT)||defined(TIME_LIMIT)
void SettingsMenu::CheckSkipLimits()
{
// only allow Charge Limit menu item if car connected and no error
m_skipLimits = ((g_EvseController.GetState() == EVSE_STATE_B) || (g_EvseController.GetState() == EVSE_STATE_C)) ? 0 : 1;
}
#endif // CHARGE_LIMIT
void SettingsMenu::Init()
{
m_CurIdx = 0;
#if defined(CHARGE_LIMIT)||defined(TIME_LIMIT)
while (m_skipLimits && (
#if defined(CHARGE_LIMIT)
(g_SettingsMenuList[m_CurIdx] == &g_ChargeLimitMenu) ||
#endif
#if defined(TIME_LIMIT)
(g_SettingsMenuList[m_CurIdx] == &g_TimeLimitMenu)
#else
0
#endif
)) {
m_CurIdx++;
}
#endif // CHARGE_LIMIT || TIME_LIMIT
g_OBD.LcdPrint_P(0,m_Title);
g_OBD.LcdPrint_P(1,g_SettingsMenuList[m_CurIdx]->m_Title);
}
void SettingsMenu::Next()
{
if (++m_CurIdx > m_menuCnt) {
m_CurIdx = 0;
}
#if defined(CHARGE_LIMIT)||defined(TIME_LIMIT)
while (m_skipLimits && (
#if defined(CHARGE_LIMIT)
(g_SettingsMenuList[m_CurIdx] == &g_ChargeLimitMenu) ||
#endif
#if defined(TIME_LIMIT)
(g_SettingsMenuList[m_CurIdx] == &g_TimeLimitMenu)
#else
0
#endif
)) {
m_CurIdx++;
}
#endif // CHARGE_LIMIT || TIME_LIMIT
const char PROGMEM *title;
if (m_CurIdx < m_menuCnt) {
title = g_SettingsMenuList[m_CurIdx]->m_Title;
}
else {
title = g_psExit;
}
g_OBD.LcdPrint_P(1,title);
}
Menu *SettingsMenu::Select()
{
if (m_CurIdx < m_menuCnt) {
return g_SettingsMenuList[m_CurIdx];
}
else {
g_OBD.LcdClear();
return NULL;
}
}
SetupMenu::SetupMenu()
{
m_Title = g_psSetup;
m_menuCnt = 0;
while (g_SetupMenuList[m_menuCnt]) {
m_menuCnt++;
}
}
void SetupMenu::Init()