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 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(Pair<ButtonKind, StateChangeEvent<Butt
{
curMenu = selectedMenu;
}
} else if (change.first == ButtonKind::BACK) {
}
else if (change.first == ButtonKind::BACK)
{
OledMenu *selectedMenu = curMenu->parent;
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; i<nReflowProfiles; i++) {
pickProfilesMenuItems[i] = OledMenuItem(reflowProfiles[i].name, 100+1);
for (int i = 0; i < nReflowProfiles; i++)
{
pickProfilesMenuItems[i] = OledMenuItem(reflowProfiles[i].name, 100 + 1);
}
pickProfilesMenu->setElements(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);
}

View File

@ -20,6 +20,7 @@ class OledDisplay {
void handleReflowState();
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 "./thermistors/Thermistor.h"
#include "./reflow.h"
#include "./EEPROMDataManager.h"
extern WrappedState<ReflowProcessState> 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

View File

@ -10,14 +10,7 @@
#include "reflow.h"
#include "displays/oled.h"
#include "globals.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};
#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;
}

View File

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

View File

@ -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