Added EEProm manager

This commit is contained in:
Andrea Baccega 2024-01-27 14:17:01 +01:00
parent 721113ddcb
commit 0aac756d7e
9 changed files with 190 additions and 105 deletions

39
src/EEPROMDataManager.cpp Normal file
View File

@ -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);
}

16
src/EEPROMDataManager.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef EEPROMDATAMANAGER_H
#define EEPROMDATAMANAGER_H
#include <Arduino.h>
#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

View File

@ -6,6 +6,7 @@
#define SCREEN_HEIGHT 64 #define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define MENUID_PLATERES 2 #define MENUID_PLATERES 2
#define MENUITEM_THERMISTOR_START 150
unsigned long lastProcessedReflowState = 0; unsigned long lastProcessedReflowState = 0;
OledDisplay::OledDisplay() OledDisplay::OledDisplay()
@ -25,15 +26,21 @@ void OledDisplay::handleButtonStateChange(Pair<ButtonKind, StateChangeEvent<Butt
{ {
curMenu = selectedMenu; curMenu = selectedMenu;
} }
} else if (change.first == ButtonKind::BACK) { }
else if (change.first == ButtonKind::BACK)
{
OledMenu *selectedMenu = curMenu->parent; OledMenu *selectedMenu = curMenu->parent;
if (selectedMenu != NULL) if (selectedMenu != NULL)
{ {
curMenu = selectedMenu; curMenu = selectedMenu;
} }
} else if (change.first == ButtonKind::UP) { }
else if (change.first == ButtonKind::UP)
{
curMenu->goNextItem(); curMenu->goNextItem();
} else if (change.first == ButtonKind::DOWN){ }
else if (change.first == ButtonKind::DOWN)
{
curMenu->goPrevItem(); curMenu->goPrevItem();
} }
} }
@ -42,40 +49,47 @@ void OledDisplay::setup()
{ {
curMenu = new OledMenu(); curMenu = new OledMenu();
curMenu->setElements(new OledMenuItem[3]{ curMenu->setElements(new OledMenuItem[3]{
OledMenuItem("Reflow\0"), OledMenuItem("Reflow\0"),
OledMenuItem("PlateR\0"), OledMenuItem("PlateR\0"),
OledMenuItem("Temps\0"), OledMenuItem("Temps\0"),
}, 3); },
3);
OledMenu *pickProfilesMenu = new OledMenu(1); OledMenu *pickProfilesMenu = new OledMenu(1);
OledMenuItem *pickProfilesMenuItems = new OledMenuItem[nReflowProfiles]; OledMenuItem *pickProfilesMenuItems = new OledMenuItem[nReflowProfiles];
for (int i=0; i<nReflowProfiles; i++) { for (int i = 0; i < nReflowProfiles; i++)
pickProfilesMenuItems[i] = OledMenuItem(reflowProfiles[i].name, 100+1); {
pickProfilesMenuItems[i] = OledMenuItem(reflowProfiles[i].name, 100 + 1);
} }
pickProfilesMenu->setElements(pickProfilesMenuItems, nReflowProfiles); pickProfilesMenu->setElements(pickProfilesMenuItems, nReflowProfiles);
OledMenu *plateRMenu = new OledMenu(MENUID_PLATERES); OledMenu *plateRMenu = new OledMenu(MENUID_PLATERES);
OledMenu *tempsMenu = new OledMenu(3); OledMenu *tempsMenu = new OledMenu(3);
tempsMenu->setElements(new OledMenuItem[6]{ tempsMenu->setElements(new OledMenuItem[7]{
OledMenuItem("T1\0", 150), OledMenuItem("ALL\0", MENUITEM_THERMISTOR_START + 6),
OledMenuItem("T2\0", 151), OledMenuItem("T1\0", MENUITEM_THERMISTOR_START + 0),
OledMenuItem("T3\0", 152), OledMenuItem("T2\0", MENUITEM_THERMISTOR_START + 1),
OledMenuItem("T4\0", 153), OledMenuItem("T3\0", MENUITEM_THERMISTOR_START + 2),
OledMenuItem("T5\0", 154), OledMenuItem("T4\0", MENUITEM_THERMISTOR_START + 3),
OledMenuItem("T6\0", 155), OledMenuItem("T5\0", MENUITEM_THERMISTOR_START + 4),
}, 6); OledMenuItem("T6\0", MENUITEM_THERMISTOR_START + 5),
},
curMenu->setChildren(new OledMenu*[3]{ 6);
pickProfilesMenu,
plateRMenu, curMenu->setChildren(
tempsMenu, new OledMenu *[3]
}, 3); {
pickProfilesMenu,
plateRMenu,
tempsMenu,
},
3);
curMenu->setChildrenMatrix(3, new uint8_t[3][2]{ curMenu->setChildrenMatrix(3, new uint8_t[3][2]{
{0, 0}, {0, 0},
{1, 1}, {1, 1},
{2, 2}, {2, 2},
}); });
// curItem = 0; // ROOT // curItem = 0; // ROOT
// Setup implementation // Setup implementation
@ -99,26 +113,36 @@ void OledDisplay::loop()
if (state == USER_INPUT) if (state == USER_INPUT)
{ {
display.clearDisplay(); display.clearDisplay();
display.setRotation(0);
display.setTextSize(2); display.setTextSize(2);
OledMenuItem menuItem = curMenu->getCurItem(); OledMenuItem menuItem = curMenu->getCurItem();
display.setTextSize(2); display.setTextSize(2);
if (curMenu->identifier == MENUID_PLATERES) { if (menuItem.identifier >= MENUITEM_THERMISTOR_START && menuItem.identifier < MENUITEM_THERMISTOR_START + 7)
{
} else { 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. // 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(); display.display();
} }
// Loop implementation // Loop implementation
@ -145,13 +169,20 @@ void OledDisplay::drawDebug()
display.println("C°: " + String(thermistor1Temp)); display.println("C°: " + String(thermistor1Temp));
display.display(); 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) void OledDisplay::centerText(const char *txt)
{ {
int16_t x1, y1; int16_t x1, y1;
uint16_t w, h; uint16_t w, h;
display.getTextBounds(txt, 0, 0, &x1, &y1, &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); display.println(txt);
} }

View File

@ -20,6 +20,7 @@ class OledDisplay {
void handleReflowState(); void handleReflowState();
void centerText(const char * text); void centerText(const char * text);
void displayIndicators();
}; };

36
src/globals.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "./globals.h"
#include "./EEPROMDataManager.h"
WrappedState<ReflowProcessState> reflowProcessState = WrappedState<ReflowProcessState>(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();

View File

@ -3,6 +3,7 @@
#include "./common.h" #include "./common.h"
#include "./thermistors/Thermistor.h" #include "./thermistors/Thermistor.h"
#include "./reflow.h" #include "./reflow.h"
#include "./EEPROMDataManager.h"
extern WrappedState<ReflowProcessState> reflowProcessState; extern WrappedState<ReflowProcessState> reflowProcessState;
extern AnalogRef analogRef; extern AnalogRef analogRef;
@ -12,6 +13,13 @@ extern Thermistor thermistor3;
extern Thermistor thermistor4; extern Thermistor thermistor4;
extern Thermistor thermistor5; extern Thermistor thermistor5;
extern Thermistor thermistor6; extern Thermistor thermistor6;
extern Thermistor thermistors[6];
extern ReflowProfile reflowProfiles[]; extern ReflowProfile reflowProfiles[];
extern uint16_t plateResistanceOhm;
// EEPROM data manager is in its own file
extern int nReflowProfiles; extern int nReflowProfiles;
extern EEPROMDataManager eepromDataManager;
#endif #endif

View File

@ -10,14 +10,7 @@
#include "reflow.h" #include "reflow.h"
#include "displays/oled.h" #include "displays/oled.h"
#include "globals.h" #include "globals.h"
#include "EEPROMDataManager.h"
WrappedState<ReflowProcessState> reflowProcessState = WrappedState<ReflowProcessState>(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};
// LCD display pins // LCD display pins
#define TFT_CS 7 #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); 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(); Buttons buttons = Buttons();
LEDS leds = LEDS(); LEDS leds = LEDS();
// Declare the PID // Declare the PID
ArduPID PID; ArduPID PID;
OledDisplay oled = OledDisplay(); 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() void setup()
{ {
@ -72,6 +40,8 @@ void setup()
buttons.setup(); buttons.setup();
leds.setup(); leds.setup();
oled.setup(); oled.setup();
eepromDataManager.setup();
reflowProcessState = USER_INPUT; reflowProcessState = USER_INPUT;
} }

View File

@ -1,31 +1,12 @@
#include "Thermistor.h" #include "Thermistor.h"
Thermistor::Thermistor()
{
}
Thermistor::Thermistor(uint8_t pin, float resistance, TempCalibration calibration) void Thermistor::setPotentiometerResistance(float resistance)
{ {
thermistorPin = pin;
setRes = resistance; setRes = resistance;
calculateCoefficents(resistance, calibration); calculateCoefficents(resistance, calibration);
} }
Thermistor::Thermistor(uint8_t pin, float resistance)
{
thermistorPin = pin;
setRes = resistance;
calculateCoefficents(resistance, calibration_100K_3950);
}
Thermistor::~Thermistor()
{
}
int Thermistor::getTemperature() int Thermistor::getTemperature()
{ {
@ -33,7 +14,7 @@ int Thermistor::getTemperature()
int temp = 0; int temp = 0;
uint8_t samples = 5; uint8_t samples = 5;
for (int i = 0; i < samples; i++) for (int i = 0; i < samples; i++)
{ {
getResistance(); getResistance();
@ -82,7 +63,7 @@ float Thermistor::getResistance()
float vOut = (buffer) / 1023; float vOut = (buffer) / 1023;
// Serial.println(vOut); // Serial.println(vOut);
buffer = (systemVoltage / vOut) - 1; buffer = (systemVoltage / vOut) - 1;
// return the resistence // return the resistence
sensorResistance = setRes * buffer; sensorResistance = setRes * buffer;

View File

@ -41,18 +41,21 @@ public:
// Constructor // Constructor
Thermistor(); 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); Thermistor(uint8_t pin, float resistance): thermistorPin(pin), setRes(resistance), calibration(calibration_100K_3950) {
calculateCoefficents(resistance, calibration);
// Destructor }
~Thermistor();
// Public Methods // Public Methods
int getTemperature(); int getTemperature();
float getResistance(); float getResistance();
void setPotentiometerResistance(float resistance);
// Public Variables // Public Variables
void calculateCoefficents(float resistance, TempCalibration calibration);
private: private:
const double K = 273.15; const double K = 273.15;
@ -61,7 +64,7 @@ private:
float setRes; float setRes;
Coefficents coefficents; Coefficents coefficents;
float referenceResistance; float referenceResistance;
void calculateCoefficents(float resistance, TempCalibration calibration); TempCalibration calibration;
}; };
#endif // THERMISTOR_H #endif // THERMISTOR_H