#include <Arduino.h> //knihovna pro programování v arduino prostředí (mimo IDE)
#include <U8g2lib.h> //knihovna pro displej
/*#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif*/
#ifdef U8X8_HAVE_HW_I2C //
#include <Wire.h> //zahrnutí knihovny pro použití s OLED displejem
#endif //
unsigned long cas; //čas pro funkci press
const unsigned long deelay = 150; //čas pro debouncing tlačítka ve funkci press
unsigned long cas1; //čas pro funkci press1
const unsigned long deelay1 = 150; //čas pro debouncing tlačítka ve funkci press1
const int but = 27; // GPIO pro tlačítko/potvrzení
volatile int count = 0; //hodnota stidku tlačítka
const int but1 = 26; // GPIO pro tlačítko/přepnutí
volatile int pocet = 0; //hodnota stidku tlačítka
//funkce, kterou vyvolá stisk tlačítka/interrupt
void IRAM_ATTR press() {
if ((millis() - cas) > deelay) {
count++;
//reset proměnných count a pocet, pokud count překročí 1
if(count > 1){
count = 0;
pocet = 0;
}
cas = millis(); //nastevení proměnné čas na millis() pro zamezení bouncingu tlačítka
}
}
//funkce, kterou vyvolá stisk tlačítka/interrupt
void IRAM_ATTR press1() {
if ((millis() - cas1) > deelay1) {
pocet++;
// reset na 0, pokud překročí 5
if (pocet > 5) {
pocet = 0;
}
cas1 = millis(); //nastevení proměnné čas na millis() pro zamezení bouncingu tlačítka
}
}
const int vbat = 34; //analogové měření napětí baterie
int hodnota; //
float napeti;
unsigned long battery;
const unsigned long btime = 3000; // 00;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); // [full framebuffer, size = 1024 bytes] //inicializace displeje
// U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); //inicializace displeje
float velikost_kola = 1; //uložení hodnoty velikosti kola
// Enum for menu state management
enum MenuState { WHEEL_SIZE, DISPLAY_MODE };
MenuState currentMenu = WHEEL_SIZE;
// úvodní obrázek, 128x64px,
const unsigned char bike_pixel [] PROGMEM = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x7f,0x00,0x00,0x00,0x0f,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0xff,0x00,0x00,0x80,
0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,
0xf8,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,
0x80,0xff,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0x00,0xf8,0x9f,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0x80,0xff,0xc1,0x39,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x06,0xf8,0x1f,0xe0,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x86,0xff,0x01,0x70,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xfe,0x1f,0x00,0x38,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xfe,0x01,0x00,0x1c,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xe0,0x1f,0x00,0x1e,0x00,0x00,0x0e,0xc0,0xf8,0x07,0x00,0x00,0x00,
0x00,0x00,0x00,0xfc,0xff,0x00,0x1f,0x00,0x00,0x07,0xc0,0xff,0x3f,0x00,0x00,
0x00,0x00,0x00,0x00,0xff,0xff,0x83,0x1b,0x00,0x80,0x03,0xc0,0xff,0xff,0x00,
0x00,0x00,0x00,0x00,0x80,0x3f,0xf0,0xc7,0x39,0x00,0xc0,0x01,0xe0,0x0f,0xfc,
0x01,0x00,0x00,0x00,0x00,0xc0,0x0f,0xc0,0xef,0x30,0x00,0xe0,0x00,0xf0,0x03,
0xf0,0x03,0x00,0x00,0x00,0x00,0xe0,0x03,0x00,0x7f,0x30,0x00,0x70,0x00,0xf8,
0x07,0xc0,0x07,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x3c,0x70,0x00,0x38,0x00,
0x3c,0x06,0x00,0x0f,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x7c,0x60,0x00,0x1c,
0x00,0x1e,0x0e,0x00,0x1e,0x00,0x00,0x00,0x00,0x3c,0x00,0x00,0xfe,0x60,0x00,
0x0e,0x00,0x0f,0x0c,0x00,0x3c,0x00,0x00,0x00,0x00,0x3c,0x00,0x00,0xf7,0xe0,
0x00,0x07,0x00,0x0f,0x1c,0x00,0x3c,0x00,0x00,0x00,0x00,0x1e,0x00,0x80,0xe3,
0xc1,0x80,0x03,0x80,0x07,0x18,0x00,0x78,0x00,0x00,0x00,0x00,0x1e,0x00,0xc0,
0xe1,0xc1,0xcf,0x01,0x80,0x07,0x38,0x00,0x78,0x00,0x00,0x00,0x00,0x0e,0x80,
0xe7,0xc0,0xc1,0xe3,0x00,0x80,0x03,0x30,0x00,0x70,0x00,0x00,0x00,0x00,0x0f,
0xe0,0x7f,0xc0,0x83,0x73,0x00,0xc0,0x03,0x70,0x00,0xf0,0x00,0x00,0x00,0x00,
0x07,0xf0,0x3f,0x80,0x83,0x3f,0x00,0xc0,0x01,0xe0,0x01,0xe0,0x00,0x00,0x00,
0x00,0x07,0xf0,0x3f,0x80,0xc3,0x1f,0x00,0xc0,0x01,0x70,0x02,0xe0,0x00,0x00,
0x00,0x00,0x07,0xf8,0x7f,0x80,0xe3,0x1f,0x00,0xc0,0x01,0xf0,0x02,0xe0,0x00,
0x00,0x00,0x00,0x07,0xf8,0xff,0xff,0xff,0x1f,0x00,0xc0,0x01,0xd0,0x02,0xe0,
0x00,0x00,0x00,0x00,0x07,0xf8,0xff,0xff,0xff,0x1f,0x00,0xc0,0x01,0x10,0x02,
0xe0,0x00,0x00,0x00,0x00,0x07,0xf8,0x7f,0x80,0xe3,0x1f,0x00,0xc0,0x01,0xe0,
0x01,0xe0,0x00,0x00,0x00,0x00,0x0f,0xf0,0x3f,0xc0,0xc3,0x0f,0x00,0xc0,0x03,
0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x0e,0xf0,0x3f,0xc0,0x81,0x07,0x00,0x80,
0x03,0x00,0x00,0x70,0x00,0x00,0x00,0x00,0x1e,0xe0,0x1f,0xe0,0x01,0x02,0x00,
0x80,0x07,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x1e,0x80,0x07,0xe0,0x01,0x02,
0x00,0x80,0x07,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x3c,0x00,0x00,0xf0,0x00,
0x02,0x00,0x00,0x0f,0x00,0x00,0x3c,0x00,0x00,0x00,0x00,0x3c,0x00,0x00,0xf0,
0x80,0x0f,0x00,0x00,0x0f,0x00,0x00,0x3c,0x00,0x00,0x00,0x00,0x78,0x00,0x00,
0x78,0x00,0x00,0x00,0x00,0x1e,0x00,0x00,0x1e,0x00,0x00,0x00,0x00,0xf0,0x00,
0x00,0x3c,0x00,0x00,0x00,0x00,0x3c,0x00,0x00,0x0f,0x00,0x00,0x00,0x00,0xe0,
0x03,0x00,0x1f,0x00,0x00,0x00,0x00,0xf8,0x00,0xc0,0x07,0x00,0x00,0x00,0x00,
0xc0,0x0f,0xc0,0x0f,0x00,0x00,0x00,0x00,0xf0,0x03,0xf0,0x03,0x00,0x00,0x00,
0x00,0x80,0x3f,0xf0,0x07,0x00,0x00,0x00,0x00,0xe0,0x0f,0xfc,0x01,0x00,0x00,
0x00,0x00,0x00,0xff,0xff,0x03,0x00,0x00,0x00,0x00,0xc0,0xff,0xff,0x00,0x00,
0x00,0x00,0x00,0x00,0xfc,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x3f,0x00,
0x00,0x00,0x00,0x00,0x00,0xe0,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0x07,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x00,0x00,0x04,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x01,0x00,0x04,0x00,0x00,
0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x88,0x38,0x84,0xc3,
0xe1,0xb8,0xf1,0x88,0x3c,0x8e,0x0c,0x00,0x00,0x00,0x00,0x10,0x88,0x44,0x44,
0x24,0x12,0x49,0x12,0x89,0x08,0x91,0x02,0x00,0x00,0x00,0x00,0x10,0x88,0x04,
0x44,0x24,0x10,0x49,0x12,0x89,0x08,0x91,0x01,0x00,0x00,0x00,0x00,0x10,0x88,
0x04,0x44,0x24,0x10,0x49,0x12,0x89,0x08,0x9f,0x00,0x00,0x00,0x00,0x00,0x10,
0x88,0x04,0x44,0x24,0x10,0x49,0x12,0x89,0x08,0x81,0x00,0x00,0x00,0x00,0x00,
0x10,0xf1,0x44,0x44,0x24,0x12,0x09,0x12,0x89,0x08,0x91,0x00,0x00,0x00,0x00,
0x00,0xe0,0x80,0x38,0x88,0xc3,0xe1,0x08,0xf2,0x70,0x30,0x8e,0x00,0x00,0x00,
0x00,0x00,0x00,0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00 };
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 592)
const int epd_bitmap_allArray_LEN = 1;
const unsigned char* epd_bitmap_allArray[1] = {
bike_pixel
};
void setup() {
pinMode(but, INPUT_PULLUP);
pinMode(but1, INPUT_PULLUP);
u8g2.begin(27, U8X8_PIN_NONE, U8X8_PIN_NONE, U8X8_PIN_NONE, 26, U8X8_PIN_NONE);
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(but), press, FALLING); //funkce interrupt pro tlačítka
attachInterrupt(digitalPinToInterrupt(but1), press1, FALLING); //funkce interrupt pro tlačítka
//vykreselní úvodního obrázku
u8g2.clearBuffer();
u8g2.drawXBMP( 0, 0, 128, 64, bike_pixel);
u8g2.sendBuffer();
delay(2000);
}
void loop() {
switch (currentMenu) {
case WHEEL_SIZE:
handleWheelSizeMenu();
break;
case DISPLAY_MODE:
handleDisplayModeMenu();
break;
}
}
void handleWheelSizeMenu() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.userInterfaceSelectionList("Wheel size", 1, "16'' - 305 mm\n20'' - 406 mm\n24'' - 507 mm\n26'' - 559 mm\n27,5'' - 584 mm\n29'' - 622 mm");
// Výběr velikosti kola na základě hodnoty `pocet`
if (count >= 1) {
switch (pocet) {
case 0:
velikost_kola = 0.305;
break;
case 1:
velikost_kola = 0.406;
break;
case 2:
velikost_kola = 0.507;
break;
case 3:
velikost_kola = 0.559;
break;
case 4:
velikost_kola = 0.584;
break;
case 5:
velikost_kola = 0.622;
break;
}
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2. drawStr(5, 15, "Wheel size:");
u8g2.setCursor(50, 30);
u8g2.print(velikost_kola, 3);
u8g2.sendBuffer();
delay(500);
count = 0; // Reset count after selection
pocet = 0; // Reset pocet after selection
currentMenu = DISPLAY_MODE; // Switch to the display mode menu
}
// Debug výstup pro sledování stavu
Serial.println("Velikost kola:");
Serial.println(velikost_kola, 3);
u8g2.sendBuffer();
delay(100);
}
void handleDisplayModeMenu() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.userInterfaceSelectionList("Display mode:", 1, "Speed\nDistance\nGPS status & Battery\nTime\nAll\nWheel size");
// Handle button presses for display mode selection
if (count >= 1) {
Serial.println("Pocet:");
Serial.println(pocet);
switch (pocet) {
case 0:
rychlost();
break;
case 1:
vzdalenost();
break;
case 2:
gpsbaterka();
break;
case 3:
casik();
break;
case 4:
vsechno();
break;
case 5:
currentMenu = WHEEL_SIZE;
//velikostznova();
break;
}
count = 0; // Reset count after selection
pocet = 0; // Reset pocet after selection
//currentMenu = WHEEL_SIZE; // Optionally switch back to the wheel size menu
}
u8g2.sendBuffer();
delay(100);
}
void rychlost() {
while (digitalRead(but)) {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(5, 30, "rychlost");
u8g2.sendBuffer();
}
}
void vzdalenost() {
while (digitalRead(but)) {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(5, 30, "vzdalenost");
u8g2.sendBuffer();
}
}
void gpsbaterka() {
while (digitalRead(but)) {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(5, 30, "gpsbaterka");
u8g2.sendBuffer();
}
}
void casik() {
while (digitalRead(but)) {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(5, 30, "cas");
u8g2.sendBuffer();
}
}
void vsechno() {
while (digitalRead(but)) {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(5, 30, "vsechno");
u8g2.sendBuffer();
}
}
int velikostznova() {
currentMenu = WHEEL_SIZE; // Reset the wheel size
}