diff --git a/client/.gitignore b/client/.gitignore new file mode 100644 index 0000000..6249363 --- /dev/null +++ b/client/.gitignore @@ -0,0 +1 @@ +wifi_credentials.h diff --git a/client/animations/spinny.gif b/client/animations/spinny.gif new file mode 100644 index 0000000..d85e1f7 Binary files /dev/null and b/client/animations/spinny.gif differ diff --git a/client/bitmaps.h b/client/bitmaps.h new file mode 100644 index 0000000..9bbffa9 --- /dev/null +++ b/client/bitmaps.h @@ -0,0 +1,83 @@ +static const uint16_t PROGMEM spinny_dims[] = {8, 8, 8}; +static const uint8_t PROGMEM spinny[][8] = { + { + 0x38, + 0x04, + 0x92, + 0x92, + 0x92, + 0x40, + 0x38, + 0x00, + }, + { + 0x18, + 0x44, + 0x92, + 0x92, + 0x92, + 0x44, + 0x30, + 0x00, + }, + { + 0x28, + 0x44, + 0x8a, + 0x92, + 0xa2, + 0x44, + 0x28, + 0x00, + }, + { + 0x30, + 0x44, + 0x8a, + 0x92, + 0xa2, + 0x44, + 0x18, + 0x00, + }, + { + 0x38, + 0x40, + 0x82, + 0xba, + 0x82, + 0x04, + 0x38, + 0x00, + }, + { + 0x38, + 0x44, + 0x80, + 0xba, + 0x02, + 0x44, + 0x38, + 0x00, + }, + { + 0x38, + 0x44, + 0xa2, + 0x10, + 0x8a, + 0x44, + 0x38, + 0x00, + }, + { + 0x38, + 0x44, + 0x22, + 0x92, + 0x88, + 0x44, + 0x38, + 0x00, + }, +}; diff --git a/client/client.ino b/client/client.ino new file mode 100644 index 0000000..29b2cfb --- /dev/null +++ b/client/client.ino @@ -0,0 +1,137 @@ +// Screen libraries etc. +#include +#include +#include +#include +#include +#include +#include + +#include "bitmaps.h" +#include "wifi_credentials.h" + +// Screen stuff +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 64 // OLED display height, in pixels +#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) +#define SCREEN_ADDRESS 0x3C + +// Button stuff +#define BUTTON_PIN D6 + +#define FRAME_DELAY 80 + +// Peripherals + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +// App state + +enum { + CONNECTING, + SHOWING_DEPARTURES +} state = CONNECTING; +int frame = 0; +int buttonPushed = false; + +void ICACHE_RAM_ATTR onButtonFalling() { + buttonPushed = true; +} + +void setup() { + Serial.begin(9600); + + // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally + if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { + Serial.println("SSD1306 allocation failed"); + for(;;); // Don't proceed, loop forever + } + Serial.println("SSD1306 initialized"); + display.clearDisplay(); + + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + + pinMode(BUTTON_PIN, INPUT_PULLUP); + attachInterrupt(BUTTON_PIN, onButtonFalling, FALLING); + + display.setTextColor(SSD1306_WHITE); + display.setTextSize(1); + display.setCursor(0, 0); + display.printf("Connecting to\n%s\n", WIFI_SSID); + display.display(); + /* + display.print(" 5 Mannheim sofort\n" + "26 Kirchheim 9 min\n" + " 5 Weinheim 20:13"); + */ +} + +void drawSpinner (int frame) { + display.fillRect(120, 56, spinny_dims[0], spinny_dims[1], SSD1306_BLACK); + display.drawBitmap(120, 56, spinny[frame % spinny_dims[2]], spinny_dims[0], spinny_dims[1], SSD1306_WHITE); +} + +void loop() { + switch (state) { + case CONNECTING: { + if (WiFi.status() != WL_CONNECTED) { + drawSpinner(frame++); + display.display(); + delay(FRAME_DELAY); + return; + } + + display.clearDisplay(); + display.setCursor(0, 0); + display.print("Connected!\nRequesting departures"); + display.display(); + + std::unique_ptr https(new BearSSL::WiFiClientSecure); + https->setInsecure(); + HTTPClient client; + if (!client.begin(*https, "vrnp.beany.club", 443, "/departures?stop_id=de:08221:1225&platform=A")) { + display.clearDisplay(); + display.setCursor(0, 0); + display.print("begin failed"); + display.display(); + for (;;); + return; + } + + client.addHeader("Authorization", AUTH_TOKEN); + int statusCode = client.GET(); + if (statusCode != 200) { + display.clearDisplay(); + display.setCursor(0, 0); + display.printf("http non-ok: %d\n", statusCode); + display.display(); + for (;;); + return; + } + + display.clearDisplay(); + display.setCursor(0, 0); + display.print(client.getString()); + display.display(); + + state = SHOWING_DEPARTURES; + delay(FRAME_DELAY); + return; + } + + case SHOWING_DEPARTURES: + if (WiFi.status() != WL_CONNECTED) { + state = CONNECTING; + return; + } + + if (buttonPushed) { + buttonPushed = false; + delay(FRAME_DELAY); + return; + } + + delay(FRAME_DELAY); + return; + } +} diff --git a/client/scripts/extract_bitmaps.py b/client/scripts/extract_bitmaps.py new file mode 100755 index 0000000..a99562b --- /dev/null +++ b/client/scripts/extract_bitmaps.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + + +import sys + +from PIL import Image +from pathlib import Path + +def get_bit(im, x, y): + px = im.getpixel((x, y)) + + if px == 0 or px == (0, 0, 0, 255): + return 0 + if px == 1 or px == (255, 255, 255, 255): + return 1 + print(x, y) + print(px) + raise + +def generate_bitmap_from_gif(file: Path, fd): + variable_name = file.with_suffix("").name + + im = Image.open(file) + (w, h) = im.size + + assert w % 8 == 0 + + fd.write(f"static const uint16_t PROGMEM {variable_name}_dims[] = {{{w}, {h}, {im.n_frames}}};\n") + fd.write(f"static const uint8_t PROGMEM {variable_name}[][{w * h // 8}] = {{\n") + for i in range(im.n_frames): + im.seek(i) + fd.write(" {\n") + for y in range(h): + line = [] + for x in range(0, w, 8): + byte_str = "".join(str(get_bit(im, x + i, y)) for i in range(8)) + byte = int(byte_str, 2) + line.append(f"0x{byte:02x}") + line_str = ", ".join(line) + fd.write(f" {line_str},\n") + fd.write(" },\n") + fd.write("};\n") + +if __name__ == "__main__": + generate_bitmap_from_gif(Path("./animations/spinny.gif"), sys.stdout)