oled responding to commands

This commit is contained in:
Andrea Baccega 2024-01-26 14:58:49 +01:00
parent 18929e46f8
commit 43dd30f135
8 changed files with 177 additions and 44 deletions

View File

@ -21,5 +21,5 @@ public:
Pair<ButtonKind, StateChangeEvent<ButtonState>> *handleButtons();
void setup();
};
#define ISBUTTONMIGRATEDTOSTATE(pair, kind, state) (((pair).first == kind) && ((pair).second.to == state))
#endif // BUTTONS_H

View File

@ -6,6 +6,7 @@ class StateChangeEvent
{
public:
StateChangeEvent(State from, State to) : from(from), to(to) {}
StateChangeEvent(State from) : from(from), to(from) {}
State from;
State to;
};
@ -14,7 +15,7 @@ template <typename State>
class WrappedState
{
public:
WrappedState(State defaultState) : state(defaultState), lastChangeEvent(new StateChangeEvent<State>(defaultState, defaultState)) {}
WrappedState(State defaultState) : state(defaultState), lastChangeEvent(new StateChangeEvent<State>(defaultState)) {}
StateChangeEvent<State> *set(State state)
{
if (this->state == state)
@ -27,10 +28,19 @@ public:
this->lastStateChangeTime = millis();
return lastChangeEvent;
}
State get()
State get() const
{
return this->state;
}
StateChangeEvent<State>* getSince(unsigned long time) const
{
if (this->lastStateChangeTime < time)
{
return NULL;
}
return lastChangeEvent;
}
State state;
unsigned long lastStateChangeTime = 0;
StateChangeEvent<State> *lastChangeEvent;

View File

@ -1,49 +1,153 @@
#include "./oled.h"
#include <Arduino.h>
#include "../globals.h"
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
OledDisplay::OledDisplay() {
unsigned long lastProcessedReflowState = 0;
OledDisplay::OledDisplay()
{
display = Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT);
}
void OledDisplay::setup() {
void OledDisplay::setup()
{
// Setup implementation
bool initialized = display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
if (!initialized) {
if (!initialized)
{
Serial.println("OLED Display failed to initialize");
} else {
}
else
{
Serial.println("OLED Display initialized");
}
display.setRotation(0);
display.setCursor(0, 0);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE); // Defaults to white
}
void OledDisplay::loop() {
void OledDisplay::loop()
{
StateChangeEvent<ReflowProcessState> *evt = reflowProcessState.getSince(lastProcessedReflowState);
// REdraw only on reflow state change
if (evt != NULL)
{
lastProcessedReflowState = millis();
ReflowProcessState reflowState = reflowProcessState.get();
if (reflowState == USER_INPUT)
{
handleUserInputState();
return;
}
else if (reflowState == SOAK)
{
handleSoakState();
return;
}
else if (reflowState == REFLOW)
{
handleReflowState();
return;
}
else if (reflowState == ReflowProcessState::COOL)
{
handleCoolingState();
return;
}
else if (reflowState == ReflowProcessState::DONE)
{
handleFinishedState();
return;
}
}
if (reflowProcessState.get() == PREHEAT)
{
drawDebug();
}
// Loop implementation
}
void OledDisplay::teardown()
{
// delete display;
}
void OledDisplay::drawDebug()
{
float sysVoltage = analogRef.calculateSystemVoltage();
float inputVoltage = analogRef.calculateInputVoltage();
int thermistor1Temp = thermistor1.getTemperature();
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
if (reflowProcessState == INITIALIZING) {
display.println("INITIALIZING");
} else if (reflowProcessState == USER_INPUT) {
display.println("USER_INPUT");
} else if (reflowProcessState == PREHEAT) {
display.println("PREHEAT");
} else if (reflowProcessState == SOAK) {
display.println("SOAK");
} else if (reflowProcessState == REFLOW) {
display.println("REFLOW");
} else if (reflowProcessState == COOL) {
display.println("COOL");
} else if (reflowProcessState == DONE) {
display.println("DONE");
}
display.println("zz");
display.println("SysV: " + String(sysVoltage));
display.setCursor(0, 20);
display.println("In V: " + String(inputVoltage));
display.setCursor(0, 40);
display.println("C°: " + String(thermistor1Temp));
display.display();
// Loop implementation
}
void OledDisplay::teardown() {
// delete display;
void OledDisplay::handleUserInputState()
{
Serial.println("USER_INPUT_STATE");
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.println("INPUT_STATE");
display.display();
}
void OledDisplay::handlePreheatState()
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("PREHEATING");
display.display();
}
void OledDisplay::handleSoakState()
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("SOAKING");
display.display();
}
void OledDisplay::handleReflowState()
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("REFLOW");
display.display();
}
void OledDisplay::handleCoolingState()
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("COOLING");
display.display();
}
void OledDisplay::handleFinishedState()
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("Done :)");
display.display();
}

