Added filtering & cleaned up code
This commit is contained in:
parent
e448385949
commit
8fba6818ca
|
@ -6,8 +6,6 @@
|
|||
#include <time.h>
|
||||
#include "Ubuntu_24px.h"
|
||||
|
||||
//M5GFX display;
|
||||
//M5Canvas canvas(&display);
|
||||
M5_LoRaWAN LoRaWAN;
|
||||
|
||||
String response;
|
||||
|
@ -16,17 +14,10 @@ void setup() {
|
|||
M5.begin();
|
||||
LoRaWAN.Init(&Serial2, 13, 14);
|
||||
delay(100);
|
||||
//display.begin();
|
||||
//display.setTextSize(1);
|
||||
//canvas.setColorDepth(1); // mono color
|
||||
//canvas.createSprite(display.width(), display.height());
|
||||
//canvas.setTextSize((float)canvas.width() / 160);
|
||||
//canvas.setTextScroll(true);
|
||||
M5.Lcd.begin();
|
||||
M5.Lcd.setTextSize(1);
|
||||
M5.Lcd.setFreeFont(&Ubuntu_24px);
|
||||
|
||||
//display.println("Module Connect.....");
|
||||
M5.Lcd.drawString("Checking LoraWAN module...", 0, 0);
|
||||
while (!LoRaWAN.checkDeviceConnect())
|
||||
;
|
||||
|
@ -73,6 +64,8 @@ void setup() {
|
|||
LoRaWAN.startJoin();
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
|
||||
String decodeMsg(String hexEncoded) {
|
||||
if ((hexEncoded.length() & 1) == 0) {
|
||||
char buf[hexEncoded.length() + 1];
|
||||
|
@ -105,6 +98,15 @@ String split(String data, char separator, int index) {
|
|||
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
|
||||
}
|
||||
|
||||
bool stringInArray(String val, String* arr, int length){
|
||||
for(int i = 0; i < length; i++){
|
||||
if(arr[i] == val) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Departure stucture
|
||||
|
||||
struct departure {
|
||||
int id;
|
||||
String line;
|
||||
|
@ -112,9 +114,13 @@ struct departure {
|
|||
int departure;
|
||||
};
|
||||
|
||||
// Global departure list
|
||||
|
||||
struct departure departures[20];
|
||||
int departureIndex = 0;
|
||||
|
||||
// Departure functions
|
||||
|
||||
struct departure parseDeparture(String s) {
|
||||
struct departure d;
|
||||
d.id = split(s, '|', 0).toInt();
|
||||
|
@ -133,9 +139,9 @@ void displayDeparture(struct departure d, int y) {
|
|||
M5.Lcd.drawString(departure, 290, y*30);
|
||||
}
|
||||
|
||||
int departureComparation(const void *a,const void *b) {
|
||||
departure *x = (departure *) a;
|
||||
departure *y = (departure *) b;
|
||||
int departureComparation(const void* a,const void* b) {
|
||||
departure* x = (departure*) a;
|
||||
departure* y = (departure*) b;
|
||||
return x->departure - y->departure;
|
||||
}
|
||||
|
||||
|
@ -166,10 +172,27 @@ void addDeparture(String s) {
|
|||
sortDepartures();
|
||||
}
|
||||
|
||||
departure* getDepartures() {
|
||||
return departures;
|
||||
void displayDepartures(int lines) {
|
||||
String displayed[20];
|
||||
int displayedIndex = 0;
|
||||
for(int i=0; i < departureIndex && displayedIndex < lines; i++) {
|
||||
String lineLastStop = departures[i].line + departures[i].last_stop;
|
||||
departures[i].departure -= 1;
|
||||
if(!stringInArray(lineLastStop, displayed, displayedIndex)) {
|
||||
displayed[displayedIndex] = lineLastStop;
|
||||
M5.Lcd.fillRect(0, displayedIndex*30, 320, 30, BLACK);
|
||||
displayDeparture(departures[i], displayedIndex);
|
||||
displayedIndex++;
|
||||
}
|
||||
if(departures[i].departure < -3) removeDeparture(i);
|
||||
}
|
||||
for(int i=displayedIndex; i<lines; i++) {
|
||||
M5.Lcd.fillRect(0, i*30, 320, 30, BLACK);
|
||||
}
|
||||
}
|
||||
|
||||
// Main loop with time counting
|
||||
|
||||
time_t seconds;
|
||||
int last_update = 0;
|
||||
|
||||
|
@ -183,19 +206,9 @@ void loop() {
|
|||
seconds = time(NULL);
|
||||
if(seconds / 6 > last_update) {
|
||||
last_update++;
|
||||
//canvas.fillSprite(TFT_BLACK);
|
||||
departure *results = getDepartures();
|
||||
int lines = 7;
|
||||
for(int i=0; i<=lines; i++) {
|
||||
M5.Lcd.fillRect(0, i*30, 320, 30, BLACK);
|
||||
if(i < departureIndex) {
|
||||
displayDeparture(results[i], i);
|
||||
}
|
||||
if(i == lines) {
|
||||
M5.Lcd.drawString(String(last_update), 0, i*30);
|
||||
}
|
||||
}
|
||||
int lines = 8;
|
||||
displayDepartures(lines);
|
||||
M5.Lcd.drawString(String(last_update), 0, lines*30);
|
||||
}
|
||||
//canvas.pushSprite(0, 0);
|
||||
delay(10);
|
||||
delay(5);
|
||||
}
|
Loading…
Reference in a new issue