-
Notifications
You must be signed in to change notification settings - Fork 6
/
ex-level.c
82 lines (72 loc) · 1.73 KB
/
ex-level.c
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
/* lab4/ex-level.c */
/* Copyright (c) 2019 J. M. Spivey */
#include "microbian.h"
#include "hardware.h"
#include "lib.h"
void accel_start(void);
void accel_reading(int *x, int *y, int *z);
/* light -- show one pixel */
void light(int x, int y)
{
image screen;
image_clear(screen);
image_set(x, y, screen);
display_show(screen);
}
/* scale -- map acceleration to coordinate */
static int scale(int x)
{
if (x < -20) return 4;
if (x < -10) return 3;
if (x <= 10) return 2;
if (x <= 20) return 1;
return 0;
}
static void i2c_map(void)
{
static char *hex = "0123456789abcdef";
printf("I2C bus map:\n");
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
for (int a = 0; a < 8; a++) {
printf("%c0:", hex[a]);
for (int b = 0; b < 16; b++) {
int addr = (a<<4) + b;
if (addr < 0x08 || addr >= 0x78)
/* Reserved addresses */
printf(" ");
else {
int status = i2c_probe(I2C_INTERNAL, addr);
if (status == OK)
printf(" %c%c", hex[a], hex[b]);
else
printf(" --");
}
}
printf("\n");
}
}
/* main_task -- show the spirit level */
static void main(int n)
{
int x, y, z;
printf("Hello\n\n");
i2c_map();
printf("\n");
timer_delay(1000);
accel_start();
while (1) {
timer_delay(200);
accel_reading(&x, &y, &z);
printf("x=%d y=%d z=%d\n", x, y, z);
x = scale(x); y = scale(y);
light(x, y);
}
}
void init(void)
{
serial_init();
timer_init();
i2c_init(I2C_INTERNAL);
display_init();
start("Main", main, 0, STACK);
}