-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlinkModule.c
144 lines (133 loc) · 4.73 KB
/
BlinkModule.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
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
//////////////////////////////////////////////////////////////////////////////////////
// Blink Module //
//////////////////////////////////////////////////////////////////////////////////////
// This module allows other systems to indicate their statuses via the use of three //
// LED's installed on the Cortex. Rather than using seperate methods per blink, we //
// create one that indicates each blink at regular intervals and rates. This //
// simplifies the problem significantly, and lends more CPU power to other //
// processes. //
//////////////////////////////////////////////////////////////////////////////////////
// Enum values for different blink task settings.
enum LED_Level {Info = 1, Warning, Severe, Gun, UNASSIGNED = 99};
enum LED_Rate {Solid = 1, Slow, Medium, Fast, Irregular, UNASSIGNED = 99};
// LED_Task structure, comparable to OOP's Class.
typedef struct {
LED_Level level;
LED_Rate rate;
} LED_Task;
LED_Task LED_tasks[15];
task LED_blink() {
// Setup the array.
// (sizeof(LED_tasks) / 4) is a way to find array size. Each struct is worth 4 bytes.
for (int i = 0; i < (sizeof(LED_tasks) / 4); i++) {
LED_tasks[i].level = UNASSIGNED;
LED_tasks[i].rate = UNASSIGNED;
}
// Reset LED's.
SensorValue[PRT_ledG] = 0;
SensorValue[PRT_ledY] = 0;
SensorValue[PRT_ledR] = 0;
// Loop through each setting and apply appropriately.
// If there are overlapping blink tasks, more recent ones (likely more important), will be shown.
while (true) {
for (int cycle = 1; cycle < (6 + 1); cycle++) {
for (int i = (sizeof(LED_tasks) / 4) - 1; i > -1; i--) {
// Check to make sure it is not an empty task.
if (LED_tasks[i].level != UNASSIGNED && LED_tasks[i].rate != UNASSIGNED) {
short index;
if (LED_tasks[i].level == Info) {
index = PRT_ledG;
} else if (LED_tasks[i].level == Warning) {
index = PRT_ledY;
} else if (LED_tasks[i].level == Severe) {
index = PRT_ledR;
} else if (LED_tasks[i].level == Gun) {
index = PRT_ledGun;
}
// Blink at set rates. There are 6 intervals in a second. Each interval lights an LED based on rate.
// Rate | 1 2 3 4 5 6
// -------------------------------
// Solid : [X] [X] [X] [X] [X] [X]
// Fast : [X] [ ] [X] [ ] [X] [ ]
// Medium: [X] [ ] [ ] [X] [ ] [ ]
// Slow : [X] [ ] [ ] [ ] [ ] [ ]
// Irreg : [X] [ ] [X] [ ] [ ] [ ]
if (cycle == 1) {
SensorValue[index] = 1;
} else if (cycle == 3 && (LED_tasks[i].rate == Fast || LED_tasks[i].rate == Irregular)) {
SensorValue[index] = 1;
} else if (cycle == 4 && LED_tasks[i].rate == Medium) {
SensorValue[index] = 1;
} else if (cycle == 5 && LED_tasks[i].rate == Fast) {
SensorValue[index] = 1;
} else if (LED_tasks[i].rate == Solid) {
SensorValue[index] = 1;
} else {
SensorValue[index] = 0;
}
}
}
wait1Msec(1000 / 6);
}
}
}
// Function that allows other modules to set up a blink task.
short LED_startBlinkTask(LED_Level level, LED_Rate rate) {
// Find an empty slot, then fill it with a new task.
// Return identifier.
for (int i = (sizeof(LED_tasks) / 4) - 1; i > -1; i--) {
if (LED_tasks[i].level == UNASSIGNED && LED_tasks[i].rate == UNASSIGNED) {
LED_tasks[i].level = level;
LED_tasks[i].rate = rate;
return i;
}
}
// If there are too many LED_tasks, return null.
return NULL;
}
// Allows other modules to shut off blink tasks with an identifier.
bool LED_stopBlinkTask(short identifier) {
// Check to see if given identifier is real, then clear it.
// Return with success value.
if (LED_tasks[identifier].level != UNASSIGNED && LED_tasks[identifier].rate != UNASSIGNED) {
switch (LED_tasks[identifier].level) {
case Info:
SensorValue[PRT_ledG] = 0;
break;
case Warning:
SensorValue[PRT_ledY] = 0;
break;
case Severe:
SensorValue[PRT_ledR] = 0;
break;
}
LED_tasks[identifier].level = UNASSIGNED;
LED_tasks[identifier].rate = UNASSIGNED;
return true;
} else {
return false;
}
}
/*
LED_Level LED_getBlinkTaskLevel(short identifier) {
return LED_tasks[identifier].level;
}
LED_Rate LED_getBlinkTaskRate(short identifier) {
return LED_tasks[identifier].rate;
}
*/
void LED_editBlinkTask(short identifier, LED_Level level, LED_Rate rate) {
LED_tasks[identifier].level = level;
LED_tasks[identifier].rate = rate;
}
/* Clear the blink task memory.
void LED_clearBlinkTasks() {
for (int i = 0; i < (sizeof(LED_tasks) / 4); i++) {
LED_tasks[i].level = UNASSIGNED;
LED_tasks[i].rate = UNASSIGNED;
}
SensorValue[PRT_ledG] = 0;
SensorValue[PRT_ledY] = 0;
SensorValue[PRT_ledR] = 0;
}
*/