View File

@ -11,6 +11,13 @@ class OledDisplay {
private:
Adafruit_SSD1306 display;
void drawDebug();
void handleUserInputState();
void handlePreheatState();
void handleSoakState();
void handleReflowState();
void handleCoolingState();
void handleFinishedState();
};

15
src/globals.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef GLOBALS_H
#define GLOBALS_H
#include "./common.h"
#include "./thermistors/Thermistor.h"
#include "./reflow.h"
extern WrappedState<ReflowProcessState> reflowProcessState;
extern AnalogRef analogRef;
extern Thermistor thermistor1;
extern Thermistor thermistor2;
extern Thermistor thermistor3;
extern Thermistor thermistor4;
extern Thermistor thermistor5;
extern Thermistor thermistor6;
#endif

View File

@ -1,5 +1,6 @@
#include "Leds.h"
#include <Arduino.h>
#include "../globals.h"
bool yellowLEDUPOn = false;
bool yellowLEDSeleOn = false;

View File

@ -9,9 +9,9 @@
#include "leds/leds.h"
#include "reflow.h"
#include "displays/oled.h"
#include "common.h"
#include "globals.h"
ReflowProcessState reflowProcessState = INITIALIZING;
WrappedState<ReflowProcessState> reflowProcessState = WrappedState<ReflowProcessState>(INITIALIZING);
// Define the analog ref used for the system voltage
AnalogRef analogRef(5.0);
@ -89,22 +89,21 @@ void setup()
void loop()
{
// Return the button that was pressed
// Return the button that changed state
Pair<ButtonKind, StateChangeEvent<ButtonState>> *k = buttons.handleButtons();
if (k != NULL) {
leds.handleButtonStateChange(*k);
if (ISBUTTONMIGRATEDTOSTATE(*k, ButtonKind::SELECT, ButtonState::PRESSED)) {
reflowProcessState.set(ReflowProcessState::PREHEAT);
} else if (ISBUTTONMIGRATEDTOSTATE(*k, ButtonKind::BACK, ButtonState::PRESSED)) {
reflowProcessState.set(ReflowProcessState::USER_INPUT);
} else if (ISBUTTONMIGRATEDTOSTATE(*k, ButtonKind::UP, ButtonState::PRESSED)) {
reflowProcessState.set(ReflowProcessState::COOL);
} else if (ISBUTTONMIGRATEDTOSTATE(*k, ButtonKind::DOWN, ButtonState::PRESSED)) {
reflowProcessState.set(ReflowProcessState::REFLOW);
}
}
float sysVoltage = analogRef.calculateSystemVoltage();
float inputVoltage = analogRef.calculateInputVoltage();
int thermistor1Temp = thermistor1.getTemperature();
oled.loop();
// Print the system voltage on the tft
// Serial.print("Output voltage: ");
// Serial.println(sysVoltage);
// Serial.print("Input voltage: ");
// Serial.println(analogRef.calculateInputVoltage());
}

View File

@ -1,5 +1,6 @@
#ifndef __reflow_h__
#define __reflow_h__
#include "./common.h"
// STATE MACHINE
enum ReflowProcessState {
@ -12,8 +13,4 @@ enum ReflowProcessState {
DONE // The reflow process is complete
};
// Holds current reflow process state to be used by other classes
// Such as the menu class to display the current state
extern ReflowProcessState reflowProcessState;
#endif