summaryrefslogtreecommitdiff
path: root/c/phb/phb.c
blob: 6f85d09cc10822143d811b4eff49252f250240b1 (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
#include <stdint.h>
#include <stdbool.h>
#include "rvcontroller-ecalls.h"

bool streq(char *a, char *b) {
	for (int i=0;a[i] != 0 || b[i] != 0;i++) {
		if (a[i] != b[i]) {
			return false;
		}
	}
	return true;
}

void sleep(uint32_t delay) {
	//This will start having problems after about 130 years
	//of uptime, but that's probably acceptable
	uint32_t endtime = rdtime() + delay;
	while (rdtime() < endtime) {}
	return;
}

bool checkdetector(void) {
	char channelbuf[16];
	char msgbuf[16];
	while (digiline_bufferlevel() > 0) {
		lightweight_mode(0);
		digiline_receive(channelbuf,16,msgbuf,16);
		if (streq(channelbuf,"detect")) {
			return true;
		}
	}
	lightweight_mode(1);
	return false;
}

void main() {
	printstr("Ped Hybrid Beacon\n");
	printstr("for RVController\nInitializing HW\n");
	digiline_send("beacon","OFF");
	digiline_send("ped","RED");
	digiline_clearbuffer();
	while (true) {
		printstr("Idle\n");
		lightweight_mode(1);
		printstr("Lightweight: On\n");
		while (!checkdetector()) {}
		printstr("Ped detected\nLightweight: Off\n");
		lightweight_mode(0);
		printstr("Beacon: Flash Yellow\n");
		digiline_send("beacon","FLASHYELLOW");
		sleep(3);
		printstr("Beacon: Yellow\n");
		digiline_send("beacon","YELLOW");
		sleep(5);
		printstr("Beacon: Red\n");
		digiline_send("beacon","RED");
		sleep(1);
		printstr("Ped: Walk\n");
		digiline_send("ped","GREEN");
		sleep(7);
		digiline_clearbuffer();
		printstr("Ped: Flash DW\n");
		digiline_send("ped","FLASHRED");
		printstr("Beacon: Flash Red\n");
		digiline_send("beacon","FLASHRED");
		sleep(15);
		printstr("Ped: Don't Walk\n");
		digiline_send("ped","RED");
		sleep(1);
		printstr("Beacon: Off\n");
		digiline_send("beacon","OFF");
		sleep(15);
	}
}