aboutsummaryrefslogtreecommitdiffhomepage
path: root/workspace/ts100/src/Modes.c
blob: 6fc463fcb40253ce10787caa782af3fbaa5a29f6 (plain)
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
/*
 * Modes.c
 *
 *  Created on: 17 Sep 2016
 *      Author: Ralim <ralim@ralimtek.com>
 */
#include "Modes.h"
//This does the required processing and state changes
void ProcessUI() {
	uint8_t Buttons = getButtons(); //read the buttons status
	static uint32_t lastModeChange = 0;
	if (millis() - getLastButtonPress() < 30)
		Buttons = 0;
	else if (Buttons != 0) {
		resetLastButtonPress();
		resetButtons();
	}
	//rough prevention for de-bouncing and allocates settling time

	switch (operatingMode) {
	case STARTUP:
		if (Buttons == (BUT_A | BUT_B)) {
			operatingMode = THERMOMETER;
		} else if (Buttons == BUT_A) {
			//A key pressed so we are moving to soldering mode
			operatingMode = SOLDERING;
		} else if (Buttons == BUT_B) {
			//B Button was pressed so we are moving to the Settings menu
			operatingMode = SETTINGS;
		}
		break;
	case SOLDERING:
		//We need to check the buttons if we need to jump out
		if (Buttons == BUT_A || Buttons == BUT_B) {
			//A or B key pressed so we are moving to temp set
			operatingMode = TEMP_ADJ;
		} else if (Buttons == (BUT_A | BUT_B)) {

			//Both buttons were pressed, exit back to the cooling screen
			operatingMode = COOLING;

		} else {
			//We need to check the timer for movement in case we need to goto idle
			if (systemSettings.movementEnabled)
				if (millis() - getLastMovement()
						> (systemSettings.SleepTime * 60000)) {
					if (millis() - getLastButtonPress()
							> (systemSettings.SleepTime * 60000)) {
						operatingMode = SLEEP;
						return;
					}
				}
			uint16_t voltage = readDCVoltage(); //get X10 voltage
			if ((voltage / 10) < systemSettings.cutoutVoltage) {
				operatingMode = UVLOWARN;
				lastModeChange = millis();
			}
			//Update the PID Loop
			int32_t newOutput = computePID(systemSettings.SolderingTemp);
			setIronTimer(newOutput);
		}
		break;
	case TEMP_ADJ:
		if (Buttons == BUT_A) {
			//A key pressed so we are moving down in temp

			if (systemSettings.SolderingTemp > 1000)
				systemSettings.SolderingTemp -= 100;
		} else if (Buttons == BUT_B) {
			//B key pressed so we are moving up in temp
			if (systemSettings.SolderingTemp < 4500)
				systemSettings.SolderingTemp += 100;
		} else {
			//we check the timeout for how long the buttons have not been pushed
			//if idle for > 3 seconds then we return to soldering
			//Or if both buttons pressed
			if ((millis() - getLastButtonPress() > 2000)
					|| Buttons == (BUT_A | BUT_B)) {
				operatingMode = SOLDERING;
				saveSettings();
			}
		}
		break;
	case SETTINGS:
		//Settings is the mode with the most logic
		//Here we are in the menu so we need to increment through the sub menus / increase the value

		if (Buttons & BUT_A) {
			//A key iterates through the menu
			if (settingsPage == SETTINGSOPTIONSCOUNT) {
				//Roll off the end
				settingsPage = 0;				//reset
				operatingMode = STARTUP;		//reset back to the startup
				saveSettings();					//Save the settings
			} else
				++settingsPage;					//move to the next option
		} else if (Buttons & BUT_B) {
			resetLastButtonPress();
			//B changes the value selected
			switch (settingsPage) {
			case UVCO:
				//we are incrementing the cutout voltage
				systemSettings.cutoutVoltage += 1;		//Go up 1V at a jump
				if (systemSettings.cutoutVoltage > 24)
					systemSettings.cutoutVoltage = 10;
				break;
			case SLEEP_TEMP:
				systemSettings.SleepTemp += 100;		//Go up 10c at a time
				if (systemSettings.SleepTemp > 3000)
					systemSettings.SleepTemp = 1000;//cant sleep higher than 300
				break;
			case SLEEP_TIME:
				++systemSettings.SleepTime;		//Go up 1 minute at a time
				if (systemSettings.SleepTime > 30)
					systemSettings.SleepTime = 1;	//cant set time over 30 mins
				//Remember that ^ is the time of no movement
				break;
			case MOTIONDETECT:
				systemSettings.movementEnabled =
						!systemSettings.movementEnabled;
				break;
			case TEMPDISPLAY:
				systemSettings.displayTempInF = !systemSettings.displayTempInF;
				break;
			case LEFTY:
				systemSettings.flipDisplay = !systemSettings.flipDisplay;
				break;
			case MOTIONSENSITIVITY:
				systemSettings.sensitivity += 0x10;
				if (systemSettings.sensitivity > 0x20)
					systemSettings.sensitivity = 0;		//reset to high on wrap

				break;
			default:
				break;
			}
		}
		break;
	case SLEEP:
		//The iron is sleeping at a lower temperature due to lack of movement
		if (Buttons & BUT_A) {
			//A Button was pressed so we are moving back to soldering
			operatingMode = SOLDERING;
			return;
		} else if (Buttons & BUT_B) {
			//B Button was pressed so we are moving back to soldering
			operatingMode = SOLDERING;
			return;
		} else if (systemSettings.movementEnabled)
			if (millis() - getLastMovement() < 1000) {//moved in the last second
				operatingMode = SOLDERING; //Goto active mode again
				return;
			}
		//else if nothing has been pushed we need to compute the PID to keep the iron at the sleep temp
		int32_t newOutput = computePID(systemSettings.SleepTemp);
		setIronTimer(newOutput);
		break;
	case COOLING: {
		setIronTimer(0); //turn off heating
		//This mode warns the user the iron is still cooling down
		uint16_t temp = readIronTemp(0, 1); //take a new reading as the heater code is not taking new readings
		if (temp < 400) { //if the temp is < 40C then we can go back to IDLE
			operatingMode = STARTUP;
		} else if (Buttons & (BUT_A | BUT_B)) { //we check if the user has pushed a button to ack
			//Either button was pushed
			operatingMode = STARTUP;
		}
	}
		break;
	case UVLOWARN:
		//We are here if the DC voltage went too low
		//We want to jump back to IDLE after a bit
		if (millis() - lastModeChange > 3000) {		//its been 3 seconds
			operatingMode = STARTUP;		//jump back to idle mode
		}
		break;
	case THERMOMETER: {
		//This lets the user check the tip temp without heating the iron.. And eventually calibration will be added here

		if ((Buttons == BUT_A) | (Buttons == BUT_B)) {
			//Single button press, cycle over to the DC display
			operatingMode = DCINDISP;
		} else if (Buttons == (BUT_A | BUT_B)) {
			//If the user is holding both button, exit the  screen
			operatingMode = STARTUP;
		}

	}
		break;
	case DCINDISP: {
		//This lets the user check the input voltage

		if ((Buttons == BUT_A) | (Buttons == BUT_B)) {
			//Single button press, cycle over to the temp display
			operatingMode = THERMOMETER;
		} else if (Buttons == (BUT_A | BUT_B)) {
			//If the user is holding both button, exit the  screen
			operatingMode = STARTUP;
		}

	}
		break;
	default:
		break;
	}
}
/*
 * Draws the temp with temp conversion if needed
 */
