/*
MIT License
Copyright (c) 2022-2024, Adolfo Gutiérrez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "PodOS.h"
#include "pixChicago4pt7b.h"
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BTN_UP 11
#define BTN_DOWN 10
int16_t SETUP_HEIGHT_DISPLAY;
int16_t MARGIN_HEIGHT = 16;
int16_t HEIGHT = 0;
#define MAX_ITEMS_ON_DISPLAY (SETUP_HEIGHT_DISPLAY / MARGIN_HEIGHT)
int16_t MENU_SELECTED = 0;
int16_t MENU_ON_TOP = 0;
int16_t MENU_ROW_POS = 0;
int16_t MENU_ROW_POS_PREV = -1;
typedef enum {
MENU,
MUSIC,
EXTRAS,
SETTINGS
} Menu;
Menu mn;
int lastState = HIGH;
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
SETUP_HEIGHT_DISPLAY = SCREEN_HEIGHT;
display.setFont(&pixChicago4pt7b);
display.clearDisplay();
display.drawBitmap(0, 0, bitmap_splash_screen, 128, 64, SSD1306_WHITE);
display.display();
delay(3000);
display.clearDisplay();
display.invertDisplay(false);
mn = MENU;
}
void loop() {
setStatusBar();
switch (mn) {
case MENU: getMenu(); break;
case MUSIC: getMusic(); break;
case EXTRAS: getExtras(); break;
case SETTINGS: getSettings(); break;
}
display.display();
}
void getMenu() {
int16_t t = MAX_ITEMS_ON_DISPLAY;
if (MENU_SIZE < t) {
t = MENU_SIZE;
}
for (int16_t i = 0; i < t; i++) {
if (i < t -1) {
addMenu(i, menu[i + MENU_ON_TOP], false, true);
} else {
addMenu(i, menu[i + MENU_ON_TOP], false, true);
}
}
menuSelect(MENU_ROW_POS, -1, menu);
int valueUp = digitalRead((BTN_UP));
if (lastState != valueUp) {
lastState = valueUp;
if (valueUp == LOW) {
Serial.println(" pressed up");
selectPos(true, MENU_SIZE, menu);
}
}
int valueDown = digitalRead((BTN_DOWN));
if (lastState != valueDown) {
lastState = valueDown;
if (valueDown == LOW) {
Serial.println(" pressed down");
selectPos(false, MENU_SIZE, menu);
}
}
}
void getMusic() {
int16_t t = MAX_ITEMS_ON_DISPLAY;
if (MUSIC_SIZE < t) {
t = MUSIC_SIZE;
}
for (int16_t i = 0; i < t; i++) {
if (i < t -1) {
addMenu(i, music[i + MENU_ON_TOP], false, true);
} else {
addMenu(i, music[i + MENU_ON_TOP], false, true);
}
}
menuSelect(MENU_ROW_POS, -1, music);
}
void getExtras() {}
void getSettings() {}
void setStatusBar() {
switch (mn) {
case MENU: setTextHorizontal("PodOS"); break;
case MUSIC: setTextHorizontal(menu[MENU_SELECTED]); break;
case EXTRAS: setTextHorizontal(menu[MENU_SELECTED]); break;
case SETTINGS: setTextHorizontal(menu[MENU_SELECTED]); break;
}
display.drawBitmap(0, 14, bitmap_statusbar_line, 128, 1, SSD1306_WHITE);
SETUP_HEIGHT_DISPLAY = 48;
HEIGHT = MARGIN_HEIGHT;
}
void menuSelect(int16_t actualPos, int16_t prevPos, char *menuList[]) {
addMenu(actualPos, menuList[MENU_ON_TOP + actualPos], true, true);
if (prevPos > -1) {
addMenu(prevPos, menuList[MENU_ON_TOP + prevPos], false, true);
}
}
void addMenu(int16_t positionY, char *name, bool isSelect, bool hasOptions) {
int16_t multiPosY = positionY * MARGIN_HEIGHT + HEIGHT;
if (isSelect) {
display.drawBitmap(0, multiPosY, bitmap_bar_select, 128, 16, SSD1306_WHITE);
setText(3, 2 + multiPosY, name, SSD1306_BLACK);
if (hasOptions) {
display.drawBitmap(116, multiPosY, bitmap_ic_arrow_right, 12, 16, SSD1306_BLACK);
}
} else {
display.drawBitmap(0, multiPosY, bitmap_bar_select, 128, 16, SSD1306_BLACK);
setText(3, 2 + multiPosY, name, SSD1306_WHITE);
if (hasOptions) {
display.drawBitmap(116, multiPosY, bitmap_ic_arrow_right, 12, 16, SSD1306_WHITE);
}
}
}
void selectPos(bool increment, byte size, char *menuList[]) {
if (increment) {
MENU_ROW_POS_PREV = MENU_ROW_POS;
if (MENU_ROW_POS == 0) {
if (MENU_SELECTED != 0) {
MENU_SELECTED--;
MENU_ON_TOP = MENU_SELECTED;
menuSelect(MENU_ROW_POS, -1, menuList);
}
} else {
MENU_ROW_POS--;
if (MENU_SELECTED > 0) {
MENU_SELECTED--;
}
menuSelect(MENU_ROW_POS, MENU_ROW_POS_PREV, menuList);
}
} else {
MENU_ROW_POS_PREV = MENU_ROW_POS;
uint16_t lastRow = 0;
if (size < MAX_ITEMS_ON_DISPLAY) {
lastRow = size - 1;
} else {
lastRow = MAX_ITEMS_ON_DISPLAY - 1;
}
if (MENU_ROW_POS == lastRow) {
if (MENU_SELECTED < size - 1) {
MENU_SELECTED++;
MENU_ON_TOP = MENU_SELECTED - lastRow;
menuSelect(MENU_ROW_POS, -1, menuList);
}
} else {
MENU_ROW_POS++;
if (MENU_SELECTED < size - 1) {
MENU_SELECTED++;
}
menuSelect(MENU_ROW_POS, MENU_ROW_POS_PREV, menuList);
}
}
}
void setText(int16_t x, int16_t y, char *text, uint16_t color) {
display.setCursor(x, y);
display.setTextColor(color);
display.println(text);
}
void setTextHorizontal(char *text) {
int16_t x;
uint16_t width;
display.getTextBounds(text, 0, 0, &x, 0, &width, 0);
display.setCursor((SCREEN_WIDTH - width) / 2, 1);
display.setTextColor(SSD1306_WHITE);
display.println(text);
}