#include <Wire.h>
#include <U8g2lib.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#define NL_PS_I_width 16
#define NL_PS_I_height 16
// 'PS-Logo', 43x33px
static const unsigned char NL_PS_Logo [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x8f, 0xf1, 0xf8, 0x3f, 0x00, 0x61, 0x8f, 0xf1, 0xf8,
0x3f, 0x00, 0x79, 0x8c, 0x01, 0x86, 0x30, 0xc0, 0x79, 0x8c, 0x01, 0x86, 0x30, 0xc0, 0x67, 0x8f,
0xf1, 0xf8, 0x30, 0xc0, 0x67, 0x8f, 0xf1, 0xf8, 0x30, 0xc0, 0x61, 0x8c, 0x01, 0x86, 0x30, 0xc0,
0x61, 0x8c, 0x01, 0x86, 0x30, 0xc0, 0x61, 0x8c, 0x01, 0x86, 0x30, 0xc0, 0x61, 0x8c, 0x01, 0x86,
0x30, 0xc0, 0x61, 0x8f, 0xf1, 0x86, 0x3f, 0x00, 0x61, 0x8f, 0xf1, 0x86, 0x3f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x70, 0x00, 0x3e, 0x03, 0xfe, 0x00, 0x70, 0x00, 0x3e, 0x03, 0xff, 0x00, 0x70, 0x01, 0xff, 0xc3,
0xff, 0x80, 0x70, 0x01, 0xc1, 0xc3, 0x83, 0x80, 0x70, 0x01, 0xc1, 0xc3, 0x83, 0x80, 0x70, 0x01,
0xc1, 0xc3, 0xff, 0x00, 0x70, 0x01, 0xc1, 0xc3, 0xfe, 0x00, 0x70, 0x01, 0xc1, 0xc3, 0xff, 0x00,
0x70, 0x01, 0xff, 0xc3, 0x83, 0x80, 0x70, 0x01, 0xff, 0xc3, 0x81, 0xc0, 0x70, 0x01, 0xff, 0xc3,
0x81, 0xc0, 0x70, 0x01, 0xc1, 0xc3, 0x81, 0xc0, 0x70, 0x01, 0xc1, 0xc3, 0x83, 0xc0, 0x7f, 0xe1,
0xc1, 0xc3, 0xff, 0x80, 0x7f, 0xe1, 0xc1, 0xc3, 0xff, 0x00, 0x7f, 0xe1, 0xc1, 0xc3, 0xfe, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'PS-I-01', 16x16px
static unsigned char PS_I_01_bits[] = {
0x00, 0x00, 0xF0, 0x0F, 0x18, 0x18, 0x4C, 0x32, 0x06, 0x60, 0x13, 0xD8,
0x01, 0x8C, 0x05, 0x86, 0x01, 0xA3, 0x81, 0x81, 0x00, 0x00, 0xCC, 0x14,
0x54, 0x1D, 0xCC, 0x14, 0x54, 0x14, 0x00, 0x00, };
// 'PS-I-02', 16x16px
static unsigned char PS_I_02_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x0F, 0x1E, 0x18, 0x1E, 0x10,
0x1E, 0x90, 0x1F, 0xF0, 0x1E, 0x90, 0x1E, 0x10, 0x1E, 0x18, 0xFC, 0x0F,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
// 'PS-I-03', 16x16px
static unsigned char PS_I_03_bits[] = {
0x1C, 0x00, 0x22, 0x00, 0xE3, 0xFF, 0x22, 0x00, 0x1C, 0x00, 0x80, 0x03,
0x40, 0x04, 0x7F, 0xFC, 0x40, 0x04, 0x80, 0x03, 0x00, 0x38, 0x00, 0x44,
0xFF, 0xC7, 0x00, 0x44, 0x00, 0x38, 0x00, 0x00, };
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 144)
static const int NL_allArray_LEN = 3;
static const unsigned char* NL_allArray[3] = {
PS_I_01_bits,
PS_I_02_bits,
PS_I_03_bits
};
// ------------------ end generated bitmaps from image2cpp ---------------------------------
const int NUM_ITEMS = 3; // number of items in the list and also the number of screenshots and screenshots with QR codes (other screens)
const int MAX_ITEM_LENGTH = 15; // maximum characters for the item name
const int UI_NUM_ITEMS = 6; // maximum characters for the item name
char menu_items [NUM_ITEMS] [MAX_ITEM_LENGTH] = { // array with item names
{ "SPEED TEST" },
{ "MOTOR BREAK-IN" },
{ "BREAK-IN SETUP" }
};
char ui_items [UI_NUM_ITEMS] [MAX_ITEM_LENGTH] = { // array with item names
{ "START" },
{ "STOP" },
{ "EXIT" },
{ "PAUSE" },
{ "SAVE" },
{ "RESET" }
};
int selected_item [] = {
1,22,43
};
#define ENCODER_CLK 2 // CLK pin van de rotary encoder
#define ENCODER_DT 3 // DT pin van de rotary encoder
#define BUTTON_SW 4 // SW (push button) pin van de rotary encoder
#define SPEAKER_PIN 8
volatile int encoderPos = 0;
int lastEncoderPos = 0;
unsigned long lastInterruptTime = 0;
unsigned long debounceDelay = 20; // Vertraging tegen debouncing
int current_screen = 0; // 0 = menu, 1 = screenshot, 2 = qr
int set_current_screen;
int item_selected = 0; // which item in the menu is selected
bool testStatus = false;
bool breakinStatus = false;
void setup() {
Serial.begin(9600);
u8g2.begin();
u8g2.setFont(u8g2_font_courB08_tr);
u8g2.setColorIndex(1); // set the color to white
pinMode(ENCODER_CLK, INPUT_PULLUP);
pinMode(ENCODER_DT, INPUT_PULLUP);
pinMode(BUTTON_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), handleEncoder, CHANGE);
}
void loop() {
int buttonState = digitalRead(BUTTON_SW);
int current_screen = set_current_screen;
u8g2.clearBuffer(); // required for page drawing mode for u8g2 library
u8g2.setDrawColor(1);
u8g2.setBitmapMode(0);
u8g2.setFont(u8g2_font_courB08_tr);
Serial.print(set_current_screen);
if (current_screen == 0) { // MENU SCREEN
u8g2.drawXBM(4, 2, NL_PS_I_width, NL_PS_I_width, PS_I_01_bits);
u8g2.setFont(u8g2_font_courB08_tr);
u8g2.drawStr(26,14, menu_items[0]);
u8g2.drawXBM(4, 24, NL_PS_I_width, NL_PS_I_width, PS_I_02_bits);
u8g2.drawStr(26,35, menu_items[1]);
u8g2.drawXBM(4, 46, NL_PS_I_width, NL_PS_I_width, PS_I_03_bits);
u8g2.drawStr(26,56, menu_items[2]);/*
if (buttonState == LOW) {
if (item_selected == 0) {
Serial.println(menu_items[item_selected]);
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
set_current_screen = 1;
}
}*/
if (item_selected < 0 ) {
item_selected = 2;
}
if (item_selected > 2 ) {
item_selected = 0;
}
// selected item Frame
u8g2.drawRFrame(1, selected_item[item_selected], 124, 20,3);
}
else if (current_screen == 1) { // SPEED TEST SCREEN
if (testStatus == false) {
u8g2.drawStr(91,14, ui_items[0]);
}
else {
u8g2.drawStr(91,14, ui_items[1]);
}
u8g2.drawStr(94,35, ui_items[2]);
if (item_selected < 0 ) {
item_selected = 1;
}
if (item_selected > 1 ) {
item_selected = 0;
}
u8g2.drawRFrame(82, selected_item[item_selected], 44, 20,3);
}
else if (current_screen == 2) { // MOTOR BREAK-IN SCREEN
if (breakinStatus == false) {
u8g2.drawStr(91,14, ui_items[0]);
}
else {
u8g2.drawStr(91,14, ui_items[4]);
}
u8g2.drawStr(94,35, ui_items[1]);
u8g2.drawStr(94,35, ui_items[2]);
if (item_selected < 0 ) {
item_selected = 1;
}
if (item_selected > 3 ) {
item_selected = 0;
}
u8g2.drawRFrame(82, selected_item[item_selected], 44, 20,3);
}
else if (current_screen == 3) { // BREAK-IN SETUP SCREEN
u8g2.drawStr(91,14, ui_items[5]);
u8g2.drawStr(91,14, ui_items[6]);
u8g2.drawStr(94,35, ui_items[2]);
if (item_selected < 0 ) {
item_selected = 1;
}
if (item_selected > 3 ) {
item_selected = 0;
}
u8g2.drawRFrame(82, selected_item[item_selected], 44, 20,3);
}
//if (buttonState == LOW) {
submitButton();
// delay(1000);
// }
u8g2.sendBuffer(); // send buffer from RAM to display controller
}
void handleEncoder() {
unsigned long currentMillis = millis();
if (currentMillis - lastInterruptTime > debounceDelay) {
int CLKstate = digitalRead(ENCODER_CLK);
int DTstate = digitalRead(ENCODER_DT);
if (CLKstate == DTstate) {
item_selected--;
} else {
item_selected++;
}
lastInterruptTime = currentMillis;
}
}
void submitButton () {
int buttonState = digitalRead(BUTTON_SW);
if ((current_screen == 0) && (buttonState == LOW)) {
if (item_selected == 0) {
Serial.println(menu_items[item_selected]);
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
current_screen == 1;
delay(100);
}
else if (item_selected == 1) {
Serial.println(menu_items[item_selected]);
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
int current_screen = 2;
delay(100);
}
else if (item_selected == 2) {
Serial.println(menu_items[item_selected]);
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
int current_screen = 3;
delay(100);
}
Serial.println("button hit");
}
/*
else if ((current_screen == 1) && (buttonState == LOW)) { // Speed Test Button
if (item_selected == 0 && testStatus == false) {
Serial.println("Start");
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
testStatus = true;
delay(100);
}
else if (item_selected == 0 && testStatus == true) {
Serial.println("Stop");
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
testStatus = false;
delay(100);
}
else if (item_selected == 1) {
set_current_screen = 0;
Serial.println("Exit to");
Serial.println(set_current_screen);
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
testStatus = false;
delay(100);
}
}
else if ((current_screen == 2) && (buttonState == LOW)) {
if (item_selected == 0 && testStatus == false) {
Serial.println("Start");
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
testStatus = true;
delay(100);
}
if (item_selected == 0 && testStatus == true) {
Serial.println("Stop");
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
testStatus = false;
delay(100);
}
if (item_selected == 2) {
Serial.println(menu_items[item_selected]);
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
current_screen = 0;
testStatus = false;
}
}
else if (current_screen == 3) {
if (item_selected == 0 && testStatus == false) {
Serial.println("Start");
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
testStatus = true;
delay(100);
}
if (item_selected == 0 && testStatus == true) {
Serial.println("Stop");
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
testStatus = false;
delay(100);
}
if (item_selected == 2) {
Serial.println(menu_items[item_selected]);
tone(8, 140, 50); // Plays 262Hz tone for 0.250 seconds
current_screen = 0;
testStatus = false;
}
}
*/
}