/*
Tiny4kOLED - Drivers for SSD1306 controlled dot matrix OLED/PLED 128x32 displays
Based on ssd1306xled, re-written and extended by Stephen Denne
from 2017-04-25 at https://github.com/datacute/Tiny4kOLED
*/
// Choose your I2C implementation before including Tiny4kOLED.h
// The default is selected is Wire.h
// To use the Wire library:
//#include <Wire.h>
// To use the Adafruit's TinyWireM library:
#include <TinyWireM.h>
// To use the TinyI2C library from https://github.com/technoblogy/tiny-i2c
//#include <TinyI2CMaster.h>
// The blue OLED screen requires a long initialization on power on.
// The code to wait for it to be ready uses 20 bytes of program storage space
// If you are using a white OLED, this can be reclaimed by uncommenting
// the following line (before including Tiny4kOLED.h):
//#define TINY4KOLED_QUICK_BEGIN
#define BUTTON_UP_PIN PB4 // pin for UP button
#define BUTTON_DOWN_PIN PB3 // pin for DOWN button
#include <Tiny4kOLED.h>
#include "fontpixelop16.h"
#include "fontpixelop16b.h"
#include "bg.h"
// ============================================================================
const int NUM_ITEMS = 8; // number of items in the list and also the number of screenshots and screenshots with QR codes (other screens)
const int MAX_ITEM_LENGTH = 20; // maximum characters for the item name
char menu_items [NUM_ITEMS] [MAX_ITEM_LENGTH] = { // array with item names
{ "3D Cube" },
{ "Battery" },
{ "Dashboard" },
{ "Fireworks" },
{ "GPS Speed" },
{ "Big Knob" },
{ "Park Sensor" },
{ "Turbo Gauge" }
};
int item_sel_previous; // previous item - used in the menu screen to draw the item before the selected one
int item_selected = 0; // which item in the menu is selected
int item_sel_next; // next item - used in the menu screen to draw next item after the selected one
void setup() {
pinMode(BUTTON_UP_PIN, INPUT_PULLUP); // up button
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP); // down button
oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
//oled.clear();
oled.bitmap(0, 0, 128, 8, epd_bitmap_bg_no_labels);
oled.on();
}
void fill_rest_of_line() {
int stored_cursor_x = oled.getCursorX();
int stored_cursor_y = oled.getCursorY();
int clear_width = 110 - stored_cursor_x;
oled.fillLength(/*B10101010*/0, clear_width);
oled.setCursor(stored_cursor_x, stored_cursor_y +1);
oled.fillLength(/*B10101010*/0, clear_width);
}
void loop() {
// set correct values for the previous and next items
item_sel_previous = item_selected - 1;
if (item_sel_previous < 0) {item_sel_previous = NUM_ITEMS - 1;} // previous item would be below first = make it the last
item_sel_next = item_selected + 1;
if (item_sel_next >= NUM_ITEMS) {item_sel_next = 0;} // next item would be after last = make it the first
oled.setFont(FONTPIXELOP16);
oled.setCursor(25, 0);
oled.print(menu_items[item_sel_previous]);
fill_rest_of_line();
oled.setFont(FONTPIXELOP16B);
oled.setCursor(25, 3);
oled.print(menu_items[item_selected]);
fill_rest_of_line();
oled.setFont(FONTPIXELOP16);
oled.setCursor(25, 6);
oled.print(menu_items[item_sel_next]);
fill_rest_of_line();
if(digitalRead(BUTTON_UP_PIN) == LOW) {// button UP is pressed
item_selected = (item_selected - 1 + NUM_ITEMS) % NUM_ITEMS;
}
if(digitalRead(BUTTON_DOWN_PIN) == LOW) {// button DOWN is pressed
item_selected = (item_selected + 1) % NUM_ITEMS;
}
}