From 0aac756d7eeb2785f575a8a5d9d71904b559d12a Mon Sep 17 00:00:00 2001 From: Andrea Baccega Date: Sat, 27 Jan 2024 14:17:01 +0100 Subject: [PATCH] Added EEProm manager --- src/EEPROMDataManager.cpp | 39 +++++++++++ src/EEPROMDataManager.h | 16 +++++ src/displays/oled.cpp | 117 +++++++++++++++++++++------------ src/displays/oled.h | 1 + src/globals.cpp | 36 ++++++++++ src/globals.h | 8 +++ src/main.cpp | 38 ++--------- src/thermistors/Thermistor.cpp | 25 +------ src/thermistors/Thermistor.h | 15 +++-- 9 files changed, 190 insertions(+), 105 deletions(-) create mode 100644 src/EEPROMDataManager.cpp create mode 100644 src/EEPROMDataManager.h create mode 100644 src/globals.cpp diff --git a/src/EEPROMDataManager.cpp b/src/EEPROMDataManager.cpp new file mode 100644 index 0000000..c04e102 --- /dev/null +++ b/src/EEPROMDataManager.cpp @@ -0,0 +1,39 @@ +#include "EEPROMDataManager.h" +#include "globals.h" + +void EEPROMDataManager::setup() +{ + EEPROM.begin(); + uint16_t eepromData; + EEPROM.get(0, eepromData); + if (eepromData != 0x1234) + { + // EEPROM is empty, so we need to initialize it + saveToEEPROM(); + } + + for (int i = 0; i < 6; i++) + { + uint16_t resUint; + EEPROM.get(2 * i + EEPROM_START_TERMISTORS, resUint); + float res = resUint / 1000.0; + thermistors[i].setPotentiometerResistance(res); + } + + EEPROM.get(EEPROM_START_PLATERES, plateResistanceOhm); +} +void EEPROMDataManager::saveToEEPROM() +{ + EEPROM.begin(); + uint16_t tmp = 0x1234; + EEPROM.put(0, &tmp); // initialize the EEPROM + + for (int i = 0; i < 6; i++) + { + float res = thermistors[i].getResistance(); + uint16_t resUint = res * 1000; + EEPROM.put(2 * i + EEPROM_START_TERMISTORS, &resUint); + } + + EEPROM.put(EEPROM_START_PLATERES, &plateResistanceOhm); +} \ No newline at end of file diff --git a/src/EEPROMDataManager.h b/src/EEPROMDataManager.h new file mode 100644 index 0000000..8945277 --- /dev/null +++ b/src/EEPROMDataManager.h @@ -0,0 +1,16 @@ +#ifndef EEPROMDATAMANAGER_H +#define EEPROMDATAMANAGER_H +#include +#define EEPROM_START_TERMISTORS 2 +#define EEPROM_START_PLATERES (EEPROM_START_TERMISTORS + 2 * 6) +#define EEPROM_START_REFLOWPROFILES (EEPROM_START_PLATERES + 2) + +class EEPROMDataManager +{ +public: + EEPROMDataManager(){}; + void setup(); + void saveToEEPROM(); +}; + +#endif \ No newline at end of file diff --git a/src/displays/oled.cpp b/src/displays/oled.cpp index 3c14ecb..cdd9ec4 100644 --- a/src/displays/oled.cpp +++ b/src/displays/oled.cpp @@ -6,6 +6,7 @@ #define SCREEN_HEIGHT 64 #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define MENUID_PLATERES 2 +#define MENUITEM_THERMISTOR_START 150 unsigned long lastProcessedReflowState = 0; OledDisplay::OledDisplay() @@ -25,15 +26,21 @@ void OledDisplay::handleButtonStateChange(Pairparent; if (selectedMenu != NULL) { curMenu = selectedMenu; } - } else if (change.first == ButtonKind::UP) { + } + else if (change.first == ButtonKind::UP) + { curMenu->goNextItem(); - } else if (change.first == ButtonKind::DOWN){ + } + else if (change.first == ButtonKind::DOWN) + { curMenu->goPrevItem(); } } @@ -42,40 +49,47 @@ void OledDisplay::setup() { curMenu = new OledMenu(); curMenu->setElements(new OledMenuItem[3]{ - OledMenuItem("Reflow\0"), - OledMenuItem("PlateR\0"), - OledMenuItem("Temps\0"), - }, 3); + OledMenuItem("Reflow\0"), + OledMenuItem("PlateR\0"), + OledMenuItem("Temps\0"), + }, + 3); OledMenu *pickProfilesMenu = new OledMenu(1); OledMenuItem *pickProfilesMenuItems = new OledMenuItem[nReflowProfiles]; - for (int i=0; isetElements(pickProfilesMenuItems, nReflowProfiles); - + OledMenu *plateRMenu = new OledMenu(MENUID_PLATERES); OledMenu *tempsMenu = new OledMenu(3); - tempsMenu->setElements(new OledMenuItem[6]{ - OledMenuItem("T1\0", 150), - OledMenuItem("T2\0", 151), - OledMenuItem("T3\0", 152), - OledMenuItem("T4\0", 153), - OledMenuItem("T5\0", 154), - OledMenuItem("T6\0", 155), - }, 6); - - curMenu->setChildren(new OledMenu*[3]{ - pickProfilesMenu, - plateRMenu, - tempsMenu, - }, 3); + tempsMenu->setElements(new OledMenuItem[7]{ + OledMenuItem("ALL\0", MENUITEM_THERMISTOR_START + 6), + OledMenuItem("T1\0", MENUITEM_THERMISTOR_START + 0), + OledMenuItem("T2\0", MENUITEM_THERMISTOR_START + 1), + OledMenuItem("T3\0", MENUITEM_THERMISTOR_START + 2), + OledMenuItem("T4\0", MENUITEM_THERMISTOR_START + 3), + OledMenuItem("T5\0", MENUITEM_THERMISTOR_START + 4), + OledMenuItem("T6\0", MENUITEM_THERMISTOR_START + 5), + }, + 6); + + curMenu->setChildren( + new OledMenu *[3] + { + pickProfilesMenu, + plateRMenu, + tempsMenu, + }, + 3); curMenu->setChildrenMatrix(3, new uint8_t[3][2]{ - {0, 0}, - {1, 1}, - {2, 2}, - }); - + {0, 0}, + {1, 1}, + {2, 2}, + }); + // curItem = 0; // ROOT // Setup implementation @@ -99,26 +113,36 @@ void OledDisplay::loop() if (state == USER_INPUT) { display.clearDisplay(); + display.setRotation(0); display.setTextSize(2); OledMenuItem menuItem = curMenu->getCurItem(); display.setTextSize(2); - if (curMenu->identifier == MENUID_PLATERES) { - - } else { + if (menuItem.identifier >= MENUITEM_THERMISTOR_START && menuItem.identifier < MENUITEM_THERMISTOR_START + 7) + { + int thermistorIndex = menuItem.identifier - MENUITEM_THERMISTOR_START; + if (thermistorIndex == 6) { + // Showing all + } else { + int thermistorTemp = thermistors[thermistorIndex].getTemperature(); + centerText((String(menuItem.title) + ": " + String(thermistorTemp)).c_str()); + displayIndicators(); + } + + } + else if (curMenu->identifier == MENUID_PLATERES) + { + // TODO: Logic for plate resistance + } + else + { // draw menu item with selectors. - - display.setRotation(0); - centerText(menuItem.title); - display.setRotation(1); - display.setCursor(0,SCREEN_WIDTH/2-5); - display.print("<"); - display.setCursor(SCREEN_HEIGHT-14,SCREEN_WIDTH/2-5); - display.print(">"); + centerText(menuItem.title); + displayIndicators(); } - + display.display(); } // Loop implementation @@ -145,13 +169,20 @@ void OledDisplay::drawDebug() display.println("C°: " + String(thermistor1Temp)); display.display(); } - +void OledDisplay::displayIndicators() +{ + display.setRotation(1); + display.setCursor(0, SCREEN_WIDTH / 2 - 5); + display.print("<"); + display.setCursor(SCREEN_HEIGHT - 14, SCREEN_WIDTH / 2 - 5); + display.print(">"); +} void OledDisplay::centerText(const char *txt) { int16_t x1, y1; uint16_t w, h; display.getTextBounds(txt, 0, 0, &x1, &y1, &w, &h); - display.setCursor(display.width() / 2 - w / 2, display.height() / 2 -h/2); + display.setCursor(display.width() / 2 - w / 2, display.height() / 2 - h / 2); display.println(txt); } diff --git a/src/displays/oled.h b/src/displays/oled.h index 4bcb48f..97faab1 100644 --- a/src/displays/oled.h +++ b/src/displays/oled.h @@ -20,6 +20,7 @@ class OledDisplay { void handleReflowState(); void centerText(const char * text); + void displayIndicators(); }; diff --git a/src/globals.cpp b/src/globals.cpp new file mode 100644 index 0000000..2d3cb8d --- /dev/null +++ b/src/globals.cpp @@ -0,0 +1,36 @@ +#include "./globals.h" +#include "./EEPROMDataManager.h" + +WrappedState reflowProcessState = WrappedState(INITIALIZING); +AnalogRef analogRef(5.0); +TempCalibration calibration_100K_3950 = {25, 100000, 86, 10000, 170, 1000}; +// Initalize the 3950 100K thermistors with ACTUAL reference resistor measurnment(Measured between Left pin and GND when the board is powered off) using the default calibration data for 100K thermistor +Thermistor thermistor1(THERMISTOR1_PIN, 2500); +Thermistor thermistor2(THERMISTOR2_PIN, 2500); +Thermistor thermistor3(THERMISTOR3_PIN, 2500); +Thermistor thermistor4(THERMISTOR4_PIN, 2500); +Thermistor thermistor5(THERMISTOR5_PIN, 2500); +Thermistor thermistor6(THERMISTOR6_PIN, 9000); +Thermistor thermistors[6] = {thermistor1, thermistor2, thermistor3, thermistor4, thermistor5, thermistor6}; + +ReflowProfile reflowProfiles[] = { + ReflowProfile(new ReflowStep[5] { + ReflowStep(ReflowProcessState::PREHEAT, 2, 150), + ReflowStep(ReflowProcessState::SOAK, 3, 180), + ReflowStep(ReflowProcessState::REFLOW, 3, 220, EASE_IN_OUT), + ReflowStep(ReflowProcessState::COOL, 3, 100), + ReflowStep(ReflowProcessState::DONE, 0, 0) + }, "Test\0"), + ReflowProfile(new ReflowStep[5] { + ReflowStep(ReflowProcessState::PREHEAT, 2, 150), + ReflowStep(ReflowProcessState::SOAK, 3, 180), + ReflowStep(ReflowProcessState::REFLOW, 3, 220, EASE_IN_OUT), + ReflowStep(ReflowProcessState::COOL, 3, 100), + ReflowStep(ReflowProcessState::DONE, 0, 0) + }, "Test2\0"), +}; + +int nReflowProfiles = 2; + +uint16_t plateResistanceOhm = 0; +EEPROMDataManager eepromDataManager = EEPROMDataManager(); diff --git a/src/globals.h b/src/globals.h index 6c831a9..85c1424 100644 --- a/src/globals.h +++ b/src/globals.h @@ -3,6 +3,7 @@ #include "./common.h" #include "./thermistors/Thermistor.h" #include "./reflow.h" +#include "./EEPROMDataManager.h" extern WrappedState reflowProcessState; extern AnalogRef analogRef; @@ -12,6 +13,13 @@ extern Thermistor thermistor3; extern Thermistor thermistor4; extern Thermistor thermistor5; extern Thermistor thermistor6; +extern Thermistor thermistors[6]; extern ReflowProfile reflowProfiles[]; +extern uint16_t plateResistanceOhm; +// EEPROM data manager is in its own file extern int nReflowProfiles; + + +extern EEPROMDataManager eepromDataManager; + #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 708cc7b..d24613b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,14 +10,7 @@ #include "reflow.h" #include "displays/oled.h" #include "globals.h" - -WrappedState reflowProcessState = WrappedState(INITIALIZING); - -// Define the analog ref used for the system voltage -AnalogRef analogRef(5.0); - -// Calibration data for 100K thermistor -TempCalibration calibration_100K_3950 = {25, 100000, 86, 10000, 170, 1000}; +#include "EEPROMDataManager.h" // LCD display pins #define TFT_CS 7 @@ -29,37 +22,12 @@ TempCalibration calibration_100K_3950 = {25, 100000, 86, 10000, 170, 1000}; Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST); -// Initalize the 3950 100K thermistors with ACTUAL reference resistor measurnment(Measured between Left pin and GND when the board is powered off) using the default calibration data for 100K thermistor -Thermistor thermistor1(THERMISTOR1_PIN, 2500); -Thermistor thermistor2(THERMISTOR2_PIN, 2500); -Thermistor thermistor3(THERMISTOR3_PIN, 2500); -Thermistor thermistor4(THERMISTOR4_PIN, 2500); -Thermistor thermistor5(THERMISTOR5_PIN, 2500); -Thermistor thermistor6(THERMISTOR6_PIN, 9000); - -// Initialize the buttons Buttons buttons = Buttons(); LEDS leds = LEDS(); // Declare the PID ArduPID PID; OledDisplay oled = OledDisplay(); -ReflowProfile reflowProfiles[] = { - ReflowProfile(new ReflowStep[5] { - ReflowStep(ReflowProcessState::PREHEAT, 2, 150), - ReflowStep(ReflowProcessState::SOAK, 3, 180), - ReflowStep(ReflowProcessState::REFLOW, 3, 220, EASE_IN_OUT), - ReflowStep(ReflowProcessState::COOL, 3, 100), - ReflowStep(ReflowProcessState::DONE, 0, 0) - }, "Test\0"), - ReflowProfile(new ReflowStep[5] { - ReflowStep(ReflowProcessState::PREHEAT, 2, 150), - ReflowStep(ReflowProcessState::SOAK, 3, 180), - ReflowStep(ReflowProcessState::REFLOW, 3, 220, EASE_IN_OUT), - ReflowStep(ReflowProcessState::COOL, 3, 100), - ReflowStep(ReflowProcessState::DONE, 0, 0) - }, "Test2\0"), -}; -int nReflowProfiles = 2; + void setup() { @@ -72,6 +40,8 @@ void setup() buttons.setup(); leds.setup(); oled.setup(); + eepromDataManager.setup(); + reflowProcessState = USER_INPUT; } diff --git a/src/thermistors/Thermistor.cpp b/src/thermistors/Thermistor.cpp index 0437b2b..8736798 100644 --- a/src/thermistors/Thermistor.cpp +++ b/src/thermistors/Thermistor.cpp @@ -1,31 +1,12 @@ #include "Thermistor.h" -Thermistor::Thermistor() -{ -} -Thermistor::Thermistor(uint8_t pin, float resistance, TempCalibration calibration) +void Thermistor::setPotentiometerResistance(float resistance) { - - thermistorPin = pin; setRes = resistance; - calculateCoefficents(resistance, calibration); } -Thermistor::Thermistor(uint8_t pin, float resistance) -{ - - thermistorPin = pin; - setRes = resistance; - - calculateCoefficents(resistance, calibration_100K_3950); -} - -Thermistor::~Thermistor() -{ -} - int Thermistor::getTemperature() { @@ -33,7 +14,7 @@ int Thermistor::getTemperature() int temp = 0; uint8_t samples = 5; - + for (int i = 0; i < samples; i++) { getResistance(); @@ -82,7 +63,7 @@ float Thermistor::getResistance() float vOut = (buffer) / 1023; // Serial.println(vOut); buffer = (systemVoltage / vOut) - 1; - + // return the resistence sensorResistance = setRes * buffer; diff --git a/src/thermistors/Thermistor.h b/src/thermistors/Thermistor.h index f6b41a8..4239f73 100644 --- a/src/thermistors/Thermistor.h +++ b/src/thermistors/Thermistor.h @@ -41,18 +41,21 @@ public: // Constructor Thermistor(); - Thermistor(uint8_t pin, float resistance, TempCalibration calibration); + Thermistor(uint8_t pin, float resistance, TempCalibration calibration): thermistorPin(pin), setRes(resistance), calibration(calibration) { + calculateCoefficents(resistance, calibration); + } - Thermistor(uint8_t pin, float resistance); - - // Destructor - ~Thermistor(); + Thermistor(uint8_t pin, float resistance): thermistorPin(pin), setRes(resistance), calibration(calibration_100K_3950) { + calculateCoefficents(resistance, calibration); + } // Public Methods int getTemperature(); float getResistance(); + void setPotentiometerResistance(float resistance); // Public Variables + void calculateCoefficents(float resistance, TempCalibration calibration); private: const double K = 273.15; @@ -61,7 +64,7 @@ private: float setRes; Coefficents coefficents; float referenceResistance; - void calculateCoefficents(float resistance, TempCalibration calibration); + TempCalibration calibration; }; #endif // THERMISTOR_H