#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
#include <ctype.h>
//U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/13, /* dc=*/16, /* reset=*/17);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* clock=*/22, /* data=*/21, /* reset=*/U8X8_PIN_NONE);
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
char serialTitle[numChars] = { 0 };
char serialValue[numChars] = { 0 };
int serialIcon = 0;
int serialIconPosition = 0;
boolean newData = false;
void setup() {
Serial.begin(115200);
u8g2.begin();
u8g2.setContrast(1);
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_profont12_tf);
u8g2.setCursor(0, 25);
u8g2.print("Startujemy...");
u8g2.sendBuffer();
}
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0';
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
char *trim(char *str) {
char *end;
while (isspace((unsigned char)*str)) {
str++;
}
if (*str == 0) {
return str;
}
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
void parseData() {
char *strtokIndx;
strtokIndx = strtok(tempChars, "|");
strcpy(serialTitle, trim(strtokIndx));
strtokIndx = strtok(NULL, "|");
strcpy(serialValue, trim(strtokIndx));
strtokIndx = strtok(NULL, "|");
serialIcon = atoi(strtokIndx);
strtokIndx = strtok(NULL, "|");
serialIconPosition = atoi(strtokIndx);
// atoi jak integer, atof jesli float
}
void showParsedData() {
if (serialTitle == "Settings")
{
u8g2.setContrast(0x2B);
}
else
{
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_profont10_tf);
u8g2.drawStr(0, 10, serialTitle);
u8g2.setFont(u8g2_font_profont29_tf);
u8g2.drawUTF8(0, 40, serialValue);
u8g2.setFont(u8g2_font_open_iconic_all_2x_t);
// u8g2.drawGlyph(0, 64, 65);
u8g2.drawGlyph(serialIconPosition, 64, serialIcon);
u8g2.sendBuffer();
}
}
/*
<Title | Value | Icon | IconPosition>
<Temperatura | 12.34°C | 65 | 0>
<Cisnienie | 1024hPa | 141 | 15>
<Wilgotnosc | 65% | 152 | 30>
<Settings | contrast | 0 | 0>
<Settings | contrast | 64 | 0>
<Settings | contrast | 128 | 0>
<Settings | contrast | 255 | 0>
*/