Skip to content

Commit c261d77

Browse files
committed
improve led management
1 parent 0c9e5ee commit c261d77

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

gps-version/MkrWan-TTN-Mapper/MkrWan-TTN-Mapper.ino

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,35 @@ unsigned long nextCommunication;
2020
unsigned long nextConfirmation;
2121
int err_count;
2222

23-
void setColor(int r,int g,int b) {
24-
digitalWrite(LBLUE,b);
25-
digitalWrite(LGREEN,g);
26-
digitalWrite(LRED,r);
23+
#define COLOR_BLACK 0x000000
24+
#define COLOR_BLUE 0x0000FF
25+
#define COLOR_RED 0xFF0000
26+
#define COLOR_GREEN 0x00FF00
27+
28+
29+
uint32_t previousLedValue;
30+
void setColor(uint32_t color, boolean save) {
31+
int r = (color >> 16) & 0xff;
32+
int g = (color >> 8) & 0xff;
33+
int b = (color ) & 0xff;
34+
35+
if ( r == 0xff ) digitalWrite(LRED,LOW);
36+
else if ( r == 0x00 ) digitalWrite(LRED,HIGH);
37+
else analogWrite(LRED,256-r);
38+
39+
if ( g == 0xff ) digitalWrite(LGREEN,LOW);
40+
else if ( g == 0x00 ) digitalWrite(LGREEN,HIGH);
41+
else analogWrite(LGREEN,256-g);
42+
43+
if ( b == 0xff ) digitalWrite(LBLUE,LOW);
44+
else if ( b == 0x00 ) digitalWrite(LBLUE,HIGH);
45+
else analogWrite(LBLUE,256-b);
46+
47+
if ( save ) previousLedValue=color;
48+
}
49+
50+
void restoreColor() {
51+
setColor(previousLedValue,false);
2752
}
2853

2954
void setup() {
@@ -47,7 +72,7 @@ void setup() {
4772
pinMode(LGREEN,OUTPUT);
4873
pinMode(LRED,OUTPUT);
4974
pinMode(BOARDLED,OUTPUT);
50-
setColor(HIGH,HIGH,LOW);
75+
setColor(COLOR_BLUE,true);
5176
Serial.println("Go on!");
5277
}
5378

@@ -81,19 +106,20 @@ void loop() {
81106
err_count=0;
82107
nextCommunication=millis()+10000; // + 10s
83108
nextConfirmation=millis()+10000;
109+
setColor(COLOR_GREEN,true);
84110
} else {
85-
setColor(LOW,HIGH,HIGH);
111+
setColor(COLOR_RED,true);
86112
}
87113
}
88114
if ( ! gps.location.isValid() || !gps.altitude.isValid() ) {
89115
// Wait for GPS to be positionned
90-
setColor(HIGH,HIGH,HIGH);
116+
setColor(COLOR_BLACK,false);
91117
delayWithGps(1000);
92118
for ( int i = 0 ; i < gps.satellites.value()+1 ; i++ ) {
93-
setColor(HIGH,HIGH,HIGH);
119+
setColor(COLOR_BLACK,false);
94120
delayWithGps(150);
95-
setColor(HIGH,(connected)?LOW:HIGH,(connected)?HIGH:LOW);
96-
delayWithGps(150);
121+
restoreColor();
122+
delayWithGps(100);
97123
}
98124
} else {
99125
// GPS Position OK
@@ -110,7 +136,6 @@ void loop() {
110136
} else {
111137
Serial.print("lat: ");Serial.print(lat);
112138
Serial.print(", lng: ");Serial.print(lng);
113-
Serial.print(", alt_src: ");Serial.print(gps.altitude.value());
114139
Serial.print(", alt: ");Serial.print(altitude);
115140
Serial.print(", hdop:");Serial.print(hdop);
116141
Serial.print(", sat:");Serial.println(gps.satellites.value());
@@ -120,27 +145,32 @@ void loop() {
120145
msg[4]=(lat>>16) & 0xFF; msg[5]=(lat>>8) & 0xFF;msg[6]=(lat) & 0xFF;
121146
msg[7]=(lng>>16) & 0xFF; msg[8]=(lng>>8) & 0xFF;msg[9]=(lng) & 0xFF;
122147
}
123-
setColor(HIGH,HIGH,HIGH);
148+
setColor(COLOR_BLACK,false);
124149
modem.beginPacket();
125150
modem.write(msg,10);
126-
int err = modem.endPacket((nextConfirmation < millis()));
151+
boolean toBeConfirmed = (nextConfirmation < millis());
152+
int err = modem.endPacket(toBeConfirmed);
127153
if ( err <= 0 ) {
128-
setColor(LOW,HIGH,HIGH);
154+
// Should only be here when in confirmation mode
155+
// with an error
156+
setColor(COLOR_RED,true);
129157
err_count++;
130158
if ( err_count > 50 ) {
131159
connected = false;
132-
setColor(HIGH,HIGH,LOW);
160+
setColor(COLOR_BLUE,true);
133161
}
134162
nextCommunication=millis()+20000L; // wait for 20 seconds
135-
nextConfirmation=millis()+180000L; // wait for 8 minutes - do not want to spam in SF12
163+
nextConfirmation=millis()+300000L; // wait for 5 minutes - do not want to spam in SF12
136164
} else {
137-
delay(50);
138-
setColor(HIGH,LOW,HIGH);
139-
err_count = 0;
140-
nextCommunication=millis()+10000L; // wait for 10 seconds.
141-
if ( nextConfirmation < millis() ) { // confirmation every 30 seconds
142-
nextConfirmation = millis()+30000L;
165+
delayWithGps(200);
166+
if (toBeConfirmed) {
167+
err_count = 0;
168+
setColor(COLOR_GREEN,true);
169+
nextConfirmation = millis()+30000L; // Next confirmation in 30 seconds
170+
} else {
171+
restoreColor();
143172
}
173+
nextCommunication=millis()+10000L; // wait for 10 seconds.
144174
}
145175
}
146176
}

0 commit comments

Comments
 (0)