aboutsummaryrefslogtreecommitdiff
path: root/ui.c
blob: e677075790f37a2569d66357f780f0b4ac8e443d (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
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
#include "common.h"

#include <util/delay.h>
#include <avr/sleep.h>
#include <stdio.h>
#include <stdlib.h>

#include "ui.h"
#include "accel.h"
#include "gyro.h"
#include "timer.h"
#include "pwm.h"

#define sign(x) ((x < 0) ? -1 : 1)
/* stop alarm after #seconds */
#define ALARM_TIME ((uint32_t) 30*1000*1000)

typedef enum {
	/* initialize */
	UIMODE_INIT,
	/* deep sleep */
	UIMODE_SLEEP,
	/* select time */
	UIMODE_SELECT_COARSE,
	UIMODE_SELECT_FINE,
	/* idle */
	UIMODE_IDLE,
	/* count time */
	UIMODE_RUN,
	/* alert */
	UIMODE_ALARM,
} uimode;

static uimode mode = UIMODE_INIT;
/* Selection values */
static signed char coarseValue = 0, fineValue = 0;
/* timer seconds (us) */
static uint32_t timerElapsed = 0, timerValue;
static uint8_t brightness[PWM_LED_COUNT];
static uint8_t currLed;
static horizon h = HORIZON_NONE;
static bool horizonChanged = false;

/*	Read sensor values
 */
static void processSensors () {
	static bool checkGyro = false;

	/* round-robin to prevent starvation */
	if (checkGyro) {
		gyroProcess ();
		accelProcess();
	} else {
		accelProcess ();
		gyroProcess ();
	}
	checkGyro = !checkGyro;
}

/*	Translate LED ids based on horizon, id 0 is always at the bottom of the
 *	device */
static uint8_t horizonLed (uint8_t i) {
	if (h == HORIZON_NEG) {
		return i;
	} else {
		return (PWM_LED_COUNT-1)-i;
	}
}

static int16_t limits (const int16_t in, const int16_t min, const int16_t max) {
	if (in < min) {
		return min;
	} else if (in > max) {
		return max;
	} else {
		return in;
	}
}

/*	Enter idle mode
 */
static void enterIdle () {
	mode = UIMODE_IDLE;

	pwmSet (horizonLed (0), 1);
	for (uint8_t i = 1; i < PWM_LED_COUNT; i++) {
		pwmSet (horizonLed (i), PWM_OFF);
	}
}

/*	Set value from fine selection and show with leds
 */
static void setFine (const int8_t value) {
	/* min timer value is 1 minute, disable subtract if coarse is below ten
	 * minutes */
	const int8_t bottomlimit = coarseValue == 0 ? 1 : -5;
	fineValue = limits(value, bottomlimit, 5);
	puts ("\nfineValue\n");
	fwrite (&fineValue, sizeof (fineValue), 1, stdout);

	/* from bottom to top for positive values, top to bottom for negative
	 * values */
	if (fineValue >= 0) {
		for (uint8_t i = 0; i < fineValue; i++) {
			pwmSet (horizonLed (i), PWM_ON);
		}
		for (uint8_t i = fineValue; i < PWM_LED_COUNT; i++) {
			pwmSet (horizonLed (i), PWM_OFF);
		}
	} else {
		for (uint8_t i = 0; i < abs (fineValue); i++) {
			pwmSet (horizonLed (PWM_LED_COUNT-1-i), PWM_ON);
		}
		for (uint8_t i = abs (fineValue); i < PWM_LED_COUNT; i++) {
			pwmSet (horizonLed (PWM_LED_COUNT-1-i), PWM_OFF);
		}
	}
}

/*	Coarse timer setting, selects from 0 to 60 minutes, in 10 min steps
 */
static void doSelectCoarse () {
	if (accelGetShakeCount () >= 2) {
		/* selection */
		accelResetShakeCount ();
		mode = UIMODE_SELECT_FINE;
		puts ("selectcoarse->selectfine");
		setFine (0);
		speakerStart (SPEAKER_BEEP);
		return;
	}

	/* use zticks as seconds */
	const int16_t zticks = gyroGetZTicks ();
	if (abs (zticks) > 0) {
		gyroResetZTicks ();
		coarseValue = limits(coarseValue + zticks, 0, 6);
		/* at least 1 min */
		fineValue = coarseValue == 0 ? 1 : 0;
		puts ("\ncoarseValue\n");
		fwrite (&coarseValue, sizeof (coarseValue), 1, stdout);

		for (uint8_t i = 0; i < coarseValue; i++) {
			pwmSet (horizonLed (i), PWM_ON);
		}
		for (uint8_t i = coarseValue; i < PWM_LED_COUNT; i++) {
			pwmSet (horizonLed (i), PWM_OFF);
		}
	}
}

/*	Fine timer setting, selects from -5 to 5 minutes, in 1 min steps
 */
static void doSelectFine () {
	if (accelGetShakeCount () >= 2) {
		/* stop selection */
		accelResetShakeCount ();
		speakerStart (SPEAKER_BEEP);
		gyroStop ();

		puts ("selectfine->idle");
		enterIdle ();
		return;
	}

	/* use zticks as seconds */
	const int16_t zticks = gyroGetZTicks ();
	if (abs (zticks) > 0) {
		gyroResetZTicks ();
		setFine (fineValue + zticks);
	}
}

/*	Idle function, waits for timer start or select commands
 */
static void doIdle () {
	if (horizonChanged) {
		/* start timer */
		for (uint8_t i = 0; i < PWM_LED_COUNT; i++) {
			brightness[i] = 0;
			pwmSet (horizonLed (i), PWM_OFF);
		}
		currLed = PWM_LED_COUNT-1;
		brightness[currLed] = PWM_MAX_BRIGHTNESS;
		pwmSet (horizonLed (currLed), brightness[currLed]);

		timerValue = coarseValue * (uint32_t) 10*60*1000*1000 +
				fineValue * (uint32_t) 60*1000*1000;
		puts ("\ntimerValue\n");
		fwrite (&timerValue, sizeof (timerValue), 1, stdout);
		timerElapsed = 0;
		/* (PWM_LED_COUNT-1)*PWM_MAX_BRIGHTNESS states; -1, since two leds’s
		 * states are interleaved */
		const uint32_t brightnessStep = timerValue/(uint32_t) ((PWM_LED_COUNT-1)*PWM_MAX_BRIGHTNESS);
		puts ("\nbrightnessStep\n");
		fwrite (&brightnessStep, sizeof (brightnessStep), 1, stdout);

		mode = UIMODE_RUN;
		timerStart (brightnessStep);
		puts ("idle->run");
		speakerStart (SPEAKER_BEEP);
	} else if (accelGetShakeCount () >= 2) {
		/* set timer */
		accelResetShakeCount ();
		gyroStart ();
		mode = UIMODE_SELECT_COARSE;
		puts ("idle->select");
		speakerStart (SPEAKER_BEEP);
		coarseValue = 0;
		return;
	}
}

/*	Run timer, alarm when count==0 or abort when horizon changed
 */
static void doRun () {
	const uint32_t t = timerHit ();
	if (t > 0) {
		timerElapsed += t;
		puts ("\ntimerElapsed");
		fwrite (&timerElapsed, sizeof (timerElapsed), 1, stdout);
		if (timerElapsed >= timerValue) {
			/* ring the alarm! */
			for (uint8_t i = 0; i < PWM_LED_COUNT; i++) {
				pwmSet (i, PWM_MAX_BRIGHTNESS);
			}
			mode = UIMODE_ALARM;
			puts ("run->alarm");
			speakerStart (SPEAKER_BEEP);
			timerStop ();
			timerStart (ALARM_TIME);
		} else if (currLed > 0) {
			/* one step */
			--brightness[currLed];
			pwmSet (horizonLed (currLed), brightness[currLed]);
			++brightness[currLed-1];
			pwmSet (horizonLed (currLed-1), brightness[currLed-1]);
			if (brightness[currLed] == 0 && currLed > 0) {
				--currLed;
			}
			puts ("\ncurrLed");
			fwrite (&currLed, sizeof (currLed), 1, stdout);
			puts ("\nbrightness");
			fwrite (&brightness, sizeof (brightness), 1, stdout);
		}
	} else if (horizonChanged) {
		/* stop timer */
		speakerStart (SPEAKER_BEEP);

		puts ("run->idle (stopped)");
		enterIdle ();
	}
}

/*	Run alarm for some time or user interaction, then stop
 */
static void doAlarm () {
	const uint32_t t = timerHit ();
	if (t > 0 || horizonChanged) {
		puts ("alarm->idle");
		enterIdle ();
	}
}

/*	Wait for sensor initialization
 */
static void doInit () {
	/* get initial orientation */
	if (horizonChanged && h != HORIZON_NONE) {
		puts ("init->idle");
		enterIdle ();

#if 0
		/* debugging */
		mode = UIMODE_RUN;
		timerValue = (uint32_t) 60*1000*1000;
		timerElapsed = 0;
		/* (PWM_LED_COUNT-1)*PWM_MAX_BRIGHTNESS states; -1, since two leds’s
		 * states are interleaved */
		brightnessStep = timerValue/(uint32_t) ((PWM_LED_COUNT-1)*PWM_MAX_BRIGHTNESS);
		puts ("\nbrightnessStep\n");
		fwrite (&brightnessStep, sizeof (brightnessStep), 1, stdout);

		currLed = PWM_LED_COUNT-1;
		brightness[currLed] = PWM_MAX_BRIGHTNESS;
		pwmSet (horizonLed (currLed), brightness[currLed]);
		timerStart (brightnessStep);
#endif
	}
}

/*	Main loop
 */
void uiLoop () {
#if 0
	/* LED test mode */
	uint8_t i = 0;
	uint8_t brightness = 0;
	while (1) {
		for (uint8_t j = 0; j < PWM_LED_COUNT; j++) {
			pwmSet (horizonLed (j), PWM_OFF);
		}
		pwmSet (horizonLed (i), brightness);
		++i;
		if (i >= PWM_LED_COUNT) {
			i = 0;
			++brightness;
			if (brightness > PWM_MAX_BRIGHTNESS) {
				brightness = 0;
			}
		}
		_delay_ms (1000);
	}
#endif

#if 0
	/* timer test mode */
	timerStart ((uint32_t) 10*1000*1000);
	while (1) {
		uint32_t t;
		while (1) {
			t = timerHit ();
			if (t > 0) {
				break;
			}
			cpuSleep ();
		}
		puts ("on");
		fwrite (&t, sizeof (t), 1, stdout);
		pwmSet (horizonLed (0), PWM_ON);

		while (1) {
			t = timerHit ();
			if (t > 0) {
				break;
			}
			cpuSleep ();
		}
		puts ("off");
		fwrite (&t, sizeof (t), 1, stdout);
		pwmSet (horizonLed (0), PWM_OFF);
	}
#endif

	/* startup, test all LED’s */
	pwmStart ();
	for (uint8_t i = 0; i < PWM_LED_COUNT; i++) {
		pwmSet (i, PWM_ON);
	}
	pwmSet (0, PWM_OFF);
	accelStart ();
	pwmSet (1, PWM_OFF);

	/* make sure data is read even when missing the first interrupt (i.e.
	 * reboot) */
	enableWakeup (WAKE_ACCEL);
	enableWakeup (WAKE_GYRO);

	while (1) {
		processSensors ();

		horizon newh = accelGetHorizon ();
		if (newh != h) {
			horizonChanged = true;
		} else {
			horizonChanged = false;
		}
		h = newh;

		switch (mode) {
			case UIMODE_INIT:
				doInit ();
				break;

			case UIMODE_SELECT_COARSE:
				doSelectCoarse ();
				break;

			case UIMODE_SELECT_FINE:
				doSelectFine ();
				break;

			case UIMODE_IDLE:
				doIdle ();
				break;

			case UIMODE_RUN:
				doRun ();
				break;

			case UIMODE_ALARM:
				doAlarm ();
				break;

			default:
				assert (0 && "invalid ui mode");
				break;
		}

		sleepwhile (wakeup == 0);

#if 0
		printf ("t=%i, h=%i, s=%i\n", gyroGetZTicks (), accelGetHorizon (),
				accelGetShakeCount ());
		const int32_t gyroval = gyroGetZAccum ();
		const int16_t gyroraw = gyroGetZRaw ();
		const int8_t accelval = accelGetZ ();
		printf ("%li - %i - %i\n", gyroval, gyroraw, accelval);
#endif
	}

	timerStop ();
	pwmStop ();
}