-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolutionWarmer.ino
460 lines (390 loc) · 12.8 KB
/
SolutionWarmer.ino
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <OneWire.h>
#include <FastPID.h>
#include <stdlib.h>
#include "Eve2_81x.h"
#include "MatrixEve2Conf.h" // Header for EVE2 Display configuration settings
#include "process.h"
#include "Arduino_AL.h"
File myFile;
char LogBuf[WorkBuffSz];
uint8_t OWTP_addr[NumProbes][8]; // Buffer to store One wire Address
bool OWTP_Plate_Connected; // Bool to indicate if the DS18S20 is connected
bool OWTP_Solution_Connected; // Bool to indicate if the DS18S20 is connected
// These constructors and the abstractions below for one-wire and PID are perforce
// in this file due to the fact that the arduino compiler expects C++ and this .ino
// file is the only actual C++ file in the project. Attempting to do this in
// process.c where they are actually desired will result in compiler errors.
// You might think of this file as providing a translation between C++ and C.
//
// Both of these libraries are available via the "Manage Libraries" function under
// "Sketch/Include Library". Find and install each.
// https://github.com/PaulStoffregen/OneWire // Reference code for OneWire
// https://github.com/mike-matera/FastPID // Reference code for FastPID
OneWire OWTP(OneWire_PIN);
FastPID PID_Heater(10, 0.0025, 40, 0.2, 8, false); // FastPID(Kp, Ki, Kd, Hz, output_bits, output_signed);
FastPID PID_Load(12.0, 0.0013, 0.0, 0.0625, 16, false);
void setup()
{
// Initializations. Order is important
GlobalInit();
FT81x_Init();
SD_Init();
OWTP.reset_search(); // Reset the one-wire bus to search from address zero
// One wire initialization of probes
if ( !searchTempProbe(OWTP_Solution))
{
// Log("OWTP_Solution not found");
while(1); // We can not operate without successful probe interaction
}
if (!searchTempProbe(OWTP_Plate))
{
// Log("OWTP_Plate not found");
while(1); // We can not operate without successful probe interaction
}
if (!LoadTouchMatrix())
{
// We failed to read calibration matrix values from the SD card.
Calibrate_Manual(DWIDTH, DHEIGHT, PIXVOFFSET, PIXHOFFSET);
SaveTouchMatrix(); // Save to flash
LoadTouchMatrix(); // reload from flash to compare values
}
// Load_JPG(RAM_G, 0, "MainScr.jpg"); // Preload background jpg image into Eve GRAM
Cmd_SetRotate(1); // Rotate the display
wr8(REG_PWM_DUTY + RAM_REG, 128); // set backlight
if(SD.exists("pidlog.txt"))
SD.remove("pidlog.txt");
SetupMainScreen();
MainLoop(); // jump to "main()"
}
// MainLoop is called from setup() and it never leaves (which is better than loop() which is called repeatedly)
void MainLoop(void)
{
while(1)
{
CheckScreen(); // Update the screen at an effective rate
CheckSensors(); // Read the sensors at the correct rate
CheckSolution(); // Check the sensor data and update TimeTillCat value
CheckHeater(); // Run PID loop for heater
CheckTouch(); // Check for user touching and update values (blocks on touch)
}
}
// ************************************************************************************
// Following are wrapper functions for C++ Arduino functions so that they may be *
// called from outside of C++ files. These are also your opportunity to use a common *
// name for your hardware functions - no matter the hardware. In Arduino-world you *
// interact with hardware using Arduino built-in functions which are all C++ and so *
// your "abstraction layer" must live in this xxx.ino file where C++ works. *
// *
// This is also an alternative to ifdef-elif hell. A different target *
// processor or compiler will include different files for hardware abstraction, but *
// the core "library" files remain unaltered - and clean. Applications built on top *
// of the libraries need not know which processor or compiler they are running / *
// compiling on (in general and within reason) *
// ************************************************************************************
void GlobalInit(void)
{
Wire.begin(); // Setup I2C bus
Serial.begin(115200); // Setup serial port for debug
while (!Serial) {;} // wait for serial port to connect.
// Matrix Orbital Eve display interface initialization
pinMode(EvePDN_PIN, OUTPUT); // Pin setup as output for Eve PDN pin.
SetPin(EvePDN_PIN, 0); // Apply a resetish condition on Eve
pinMode(EveChipSelect_PIN, OUTPUT); // SPI CS Initialization
SetPin(EveChipSelect_PIN, 1); // Deselect Eve
pinMode(EveAudioEnable_PIN, OUTPUT); // Audio Enable PIN
SetPin(EveAudioEnable_PIN, 0); // Disable Audio
pinMode(ControlOutput_PIN, OUTPUT); // Pin setup as output for the software PWM pin controlling the heater
SetPin(ControlOutput_PIN, 0); // Turn that heater OFF!
SPI.begin(); // Enable SPI
// Log("Startup\n");
}
// Send a single byte through SPI
void SPI_WriteByte(uint8_t data)
{
SPI.beginTransaction(SPISettings(SPISpeed, MSBFIRST, SPI_MODE0));
digitalWrite(EveChipSelect_PIN, LOW);
SPI.transfer(data);
digitalWrite(EveChipSelect_PIN, HIGH);
SPI.endTransaction();
}
// Send a series of bytes (contents of a buffer) through SPI
void SPI_WriteBuffer(uint8_t *Buffer, uint32_t Length)
{
SPI.beginTransaction(SPISettings(SPISpeed, MSBFIRST, SPI_MODE0));
digitalWrite(EveChipSelect_PIN, LOW);
SPI.transfer(Buffer, Length);
digitalWrite(EveChipSelect_PIN, HIGH);
SPI.endTransaction();
}
// Send a byte through SPI as part of a larger transmission. Does not enable/disable SPI CS
void SPI_Write(uint8_t data)
{
// Log("W-0x%02x\n", data);
SPI.transfer(data);
}
// Read a series of bytes from SPI and store them in a buffer
void SPI_ReadBuffer(uint8_t *Buffer, uint32_t Length)
{
uint8_t a = SPI.transfer(0x00); // dummy read
while (Length--)
{
*(Buffer++) = SPI.transfer(0x00);
}
}
// Enable SPI by activating chip select line
void SPI_Enable(void)
{
SPI.beginTransaction(SPISettings(SPISpeed, MSBFIRST, SPI_MODE0));
digitalWrite(EveChipSelect_PIN, LOW);
}
// Disable SPI by deasserting the chip select line
void SPI_Disable(void)
{
digitalWrite(EveChipSelect_PIN, HIGH);
SPI.endTransaction();
}
void Eve_Reset_HW(void)
{
// Reset Eve
SetPin(EvePDN_PIN, 0); // Set the Eve PDN pin low
MyDelay(50); // delay
SetPin(EvePDN_PIN, 1); // Set the Eve PDN pin high
MyDelay(100); // delay
}
void DebugPrint(char *str)
{
Serial.print(str);
}
// A millisecond delay wrapper for the Arduino function
void MyDelay(uint32_t DLY)
{
uint32_t wait;
wait = millis() + DLY; while(millis() < wait);
}
// Externally accessible abstraction for millis()
uint32_t MyMillis(void)
{
return millis();
}
// An abstracted pin write that may be called from outside this file.
void SetPin(uint8_t pin, bool state)
{
digitalWrite(pin, state);
}
// An abstracted pin read that may be called from outside this file.
uint8_t ReadPin(uint8_t pin)
{
return(digitalRead(pin));
}
//================================== Fast-PID Functions ====================================
uint8_t PID_Heater_Step(uint16_t SetPoint, uint16_t CurrentVal)
{
uint8_t tmp = PID_Heater.step(SetPoint, CurrentVal);
return ( tmp );
}
uint16_t PID_Load_Step(uint16_t SetPoint, uint16_t CurrentVal)
{
uint16_t tmp;
tmp = PID_Load.step(SetPoint, CurrentVal);
return ( tmp );
}
void PID_ClearAll(void)
{
PID_Heater.clear();
PID_Load.clear();
}
void PID_Load_SetRange(uint16_t Lowend, uint16_t Highend)
{
PID_Load.setOutputRange(Lowend, Highend); // Set the solution PID to demand temperatures between low and high
}
//================================== One-Wire Functions ====================================
int8_t searchTempProbe(uint8_t ProbeNum)
{
if ( !OWTP.search(OWTP_addr[ProbeNum])) {
// Log("No more probes found\n");
return 0;
}
if ( OneWire::crc8( OWTP_addr[ProbeNum], 7) != OWTP_addr[ProbeNum][7]) {
// Log("CRC Error\n");
return 0;
}
if ( OWTP_addr[ProbeNum][0] == 0x10) {
// Log("DS18S20 Probe\n");
return 1;
}
else
{
// Log("One-wire device not recognised\n");
return 0;
}
}
int16_t readTempProbe(uint8_t ProbeNum)
{
uint8_t data[12];
float temp;
OWTP.reset();
OWTP.select(OWTP_addr[ProbeNum]);
OWTP.write(0xBE); // read scratchpad command
for ( int8_t i = 0; i < 9; i++) // get 9 bytes of data from probe
data[i] = OWTP.read();
temp = (data[1] << 8) + data[0];
// Start the next conversion - be sure to not read for at least 750mS
OWTP.reset();
OWTP.select(OWTP_addr[ProbeNum]);
OWTP.write(0x44, 0); // start conversion then release the bus (0)
return temp;
}
//================================== SD Card Functions ====================================
void SD_Init(void)
{
// Log("Initializing SD card...\n");
if (!SD.begin(SDChipSelect_PIN))
{
Log("SD initialization failed!\n");
return;
}
// Log("SD initialization done\n");
}
// Read the touch digitizer calibration matrix values from the Eve and write them to a file
void SaveTouchMatrix(void)
{
uint8_t count = 0;
uint32_t data;
uint32_t address = REG_TOUCH_TRANSFORM_A + RAM_REG;
// Log("Enter SaveTouchMatrix\n");
// If the file exists already from previous run, then delete it.
if(SD.exists("tmatrix.txt"))
{
SD.remove("tmatrix.txt");
MyDelay(50);
}
FileOpen("tmatrix.txt", FILEWRITE);
if(!myFileIsOpen())
{
Log("No create file\n");
FileClose();
return false;
}
do
{
data = rd32(address + (count * 4));
Log("TM%dw: 0x%08lx\n", count, data);
FileWrite(data & 0xff); // Little endian file storage to match Eve
FileWrite((data >> 8) & 0xff);
FileWrite((data >> 16) & 0xff);
FileWrite((data >> 24) & 0xff);
count++;
}while(count < 6);
FileClose();
Log("Matrix Saved\n\n");
}
// Read the touch digitizer calibration matrix values from a file and write them to the Eve.
bool LoadTouchMatrix(void)
{
uint8_t count = 0;
uint32_t data;
uint32_t address = REG_TOUCH_TRANSFORM_A + RAM_REG;
FileOpen("tmatrix.txt", FILEREAD);
if(!myFileIsOpen())
{
Log("tmatrix.txt not open\n");
FileClose();
return false;
}
do
{
data = FileReadByte() + ((uint32_t)FileReadByte() << 8) + ((uint32_t)FileReadByte() << 16) + ((uint32_t)FileReadByte() << 24);
Log("TM%dr: 0x%08lx\n", count, data);
wr32(address + (count * 4), data);
count++;
}while(count < 6);
FileClose();
Log("Matrix Loaded \n\n");
return true;
}
// ************************************************************************************
// Following are abstracted file operations for Arduino. This is possible by using a *
// global pointer to a single file. It is enough for our needs and it hides file *
// handling details within the abstraction. *
// ************************************************************************************
void FileOpen(char *filename, uint8_t mode)
{
// Since one also loses access to defined values like FILE_READ from outside the .ino
// I have been forced to make up values and pass them here (mode) where I can use the
// Arduino defines.
switch(mode)
{
case FILEREAD:
myFile = SD.open(filename, FILE_READ);
break;
case FILEWRITE:
myFile = SD.open(filename, FILE_WRITE);
break;
default:;
}
}
void FileClose(void)
{
myFile.close();
if(myFileIsOpen())
{
Log("Failed to close file\n");
}
}
// Read a single byte from a file
uint8_t FileReadByte(void)
{
return(myFile.read());
}
// Read bytes from a file into a provided buffer
void FileReadBuf(uint8_t *data, uint32_t NumBytes)
{
myFile.read(data, NumBytes);
}
void FileWrite(uint8_t data)
{
myFile.write(data);
}
// Write a string of characters to a file
// MaxChars does not include the null terminator of the source string.
// We make no attempt to detect the usage of MaxChars and simply truncate the output
void FileWriteStr(uint8_t *str, uint16_t MaxChars)
{
int16_t count = 0;
FileOpen("pidlog.txt", FILEWRITE);
if(!myFileIsOpen())
{
Log("No file\n");
FileClose();
return;
}
// Write out the string until the terminator or until we've written the max
while( (count < MaxChars) && str[count] )
{
FileWrite(str[count]);
count++;
}
FileClose();
}
uint32_t FileSize(void)
{
return(myFile.size());
}
uint32_t FilePosition(void)
{
return(myFile.position());
}
bool FileSeek(uint32_t offset)
{
return(myFile.seek(offset));
}
bool myFileIsOpen(void)
{
if(myFile)
return true;
else
return false;
}