#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "rotary_encoder.h"
#include "debounce.h"
#define SCREEN_I2C_ADDR 0x3C // or 0x3C
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RST_PIN -1 // Reset pin (-1 if not available)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST_PIN);
const byte ENCODER_CLK = 2;
const byte ENCODER_DT = 3;
const byte ENCODER_SW = 4;
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_I2C_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
setupRotary(ENCODER_DT, ENCODER_CLK);
pinMode(ENCODER_SW, INPUT_PULLUP);
display.setRotation(1);
updateDisplay();
}
// 'illustration', 44x3px
const unsigned char bm_wire [] PROGMEM = {
0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xd8, 0x00,
0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xb0, 0x00, 0x7f, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xa7, 0xff,
0xff, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xaf, 0xff, 0xff, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xa7, 0xfe,
0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xb0, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0xd8, 0x00,
0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00
};
// 'lead-indicator', 44x3px
const unsigned char bm_lead [] PROGMEM = {
0x80, 0x20, 0xaa, 0xa0, 0x80, 0x20
};
// 'length-indicator', 44x3px
const unsigned char bm_length [] PROGMEM = {
0x80, 0x00, 0x00, 0x00, 0x00, 0x10, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xb0, 0x80, 0x00, 0x00, 0x00,
0x00, 0x10
};
const byte parameter_length = 5;
const char *parameters[parameter_length] = {
"AWG",
"Length",
"Lead 1",
"Lead 2",
"Qty"
};
byte values[parameter_length] = {
10, 10, 5, 5, 10
};
// byte cursor = 0;
byte cursor = 1;
void updateDisplay() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
drawIndicators();
drawParameters();
drawCta();
display.display();
}
void drawIndicators() {
display.drawBitmap(0, 11, bm_wire, display.width(), 9, 1);
if (cursor == 1)
display.drawBitmap(10, 23, bm_length, 44, 3, 1);
if (cursor == 2)
display.drawBitmap(0, 23, bm_lead, 11, 3, 1);
if (cursor == 3)
display.drawBitmap(53, 23, bm_lead, 11, 3, 1);
}
bool selected = false;
bool processing = false;
void drawParameters() {
for (int i = 0; i < parameter_length; i++) {
int offset = 29 + (i * 16);
if (cursor == i) {
if (selected) {
display.fillRoundRect(0, offset, display.width(), 15, 3, SSD1306_WHITE);
display.setTextColor(SSD1306_BLACK);
} else {
display.drawRoundRect(0, offset, display.width(), 15, 3, SSD1306_WHITE);
}
}
display.setCursor(3, offset + 4);
display.print(parameters[i]);
printRg(String(values[i]), 3, offset + 4);
if (selected)
display.setTextColor(SSD1306_WHITE);
}
}
void drawCta() {
display.fillRoundRect(0, 113, display.width(), 15, 3, SSD1306_WHITE);
if (cursor == parameter_length)
display.drawRoundRect(1, 114, display.width() - 2, 13, 3, SSD1306_BLACK);
display.setTextColor(SSD1306_BLACK);
printCtr(processing ? "..." : "Start", 0, 117);
display.setTextColor(SSD1306_WHITE);
}
void printCtr(const String &buf, int16_t x, int16_t y) {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
display.setCursor((x + (display.width() - w) / 2), y);
display.print(buf);
}
void printRg(const String &buf, int16_t x, int16_t y) {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
display.setCursor((display.width() - x - w), y);
display.print(buf);
}
void loop() {
debounce(&click, 50);
if (!processing)
decodeRotary(&cw, &ccw);
}
void cw() {
if (!selected && cursor == parameter_length)
return;
if (selected)
values[cursor]++;
else
cursor++;
updateDisplay();
}
void ccw() {
// if (!selected && cursor == 0)
if (!selected && cursor == 1)
return;
if (selected)
values[cursor]--;
else
cursor--;
updateDisplay();
}
bool clicked = false;
void click() {
if (digitalRead(ENCODER_SW) == LOW && !clicked) {
clicked = true;
if (cursor < parameter_length)
selected = !selected;
else
processing = !processing;
updateDisplay();
} else if (digitalRead(ENCODER_SW) == HIGH && clicked) {
clicked = false;
}
}
Loading
ssd1306
ssd1306