You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
3.0 KiB
165 lines
3.0 KiB
2 years ago
|
#include <Wire.h>
|
||
|
#include <Adafruit_GFX.h>
|
||
|
#include <Adafruit_SSD1306.h>
|
||
|
#include <Adafruit_FeatherOLED.h>
|
||
|
|
||
|
|
||
|
#define DEBUG 0
|
||
|
|
||
|
#define BUTTON_HOLD_TIME 500
|
||
|
#define BUTTON_PRESS_TIME 20
|
||
|
|
||
|
#define UP_BUTTON 31
|
||
|
#define ENTER_BUTTON 30
|
||
|
#define DOWN_BUTTON 27
|
||
|
|
||
|
Adafruit_FeatherOLED oled = Adafruit_FeatherOLED();
|
||
|
|
||
|
static const unsigned char PROGMEM arrow[] =
|
||
|
{ B10000000,
|
||
|
B11000000,
|
||
|
B11100000,
|
||
|
B11000000,
|
||
|
B10000000};
|
||
|
//oled.drawBitmap(0, 1, arrow, 8, 5, 1);
|
||
|
|
||
|
|
||
|
enum buttonStates {
|
||
|
OPEN,
|
||
|
CLOSED,
|
||
|
CHECK_PRESSED,
|
||
|
PRESSED,
|
||
|
HELD,
|
||
|
NUM_BUTTONSTATES
|
||
|
};
|
||
|
|
||
|
enum screenStates {
|
||
|
PRESSURE,
|
||
|
SETTINGS,
|
||
|
NUM_SCREENSTATES
|
||
|
};
|
||
|
|
||
|
enum buttonStates upButton = OPEN;
|
||
|
enum buttonStates enterButton = OPEN;
|
||
|
enum buttonStates downButton = OPEN;
|
||
|
|
||
|
int pressureSetPoint = 69;
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
Serial.begin(115200);
|
||
|
|
||
|
oled.init();
|
||
|
|
||
|
pinMode(UP_BUTTON, INPUT_PULLUP);
|
||
|
pinMode(ENTER_BUTTON, INPUT); // Has external pullup
|
||
|
pinMode(DOWN_BUTTON, INPUT_PULLUP);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
pollButtons();
|
||
|
runUI();
|
||
|
}
|
||
|
|
||
|
void runUI() {
|
||
|
static enum screenStates screenState = PRESSURE;
|
||
|
|
||
|
oled.clearDisplay();
|
||
|
|
||
|
switch (screenState) {
|
||
|
case PRESSURE:
|
||
|
if (enterButton == HELD) screenState = SETTINGS;
|
||
|
|
||
|
if (upButton == PRESSED) {
|
||
|
pressureSetPoint++;
|
||
|
} else if (downButton == PRESSED) {
|
||
|
pressureSetPoint--;
|
||
|
} else if (upButton == HELD) {
|
||
|
pressureSetPoint++;
|
||
|
delay(75);
|
||
|
} else if (downButton == HELD) {
|
||
|
pressureSetPoint--;
|
||
|
delay(75);
|
||
|
}
|
||
|
|
||
|
|
||
|
oled.setCursor(0,0);
|
||
|
oled.setTextSize(3);
|
||
|
oled.print(pressureSetPoint);
|
||
|
|
||
|
oled.display();
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void pollButtons() {
|
||
|
static unsigned long upButtonTime = 0;
|
||
|
static unsigned long enterButtonTime = 0;
|
||
|
static unsigned long downButtonTime = 0;
|
||
|
|
||
|
processButtonState(UP_BUTTON, upButton, upButtonTime);
|
||
|
processButtonState(ENTER_BUTTON, enterButton, enterButtonTime);
|
||
|
processButtonState(DOWN_BUTTON, downButton, downButtonTime);
|
||
|
|
||
|
if (DEBUG) {
|
||
|
if (upButton == PRESSED) {
|
||
|
Serial.println("up button pressed");
|
||
|
} else if (upButton == HELD) {
|
||
|
Serial.println("up button held");
|
||
|
}
|
||
|
|
||
|
if (enterButton == PRESSED) {
|
||
|
Serial.println("enter button pressed");
|
||
|
} else if (enterButton == HELD) {
|
||
|
Serial.println("enter button held");
|
||
|
}
|
||
|
|
||
|
if (downButton == PRESSED) {
|
||
|
Serial.println("down button pressed");
|
||
|
} else if (downButton == HELD) {
|
||
|
Serial.println("down button held");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void processButtonState(int buttonPin, buttonStates &buttonState, unsigned long &buttonTime) {
|
||
|
bool pinState = !digitalRead(buttonPin);
|
||
|
|
||
|
switch(buttonState) {
|
||
|
case OPEN:
|
||
|
if (pinState) {
|
||
|
buttonState = CLOSED;
|
||
|
buttonTime = millis();
|
||
|
}
|
||
|
break;
|
||
|
case CLOSED:
|
||
|
if (millis() >= buttonTime + BUTTON_HOLD_TIME) {
|
||
|
buttonState = HELD;
|
||
|
}
|
||
|
if (pinState) {
|
||
|
;
|
||
|
} else {
|
||
|
buttonState = CHECK_PRESSED;
|
||
|
}
|
||
|
break;
|
||
|
case CHECK_PRESSED:
|
||
|
if (millis() >= buttonTime + BUTTON_PRESS_TIME) {
|
||
|
buttonState = PRESSED;
|
||
|
} else {
|
||
|
buttonState = OPEN;
|
||
|
}
|
||
|
break;
|
||
|
case PRESSED:
|
||
|
buttonState = OPEN;
|
||
|
break;
|
||
|
case HELD:
|
||
|
if (!pinState) {
|
||
|
buttonState = OPEN;
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|