test works
This commit is contained in:
+3
-5
@@ -4,10 +4,8 @@ board = adafruit_feather
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
board_build.core = earlephilhower
|
board_build.core = earlephilhower
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
build_flags =
|
|
||||||
-DUSE_TINYUSB
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
adafruit/Adafruit SSD1306
|
|
||||||
adafruit/Adafruit GFX Library
|
adafruit/Adafruit GFX Library
|
||||||
adafruit/Adafruit TinyUSB Library
|
adafruit/Adafruit SSD1306
|
||||||
adafruit/SdFat - Adafruit Fork
|
adafruit/Adafruit BusIO
|
||||||
+114
-40
@@ -4,17 +4,37 @@
|
|||||||
#include <Adafruit_SSD1306.h>
|
#include <Adafruit_SSD1306.h>
|
||||||
|
|
||||||
// OLED on SPI1 (SCK = GP14, MOSI = GP15)
|
// OLED on SPI1 (SCK = GP14, MOSI = GP15)
|
||||||
// If your display worked with other pins, change these three lines.
|
#define OLED_CS 24 // change to 24 when the PCM5100 uses D9
|
||||||
#define OLED_CS 9
|
|
||||||
#define OLED_DC 6
|
#define OLED_DC 6
|
||||||
#define OLED_RST 5
|
#define OLED_RST 5
|
||||||
|
|
||||||
|
#define BAT_PIN A3
|
||||||
#define BAT_PIN A3 // GP29, battery voltage / 2
|
|
||||||
#define SAMPLES 16
|
#define SAMPLES 16
|
||||||
|
|
||||||
|
#define ENC_CLK 26 // A0
|
||||||
|
#define ENC_DT 27 // A1
|
||||||
|
#define ENC_SW 28 // A2
|
||||||
|
|
||||||
|
#define NUM_MIN 0
|
||||||
|
#define NUM_MAX 999
|
||||||
|
#define NUM_STEP 1
|
||||||
|
|
||||||
Adafruit_SSD1306 display(128, 64, &SPI1, OLED_DC, OLED_RST, OLED_CS);
|
Adafruit_SSD1306 display(128, 64, &SPI1, OLED_DC, OLED_RST, OLED_CS);
|
||||||
|
|
||||||
|
volatile int encPos = 0;
|
||||||
|
volatile uint8_t encState = 0;
|
||||||
|
const int8_t ENC_TABLE[16] = {
|
||||||
|
0, -1, 1, 0,
|
||||||
|
1, 0, 0, -1,
|
||||||
|
-1, 0, 0, 1,
|
||||||
|
0, 1, -1, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
int screenId = 0; // 0 = battery, 1 = number
|
||||||
|
int number = 0;
|
||||||
|
float batVolts = 0;
|
||||||
|
int batPct = 0;
|
||||||
|
|
||||||
struct BatPoint { float v; int pct; };
|
struct BatPoint { float v; int pct; };
|
||||||
const BatPoint CURVE[] = {
|
const BatPoint CURVE[] = {
|
||||||
{4.20f, 100}, {4.10f, 90}, {4.00f, 80}, {3.92f, 70}, {3.87f, 60},
|
{4.20f, 100}, {4.10f, 90}, {4.00f, 80}, {3.92f, 70}, {3.87f, 60},
|
||||||
@@ -23,10 +43,16 @@ const BatPoint CURVE[] = {
|
|||||||
};
|
};
|
||||||
const int CURVE_N = sizeof(CURVE) / sizeof(CURVE[0]);
|
const int CURVE_N = sizeof(CURVE) / sizeof(CURVE[0]);
|
||||||
|
|
||||||
|
void encISR() {
|
||||||
|
uint8_t newState = (digitalRead(ENC_CLK) << 1) | digitalRead(ENC_DT);
|
||||||
|
encPos += ENC_TABLE[(encState << 2) | newState];
|
||||||
|
encState = newState;
|
||||||
|
}
|
||||||
|
|
||||||
float readVolts() {
|
float readVolts() {
|
||||||
uint32_t sum = 0;
|
uint32_t sum = 0;
|
||||||
for (int i = 0; i < SAMPLES; i++) sum += analogRead(BAT_PIN);
|
for (int i = 0; i < SAMPLES; i++) sum += analogRead(BAT_PIN);
|
||||||
return (sum / (float)SAMPLES) * 3.3f / 4095.0f * 3.55f;
|
return (sum / (float)SAMPLES) * 3.3f / 4095.0f * 2.0f; // unverified divider
|
||||||
}
|
}
|
||||||
|
|
||||||
int voltsToPercent(float v) {
|
int voltsToPercent(float v) {
|
||||||
@@ -48,56 +74,104 @@ void drawBatteryIcon(int x, int y, int pct) {
|
|||||||
display.fillRect(x + 2, y + 2, w, 8, SSD1306_WHITE);
|
display.fillRect(x + 2, y + 2, w, 8, SSD1306_WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void drawBatteryScreen() {
|
||||||
|
display.setTextSize(1);
|
||||||
|
display.setCursor(0, 0);
|
||||||
|
display.print("Battery");
|
||||||
|
display.setTextSize(3);
|
||||||
|
display.setCursor(0, 18);
|
||||||
|
display.print(batPct);
|
||||||
|
display.print('%');
|
||||||
|
display.setTextSize(1);
|
||||||
|
display.setCursor(0, 54);
|
||||||
|
display.print(batVolts, 2);
|
||||||
|
display.print(" V");
|
||||||
|
drawBatteryIcon(96, 52, batPct);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawNumberScreen() {
|
||||||
|
display.setTextSize(1);
|
||||||
|
display.setCursor(0, 0);
|
||||||
|
display.print("Number");
|
||||||
|
|
||||||
|
char buf[8];
|
||||||
|
snprintf(buf, sizeof(buf), "%d", number);
|
||||||
|
display.setTextSize(4);
|
||||||
|
int textW = strlen(buf) * 6 * 4;
|
||||||
|
display.setCursor((128 - textW) / 2, 20);
|
||||||
|
display.print(buf);
|
||||||
|
|
||||||
|
display.drawRect(0, 56, 128, 8, SSD1306_WHITE);
|
||||||
|
int w = map(number, NUM_MIN, NUM_MAX, 0, 124);
|
||||||
|
display.fillRect(2, 58, w, 4, SSD1306_WHITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawScreen() {
|
||||||
|
display.clearDisplay();
|
||||||
|
display.setTextColor(SSD1306_WHITE);
|
||||||
|
if (screenId == 0) drawBatteryScreen();
|
||||||
|
else drawNumberScreen();
|
||||||
|
display.display();
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
analogReadResolution(12);
|
analogReadResolution(12);
|
||||||
|
|
||||||
|
pinMode(ENC_CLK, INPUT_PULLUP);
|
||||||
|
pinMode(ENC_DT, INPUT_PULLUP);
|
||||||
|
pinMode(ENC_SW, INPUT_PULLUP);
|
||||||
|
encState = (digitalRead(ENC_CLK) << 1) | digitalRead(ENC_DT);
|
||||||
|
attachInterrupt(digitalPinToInterrupt(ENC_CLK), encISR, CHANGE);
|
||||||
|
attachInterrupt(digitalPinToInterrupt(ENC_DT), encISR, CHANGE);
|
||||||
|
|
||||||
SPI1.setSCK(14);
|
SPI1.setSCK(14);
|
||||||
SPI1.setTX(15);
|
SPI1.setTX(15);
|
||||||
if (!display.begin(SSD1306_SWITCHCAPVCC)) {
|
if (!display.begin(SSD1306_SWITCHCAPVCC)) {
|
||||||
Serial.println("OLED init failed");
|
Serial.println("OLED init failed");
|
||||||
while (true);
|
while (true) delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
display.clearDisplay();
|
batVolts = readVolts();
|
||||||
display.setTextColor(SSD1306_WHITE);
|
batPct = voltsToPercent(batVolts);
|
||||||
display.setTextSize(2);
|
drawScreen();
|
||||||
display.setCursor(0, 0);
|
|
||||||
display.print("Hello");
|
|
||||||
display.display();
|
|
||||||
delay(1500);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
float volts = readVolts();
|
static int lastDetent = 0;
|
||||||
int pct = voltsToPercent(volts);
|
static bool lastBtn = HIGH;
|
||||||
|
static uint32_t lastPress = 0;
|
||||||
|
static uint32_t lastBat = 0;
|
||||||
|
bool redraw = false;
|
||||||
|
|
||||||
Serial.print(volts, 2);
|
noInterrupts();
|
||||||
Serial.print(" V ");
|
int pos = encPos;
|
||||||
Serial.print(pct);
|
interrupts();
|
||||||
Serial.println(" %");
|
int detent = pos / 4; // use /2 if one click gives 2 counts
|
||||||
|
if (detent != lastDetent) {
|
||||||
|
int delta = detent - lastDetent;
|
||||||
|
lastDetent = detent;
|
||||||
|
if (screenId == 1) {
|
||||||
|
number = constrain(number + delta * NUM_STEP, NUM_MIN, NUM_MAX);
|
||||||
|
Serial.println(number);
|
||||||
|
redraw = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
display.clearDisplay();
|
bool btn = digitalRead(ENC_SW);
|
||||||
display.setTextColor(SSD1306_WHITE);
|
if (btn == LOW && lastBtn == HIGH && millis() - lastPress > 50) {
|
||||||
|
lastPress = millis();
|
||||||
|
screenId = (screenId + 1) % 2;
|
||||||
|
redraw = true;
|
||||||
|
}
|
||||||
|
lastBtn = btn;
|
||||||
|
|
||||||
display.setTextSize(1);
|
if (millis() - lastBat >= 1000) {
|
||||||
display.setCursor(0, 0);
|
lastBat = millis();
|
||||||
display.print("Battery");
|
batVolts = readVolts();
|
||||||
|
batPct = voltsToPercent(batVolts);
|
||||||
|
if (screenId == 0) redraw = true;
|
||||||
|
}
|
||||||
|
|
||||||
display.setTextSize(3);
|
if (redraw) drawScreen();
|
||||||
display.setCursor(0, 18);
|
|
||||||
display.print(pct);
|
|
||||||
display.print('%');
|
|
||||||
|
|
||||||
display.setTextSize(1);
|
|
||||||
display.setCursor(0, 54);
|
|
||||||
display.print(volts, 2);
|
|
||||||
display.print(" V");
|
|
||||||
|
|
||||||
drawBatteryIcon(96, 52, pct);
|
|
||||||
|
|
||||||
display.display();
|
|
||||||
delay(1000);
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user