/*********
Complete project details at https://randomnerdtutorials.com
This is an example for our Monochrome OLEDs based on SSD1306 drivers. Pick one up today in the adafruit shop! ------> http://www.adafruit.com/category/63_98
This example is for a 128x32 pixel display using I2C to communicate 3 pins are required to interface (two I2C and one reset).
Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries, with contributions from the open source community. BSD license, check license.txt for more information All text above, and the splash screen below must be included in any redistribution.
*********/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define POTPIN 34
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
class OledMenu {
public:
int textSize;
int padding;
int lineHeight;
int currentSelect = 0;
OledMenu(int a, int b) {
textSize = a;
padding = b;
lineHeight = (a*8) + b*2;
}
void printMenuItems(String menuItems[]) {
display.clearDisplay();
display.setTextSize(textSize);
display.setTextColor(WHITE);
for (int i = 0; !menuItems[i + 1].isEmpty(); i++) {
display.setCursor(padding*2, i*lineHeight + padding);
display.println(menuItems[i]);
}
display.display();
toggleHighlightMenuItem(currentSelect);
}
void nextCurrentSelect(int menuItemCount) {
currentSelect = (currentSelect + 1) % menuItemCount;
}
void toggleHighlightMenuItem(int n) {
display.fillRoundRect(0, n*lineHeight, display.width(), lineHeight, lineHeight/5, INVERSE);
display.display();
}
void highlightNextMenuItem(int menuItemCount) {
display.fillRoundRect(0, currentSelect*lineHeight, display.width(), lineHeight, lineHeight/5, INVERSE);
nextCurrentSelect(menuItemCount);
display.fillRoundRect(0, currentSelect*lineHeight, display.width(), lineHeight, lineHeight/5, INVERSE);
display.display();
}
void highlightMenuItem(int menuItem) {
if (menuItem != currentSelect) {
display.fillRoundRect(0, currentSelect*lineHeight, display.width(), lineHeight, lineHeight/5, INVERSE);
currentSelect = menuItem;
display.fillRoundRect(0, currentSelect*lineHeight, display.width(), lineHeight, lineHeight/5, INVERSE);
display.display();
}
}
};
OledMenu newMenu(1, 2);
void setup() {
Serial.begin(115200);
pinMode(2, INPUT_PULLUP);
pinMode(34, INPUT);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.setRotation(1);
display.display();
delay(300);
display.clearDisplay();
String menuItems[] = { "Hello", "there", "general", "kenobi" };
newMenu.printMenuItems( menuItems );
}
void loop() {
newMenu.highlightMenuItem(getPotPart(3));
delay(50);
}
int getPotPart(int parts) {
return getPart(analogRead(POTPIN), 4096, parts);
}
int getPart(int val, int size, int parts) {
return val / ((size / parts) + 1);
}
void printMenuItems(String items[]) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
for (int i = 0; !items[i + 1].isEmpty(); i++) {
display.setCursor(2, i*10 + 1);
display.println(items[i]);
}
display.display();
}