aboutsummaryrefslogtreecommitdiff
path: root/ui.c
blob: 8d621e447ea97c693424bd4d2c1527e72a31dd3f (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
#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 "speaker.h"
#include "timer.h"
#include "pwm.h"

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

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

static uimode mode = UIMODE_INIT;
/* timer seconds */
static int16_t coarseSeconds = 0, fineSeconds = 0;
static uint8_t step = 0, substep = 0;
static uint16_t secPerSubstep = 0;
static uint16_t substepsec = 0;
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;
	}
}

/*	Timer value selection
 */
static void doSelectCoarse () {
	if (accelGetShakeCount () >= 2) {
		/* stop selection */
		accelResetShakeCount ();
		mode = UIMODE_SELECT_FINE;
		puts ("selectcoarse->selectfine");
		speakerStart (SPEAKER_BEEP);
		return;
	}

	/* use zticks as seconds */
	const int16_t zticks = gyroGetZTicks ();
	if (abs (zticks) > 0) {
		gyroResetZTicks ();
		coarseSeconds = limits(coarseSeconds + zticks*60*5, 0, 60*60);
		//printf ("c:%it:%i\n", coarseSeconds, zticks);

		pwmStop ();
		const uint8_t tenminutes = coarseSeconds/60/10;
		for (uint8_t i = 0; i < tenminutes; i++) {
			pwmSetBlink (horizonLed (i), PWM_BLINK_ON);
		}
		for (uint8_t i = tenminutes; i < PWM_LED_COUNT; i++) {
			pwmSetBlink (horizonLed (i), PWM_BLINK_OFF);
		}
		pwmStart ();
	}
}

static void doSelectFine () {
	if (accelGetShakeCount () >= 2) {
		/* stop selection */
		accelResetShakeCount ();
		step = 6;
		substep = 3;
		secPerSubstep = (coarseSeconds + fineSeconds)/(6*3);
		mode = UIMODE_IDLE;
		puts ("selectfine->idle");
		speakerStart (SPEAKER_BEEP);
		return;
	}

	/* use zticks as seconds */
	const int16_t zticks = gyroGetZTicks ();
	if (abs (zticks) > 0) {
		gyroResetZTicks ();
		fineSeconds = limits(fineSeconds + zticks*30, -5*60, 5*60);
		//printf ("f:%it:%i\n", fineSeconds, zticks);

		pwmStop ();
		const uint8_t minutes = abs (fineSeconds)/60;
		for (uint8_t i = 0; i < minutes; i++) {
			pwmSetBlink (horizonLed (i), PWM_BLINK_ON);
		}
		for (uint8_t i = minutes; i < PWM_LED_COUNT-1; i++) {
			pwmSetBlink (horizonLed (i), PWM_BLINK_OFF);
		}
		if (fineSeconds < 0) {
			pwmSetBlink (horizonLed (PWM_LED_COUNT-1), PWM_BLINK_ON);
		} else {
			pwmSetBlink (horizonLed (PWM_LED_COUNT-1), PWM_BLINK_OFF);
		}
		pwmStart ();
	}
}

/*	Idle function, waits for timer start or select commands
 */
static void doIdle () {
	if (horizonChanged) {
		/* start timer */
		mode = UIMODE_RUN;
		timerStart ();
		puts ("idle->run");
		speakerStart (SPEAKER_BEEP);
	} else if (accelGetShakeCount () >= 2) {
		/* set timer */
		accelResetShakeCount ();
		mode = UIMODE_SELECT_COARSE;
		puts ("idle->select");
		speakerStart (SPEAKER_BEEP);
		return;
	}
}

/*	Run timer, alarm when count==0 or abort when horizon changed
 */
static void doRun () {
	if (timerHit ()) {
		++substepsec;
		if (substepsec > secPerSubstep) {
			--substep;
			substepsec = 0;
		}
		if (substep == 0) {
			--step;
			substep = 3;
			substepsec = 0;
		}
		//printf("s:%uss:%u\n", step, substep);
		if (step == 0) {
			/* blink all leds */
			pwmStop ();
			for (uint8_t i = 0; i < PWM_LED_COUNT; i++) {
				pwmSetBlink (i, 1);
			}
			pwmStart ();
			step = ALARM_TIME;
			mode = UIMODE_ALARM;
			puts ("run->alarm");
			speakerStart (SPEAKER_BEEP);
		} else {
			pwmStop ();
			for (uint8_t i = 0; i < step-1; i++) {
				pwmSetBlink (horizonLed (i), PWM_BLINK_ON);
			}
			pwmSetBlink (horizonLed (step-1), PWM_BLINK_ON);
			for (uint8_t i = step; i < PWM_LED_COUNT; i++) {
				pwmSetBlink (horizonLed (i), PWM_BLINK_OFF);
			}
			pwmStart ();
		}
	} else if (horizonChanged) {
		/* stop timer */
		mode = UIMODE_IDLE;
		puts ("run->idle (stopped)");
		speakerStart (SPEAKER_BEEP);
	}
}

/*	Run alarm for some time or user interaction, then stop
 */
static void doAlarm () {
	if (timerHit ()) {
		--step;
	}
	if (horizonChanged || step == 0) {
		timerStop ();
		step = 0;
		/* stop blinking */
		pwmStop ();
		mode = UIMODE_IDLE;
	}
}

/*	Wait for sensor initialization
 */
static void doInit () {
	/* get initial orientation */
	h = accelGetHorizon ();
	if (h != HORIZON_NONE) {
		mode = UIMODE_IDLE;
		puts ("init->idle");
		pwmStop ();
	}
}

/*	Sleep CPU
 */
static void cpuSleep () {
	sleep_enable ();
	sleep_cpu ();
	sleep_disable ();
}

/*	Main loop
 */
void uiLoop () {
#if 0
	/* LED test mode */
	uint8_t i = 0;
	uint8_t blink = 0;
	while (1) {
		pwmStop ();
		for (uint8_t j = 0; j < PWM_LED_COUNT; j++) {
			pwmSetBlink (horizonLed (j), PWM_BLINK_OFF);
		}
		pwmSetBlink (horizonLed (i), blink == 0 ? PWM_BLINK_ON : blink);
		++i;
		if (i >= PWM_LED_COUNT) {
			i = 0;
			++blink;
			if (blink >= 3) {
				blink = 0;
			}
		}
		pwmStart ();
		_delay_ms (1000);
	}
#endif

	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;
		}
		cpuSleep ();

#if 0
		printf ("t=%i, h=%i, s=%i\n", gyroGetZTicks (), accelGetHorizon (),
				accelGetShakeCount ());
		volatile const int32_t *gyroval = gyroGetAccum ();
		volatile const int16_t *gyroraw = gyroGetRaw ();
		volatile const int8_t *accelval = accelGet ();
		printf ("%li/%li/%li - %i/%i/%i - %i/%i/%i\n",
				gyroval[0], gyroval[1], gyroval[2],
				gyroraw[0], gyroraw[1], gyroraw[2],
				accelval[1], accelval[3], accelval[5]);
#endif
	}
}