Repository navigation
Expand file tree
/
Copy pathYamuraPyrometer.h
More file actions
287 lines (275 loc) · 8.42 KB
/
Copy pathYamuraPyrometer.h
File metadata and controls
287 lines (275 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
YamuraLog Recording Tire Pyrometer
By: Brian Smith
Yamura Electronics Division
Date: May 2024
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware License).
*/
// select 1 RTC library. Modify RTC_ functions as needed
#define HAS_RTC
//#define RTC_8563 // using 8563 RTC
#define RTC_3231 // using 3231 RTC
// select 1 Thermocouple amp library. Modify Thermo_ functions as needed
//#define THERMO_MCP9600 // using MPC9600 thermocouple amp
#define THERMO_MCP9601 // using MPC9601 thermocouple amp
//
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ElegantOTA.h> //
#include <ESPAsyncWebServer.h>
#include <TFT_eSPI.h> // https://github.com/Bodmer/TFT_eSPI Graphics and font library for ST7735 driver chip
#include "Free_Fonts.h" // Include the header file attached to this sketch
#include "FS.h"
#include "LittleFS.h"
#include "SD.h"
#include "SPI.h"
#include <ESP32Time.h>
// install correct thermocouple library
#ifdef THERMO_MCP9600
//#include <SparkFun_MCP9600.h> // MPC9600 Thermocouple library https://github.com/sparkfun/SparkFun_MCP9600_Arduino_Library
#include <Adafruit_MCP9600.h>
#endif
#ifdef THERMO_MCP9601
#include <Adafruit_MCP9601.h>
#endif
// install correct RTC library
#include "RTClib.h" // PCF8563 RTC library https://github.com/adafruit/RTClib
// uncomment for debug to serial monitor (use #ifdef...#endif around debug output prints)
//#define DEBUG_VERBOSE
//#define DEBUG_EXTRA_VERBOSE
//#define DEBUG_HTML
//#define SET_TO_SYSTEM_TIME
// microSD chip reader select
#define SD_CS 5
// I2C pins
#define I2C_SDA 21
#define I2C_SCL 22
// max menu item count
#define MAX_MENU_ITEMS 100
// main menu
#define DISPLAY_MENU 0
#define SELECT_CAR 1
#define MEASURE_TIRES 2
#define DISPLAY_TIRES 3
#define DISPLAY_SELECTED_RESULT 4
#define CHANGE_SETTINGS 5
#define INSTANT_TEMP 6
#define TEST_MENU 7
// settings menu
#define SET_DATETIME 0
#define SET_TEMPUNITS 1
#define SET_FLIPDISPLAY 2
#define SET_FONTSIZE 3
#define SET_12H24H 4
#define SET_STABLEBAND 5
#define SET_STABLEDELAY 6
#define SET_STABLEBUFFER 7
#define SET_DELETEDATA 8
#define SET_IPADDRESS 9
#define SET_PASS 10
#define SET_SAVESETTINGS 11
#define SET_EXIT 12
// font size menu
#define FONTSIZE_9 0
#define FONTSIZE_12 1
#define FONTSIZE_18 2
#define FONTSIZE_24 3
// 12/24 hour menu
#define HOURS_12 0
#define HOURS_24 1
// index to date/time value array
#define DATE 0
#define MONTH 1
#define YEAR 2
#define DAYOFWEEK 3
#define HOUR 4
#define MINUTE 5
#define SECOND 6
#define HUNDSEC 7
// user inputs
#define BUTTON_COUNT 3
#define BUTTON_1 0
#define BUTTON_2 1
#define BUTTON_3 2
#define BUTTON_RELEASED 0
#define BUTTON_PRESSED 1
#define BUTTON_DEBOUNCE_DELAY 20 // ms
#define FORMAT_LITTLEFS_IF_FAILED true
#define PIN_UP 14 // up
#define PIN_DOWN 12 // down
#define PIN_SELECT 26 // select
// car info structure
struct CarSettings
{
int carID;
char carName[64];
char dateTime[32];
int tireCount;
char tireShortName[6][16];
char tireLongName[6][32];
int positionCount;
char positionShortName[6][16];
char positionLongName[3][32];
float maxTemp[6];
};
struct MenuChoice
{
String description;
int result;
};
// device settings structure
struct DeviceSettings
{
char ssid[32] = "Yamura-Pyrometer";
char pass[32] = "ZoeyDora48375";
int screenRotation = 1;
bool tempUnits = false; // true for C, false for F
bool is12Hour = true; // true for 12 hour clock, false for 24 hour clock
int fontPoints = 12; // size of font to use for display
float stableBand[2] = {-0.25, 0.25};
unsigned long stableDelay = 500;
int stableBuffer = 10;
};
// button debounce structure
struct UserButton
{
int buttonPin = 0;
bool buttonReleased = false;
bool buttonPressed = false;
byte buttonLast = BUTTON_RELEASED;
unsigned long pressDuration = 0;
unsigned long releaseDuration = 0;
unsigned long lastChange = 0;
};
// button structure list
UserButton buttons[BUTTON_COUNT];
// car list from setup file
CarSettings* cars;
// device settings from file
DeviceSettings deviceSettings;
// tire temp array - max of 6 tires, 3 readings per tire
float tireTemps[18];
float currentTemps[18];
// devices
// thermocouple amplifier
#ifdef THERMO_MCP9600
Adafruit_MCP9600 tempSensor;
#define I2C_ADDRESS_THERMO 0x67
#endif
#ifdef THERMO_MCP9601
Adafruit_MCP9601 tempSensor;
#define I2C_ADDRESS_THERMO 0x67
#endif
// temp values for stabilization calculation
float tempValues[100];
#ifdef RTC_8563
RTC_PCF8563 rtc;
#endif
#ifdef RTC_3231
RTC_DS3231 rtc;
#endif
bool century = false;
bool h12Flag;
bool pmFlag;
String days[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri","Sat"};
String ampmStr[3] = {"am", "pm", "\0"};
// for ESP32 internal clock (SD card timestamps)
ESP32Time espInternalRTC;
// TFT display
TFT_eSPI tftDisplay = TFT_eSPI();
int fontHeight;
int fontWidth;
int textPosition[2] = {5, 0};
int carCount = 0;
int maxCarID = 0;
int selectedCar = 0;
int carSetupIdx = 0;
int tireIdx = 0;
int measIdx = 0;
float tempRes = 1.0;
// initial state of pyrometer (show main menu)
int deviceState = 0;
// files list
String littleFSFileList[50];
String sdFileList[500];
// web server
IPAddress IP;
AsyncWebServer server(80);
//
// grid lines for temp measure/display
//
int gridLineH[4][2][2]; // 4 vertical lines, 2 points per line, 2 values per point (X and Y)
int gridLineV[3][2][2]; // 3 vertical lines, 2 points per line, 2 values per point (X and Y)
int cellPoint[7][6][2]; // 6 max rows, 6 points per cell, 2 values per point (X and Y)
// FUNCTION PROTOTYPES
// required
void setup();
void loop();
// user interface
// menu generators call MenuSelect with the list of selections and returned state
void MainMenu();
void SelectCarMenu();
void SelectedResultsMenu(fs::FS &fs, const char * path);
void ChangeSettingsMenu();
void Select12or24Menu();
void SelectFontSizeMenu();
void SetUnitsMenu();
void SetStableBandwidthMenu();
void SetStableDelayMenu();
void DeleteDataFilesMenu(bool verify = true);
int MenuSelect(int fontSize, MenuChoice choices[], int menuCount, int initialSelect);
// set date/time values
void SetDateTime();
// read, write, generate HTML for setup files and results
void ReadCarSetupFile(fs::FS &fs, const char * path);
void WriteCarSetupFile(fs::FS &fs, const char * path);
void WriteCarSetupHTML(fs::FS &fs, const char * path, int carIdx);
void ReadDeviceSetupFile(fs::FS &fs, const char * path);
void WriteDeviceSetupFile(fs::FS &fs, const char * path);
void WriteDeviceSetupHTML(fs::FS &fs, const char * path);
void WriteResultsHTML(fs::FS &fs);
void ReadMeasurementFile(char buf[], CarSettings ¤tResultCar);
void WriteMeasurementFile();
// measure and display tire temps, TFT specific functions
// single tire version of tire temp measure
void MeasureAllTireTemps();
int MeasureTireTemps(int tire); // measure single tire temps full screen
float GetStableTemp(int row, int col);
int GetNextTire(int selTire, int nextDirection);
// current probe temp
void InstantTemp();
// display results
void DrawTireMeasureGrid(int tireCount);
void SetupTireMeasureGrid(int fontHeight);
void DisplayAllTireTemps(CarSettings currentResultCar);
void DrawCellText(int row, int col, char* text, uint16_t textColor, uint16_t backColor);
// rotate display (right/left hand orientation)
void RotateDisplay(bool rotateButtons);
// set font size
void SetFont(int fontSize);
// banner
void YamuraBanner();
// 'common' functions defined in this file
// temperature functions
float CtoFAbsolute(float tempC);
float CtoFRelative(float tempC);
float FtoCAbsolute(float tempF);
float FtoCRelative(float tempF);
// date, time, RTC
bool RTC_IsPM();
String RTC_GetStringTime();
String RTC_GetStringDate();
DateTime RTC_DateTime();
void RTC_SetDateTime(DateTime timeVal);
void RTC_SetDateTime(int year, int month, int date, int hour, int minute, int second);
void Thermo_Setup();
float Thermo_GetTemp();
// user input (button presses)
void CheckButtons(unsigned long curTime);
// SD and LittleFS file handling
void ReadLine(File file, char* buf);
void AppendFile(fs::FS &fs, const char * path, const char * message);
void DeleteFile(fs::FS &fs, const char * path);
void ListDirectory(fs::FS &fs, const char * dirname, int &fileCount, String fileList[], uint8_t levels);
void CopyFile(fs::FS &fsSource, const char * pathSource, fs::FS &fsTarget, const char * pathTarget, bool moveFile = true);