void drawTemp(uint16_t temp, uint8_t x) {
	if (systemSettings.displayTempInF)
		temp = (temp * 9 + 1600) / 5;/*Convert to F -> T*(9/5)+32*/
	OLED_DrawThreeNumber(temp / 10, x);
}

/*
 * Performs all the OLED drawing for the current operating mode
 */
void DrawUI() {
	uint16_t temp = readIronTemp(0, 0);
	switch (operatingMode) {
	case STARTUP:
		//We are chilling in the idle mode
		//Check if movement in the last 5 minutes , if not sleep OLED
		if (millis() - getLastMovement() > (5 * 60 * 1000)
				&& (millis() - getLastButtonPress() > (5 * 60 * 1000))) {
			//OLED off
			Oled_DisplayOff();
		} else {
			Oled_DisplayOn();
			OLED_DrawString("  IDLE  ", 8);		//write the word IDLE
		}
		break;
	case SOLDERING:
		//The user is soldering
	{
		if (getIronTimer() == 0) {
			OLED_DrawChar('C', 5);
		} else {
			if (getIronTimer() < 900) {
				OLED_DrawChar(' ', 5);
			} else {		//we are heating
				OLED_DrawChar('H', 5);
			}
		}
		drawTemp(temp, 0);
		OLED_DrawChar(' ', 3);
		OLED_DrawChar(' ', 4);
		OLED_DrawChar(' ', 6);
		OLED_DrawChar(' ', 7);

	}
		break;
	case TEMP_ADJ:
		//We are prompting the user to change the temp so we draw the current setpoint temp
		//With the nifty arrows

		OLED_DrawChar(' ', 0);
		OLED_DrawChar('<', 1);
		drawTemp(systemSettings.SolderingTemp, 2);
		OLED_DrawChar(' ', 5);
		OLED_DrawChar(' ', 6);
		OLED_DrawChar('>', 7);

		break;
	case SETTINGS:
		//We are prompting the user the setting name

		switch (settingsPage) {
		case UVCO:
			OLED_DrawString("UVCO ", 5);
			OLED_DrawTwoNumber(systemSettings.cutoutVoltage, 5);
			OLED_DrawChar('V', 7);
			break;
		case SLEEP_TEMP:
			OLED_DrawString("STMP ", 5);
			OLED_DrawThreeNumber(systemSettings.SleepTemp / 10, 5);
			break;
		case SLEEP_TIME:
			OLED_DrawString("STIME ", 6);
			OLED_DrawTwoNumber(systemSettings.SleepTime, 6);
			break;
		case MOTIONDETECT:/*Toggle the mode*/
			if (systemSettings.movementEnabled)
				OLED_DrawString("MOTION T", 8);
			else
				OLED_DrawString("MOTION F", 8);
			break;
		case TEMPDISPLAY:/*Are we showing in C or F ?*/
			if (systemSettings.displayTempInF)
				OLED_DrawString("TMPUNT F", 8);
			else
				OLED_DrawString("TMPUNT C", 8);
			break;

		case LEFTY:

			if (systemSettings.flipDisplay)
				OLED_DrawString("FLPDSP T", 8);
			else
				OLED_DrawString("FLPDSP F", 8);
			break;
		case MOTIONSENSITIVITY:
			switch (systemSettings.sensitivity) {
			case MOTION_HIGH:
				OLED_DrawString("SENSE H ", 8);
				break;
			case MOTION_MED:
				OLED_DrawString("SENSE M ", 8);
				break;
			case MOTION_LOW:
				OLED_DrawString("SENSE L ", 8);
				break;
			default:
				OLED_DrawString("SENSE   ", 8);
				break;
			}

			break;
		default:
			break;
		}
		break;
	case SLEEP:
		//The iron is in sleep temp mode
		//Draw in temp and sleep
		OLED_DrawString("SLP", 3);
		drawTemp(temp, 4);
		break;
	case COOLING:
		//We are warning the user the tip is cooling
		OLED_DrawString("COOL", 4);
		drawTemp(temp, 5);
		break;
	case UVLOWARN:
		OLED_DrawString("LOW VOLT", 8);
		break;
	case THERMOMETER:
		temp = readIronTemp(0, 1);		//Force a reading as heater is off
		OLED_DrawString("TEMP ", 5);//extra one to it clears the leftover 'L' from IDLE
		drawTemp(temp, 5);
		break;
	case DCINDISP: {
		uint16_t voltage = readDCVoltage(); //get X10 voltage
		OLED_DrawString("IN", 2);
		OLED_DrawChar((voltage / 100) % 10, 2);
		voltage -= (voltage / 100) * 100;
		OLED_DrawChar((voltage / 10) % 10, 3);
		voltage -= (voltage / 10) * 10;
		OLED_DrawChar('.', 4);
		OLED_DrawChar(voltage % 10, 5);
		OLED_DrawChar('V', 6);
		OLED_DrawChar(' ', 7);

	}
		break;
	default:
		break;
	}
}