Raphaël Drouin
/
app2
g
main.cpp@1:8475f46f050b, 2017-01-31 (annotated)
- Committer:
- ShaolinPoutine
- Date:
- Tue Jan 31 15:43:41 2017 +0000
- Revision:
- 1:8475f46f050b
- Parent:
- 0:8b27cd0189c9
- Child:
- 2:2d84e5bf72bd
s
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ShaolinPoutine | 0:8b27cd0189c9 | 1 | #include "mbed.h" |
ShaolinPoutine | 0:8b27cd0189c9 | 2 | #include "rtos.h" |
ShaolinPoutine | 0:8b27cd0189c9 | 3 | Serial pc(USBTX, USBRX); |
ShaolinPoutine | 0:8b27cd0189c9 | 4 | DigitalOut led1(LED1); |
ShaolinPoutine | 0:8b27cd0189c9 | 5 | DigitalOut led2(LED2); |
ShaolinPoutine | 0:8b27cd0189c9 | 6 | DigitalOut led3(LED3); |
ShaolinPoutine | 0:8b27cd0189c9 | 7 | DigitalOut led4(LED4); |
ShaolinPoutine | 0:8b27cd0189c9 | 8 | DigitalIn en_1(p15, PullUp); |
ShaolinPoutine | 0:8b27cd0189c9 | 9 | DigitalIn en_2(p16, PullUp); |
ShaolinPoutine | 0:8b27cd0189c9 | 10 | AnalogIn ea_1(p19); |
ShaolinPoutine | 0:8b27cd0189c9 | 11 | AnalogIn ea_2(p20); |
ShaolinPoutine | 0:8b27cd0189c9 | 12 | |
ShaolinPoutine | 0:8b27cd0189c9 | 13 | int signalnewdata = 0x1 << 6; |
ShaolinPoutine | 0:8b27cd0189c9 | 14 | int signal100ms = 0x1 << 5; |
ShaolinPoutine | 0:8b27cd0189c9 | 15 | int signal50ms = 0x1 << 4; |
ShaolinPoutine | 0:8b27cd0189c9 | 16 | int signal250ms = 0x1 << 3; |
ShaolinPoutine | 0:8b27cd0189c9 | 17 | |
ShaolinPoutine | 0:8b27cd0189c9 | 18 | int typeAnalog = 0; |
ShaolinPoutine | 0:8b27cd0189c9 | 19 | int typeNumeric = 1; |
ShaolinPoutine | 1:8475f46f050b | 20 | time_t begin_time = 0; |
ShaolinPoutine | 0:8b27cd0189c9 | 21 | |
ShaolinPoutine | 1:8475f46f050b | 22 | MemoryPool<float, 16> analogpool; |
ShaolinPoutine | 0:8b27cd0189c9 | 23 | // TODO |
ShaolinPoutine | 0:8b27cd0189c9 | 24 | MemoryPool<int, 16> numericpool; |
ShaolinPoutine | 0:8b27cd0189c9 | 25 | |
ShaolinPoutine | 0:8b27cd0189c9 | 26 | Ticker analogtimer; |
ShaolinPoutine | 0:8b27cd0189c9 | 27 | Ticker numerictimer; |
ShaolinPoutine | 0:8b27cd0189c9 | 28 | |
ShaolinPoutine | 0:8b27cd0189c9 | 29 | Thread* panalog_thread; |
ShaolinPoutine | 0:8b27cd0189c9 | 30 | Thread* pnumeric_thread; |
ShaolinPoutine | 0:8b27cd0189c9 | 31 | |
ShaolinPoutine | 0:8b27cd0189c9 | 32 | |
ShaolinPoutine | 0:8b27cd0189c9 | 33 | typedef struct |
ShaolinPoutine | 0:8b27cd0189c9 | 34 | { |
ShaolinPoutine | 1:8475f46f050b | 35 | int type; // Which type of event |
ShaolinPoutine | 1:8475f46f050b | 36 | int id; // Which pins changed |
ShaolinPoutine | 1:8475f46f050b | 37 | int* position; // Position in the memory pool |
ShaolinPoutine | 1:8475f46f050b | 38 | unsigned long seconds; // Timestamp when the event occured |
ShaolinPoutine | 0:8b27cd0189c9 | 39 | } reading; |
ShaolinPoutine | 0:8b27cd0189c9 | 40 | |
ShaolinPoutine | 0:8b27cd0189c9 | 41 | Queue<reading, 16> queue; |
ShaolinPoutine | 0:8b27cd0189c9 | 42 | |
ShaolinPoutine | 0:8b27cd0189c9 | 43 | void lecture_analog(void const *args) { |
ShaolinPoutine | 0:8b27cd0189c9 | 44 | pc.printf("Debut des lectures analogiques\r\n"); |
ShaolinPoutine | 1:8475f46f050b | 45 | float state[2] = {0,0}; |
ShaolinPoutine | 1:8475f46f050b | 46 | float newstate[2] = {0,0}; |
ShaolinPoutine | 1:8475f46f050b | 47 | float mean1[5] = {0,0,0,0,0}; |
ShaolinPoutine | 1:8475f46f050b | 48 | float mean2[5] = {0,0,0,0,0}; |
ShaolinPoutine | 0:8b27cd0189c9 | 49 | int i = 0; |
ShaolinPoutine | 1:8475f46f050b | 50 | time_t timestamp = time(NULL); |
ShaolinPoutine | 1:8475f46f050b | 51 | unsigned long seconds = 0; |
ShaolinPoutine | 0:8b27cd0189c9 | 52 | reading *message; |
ShaolinPoutine | 0:8b27cd0189c9 | 53 | while (true) { |
ShaolinPoutine | 0:8b27cd0189c9 | 54 | // synchronisation sur la période d'échantillonnage |
ShaolinPoutine | 0:8b27cd0189c9 | 55 | Thread::signal_wait(signal250ms); |
ShaolinPoutine | 0:8b27cd0189c9 | 56 | // lecture de l'étampe temporelle |
ShaolinPoutine | 1:8475f46f050b | 57 | seconds = (unsigned long) difftime(begin_time, 0); |
ShaolinPoutine | 0:8b27cd0189c9 | 58 | // lecture des échantillons analogiques |
ShaolinPoutine | 0:8b27cd0189c9 | 59 | mean1[i] = ea_1; |
ShaolinPoutine | 0:8b27cd0189c9 | 60 | mean2[i] = ea_2; |
ShaolinPoutine | 0:8b27cd0189c9 | 61 | // calcul de la nouvelle moyenne courante |
ShaolinPoutine | 1:8475f46f050b | 62 | newstate[0] = (mean1[0] + mean1[1] + mean1[2] + mean1[3] + mean1[4]) / 5.0; |
ShaolinPoutine | 1:8475f46f050b | 63 | newstate[1] = (mean2[0] + mean2[1] + mean2[2] + mean2[3] + mean2[4]) / 5.0; |
ShaolinPoutine | 0:8b27cd0189c9 | 64 | // génération éventuelle d'un événement |
ShaolinPoutine | 1:8475f46f050b | 65 | led3 = !led3; |
ShaolinPoutine | 1:8475f46f050b | 66 | if (abs(newstate[0] - state[0]) > 1.0/8.0) |
ShaolinPoutine | 0:8b27cd0189c9 | 67 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 68 | state[0] = newstate[0]; |
ShaolinPoutine | 1:8475f46f050b | 69 | float *state = analogpool.alloc(); |
ShaolinPoutine | 0:8b27cd0189c9 | 70 | *state = state[0]; |
ShaolinPoutine | 0:8b27cd0189c9 | 71 | message->type = typeAnalog; |
ShaolinPoutine | 0:8b27cd0189c9 | 72 | message->id = 1; |
ShaolinPoutine | 1:8475f46f050b | 73 | message->position = (int *) state; |
ShaolinPoutine | 1:8475f46f050b | 74 | message->seconds = seconds; |
ShaolinPoutine | 0:8b27cd0189c9 | 75 | queue.put(message); |
ShaolinPoutine | 0:8b27cd0189c9 | 76 | } |
ShaolinPoutine | 1:8475f46f050b | 77 | if (abs(newstate[1] - state[1]) > 1.0/8.0) |
ShaolinPoutine | 0:8b27cd0189c9 | 78 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 79 | state[1] = newstate[1]; |
ShaolinPoutine | 1:8475f46f050b | 80 | float *state = analogpool.alloc(); |
ShaolinPoutine | 0:8b27cd0189c9 | 81 | *state = state[1]; |
ShaolinPoutine | 0:8b27cd0189c9 | 82 | message->type = typeAnalog; |
ShaolinPoutine | 0:8b27cd0189c9 | 83 | message->id = 2; |
ShaolinPoutine | 1:8475f46f050b | 84 | message->position = (int *) state; |
ShaolinPoutine | 1:8475f46f050b | 85 | message->seconds = seconds; |
ShaolinPoutine | 0:8b27cd0189c9 | 86 | queue.put(message); |
ShaolinPoutine | 0:8b27cd0189c9 | 87 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 88 | i = (i + 1) % 5; |
ShaolinPoutine | 0:8b27cd0189c9 | 89 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 90 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 91 | |
ShaolinPoutine | 0:8b27cd0189c9 | 92 | void lecture_num(void const *args) { |
ShaolinPoutine | 0:8b27cd0189c9 | 93 | pc.printf("Debut des lectures numeriques\r\n"); |
ShaolinPoutine | 0:8b27cd0189c9 | 94 | bool state[2] = {false, false}; |
ShaolinPoutine | 0:8b27cd0189c9 | 95 | bool newstate[2] = {false, false}; |
ShaolinPoutine | 0:8b27cd0189c9 | 96 | bool changed[2] = {false, false}; |
ShaolinPoutine | 1:8475f46f050b | 97 | unsigned long seconds = 0; |
ShaolinPoutine | 0:8b27cd0189c9 | 98 | reading *message; |
ShaolinPoutine | 0:8b27cd0189c9 | 99 | while (true) { |
ShaolinPoutine | 0:8b27cd0189c9 | 100 | // synchronisation sur la période d'échantillonnage |
ShaolinPoutine | 0:8b27cd0189c9 | 101 | //numeric_thread.signal_clr(signal100ms); |
ShaolinPoutine | 0:8b27cd0189c9 | 102 | Thread::signal_wait(signal100ms); |
ShaolinPoutine | 0:8b27cd0189c9 | 103 | |
ShaolinPoutine | 0:8b27cd0189c9 | 104 | // lecture de l'étampe temporelle |
ShaolinPoutine | 1:8475f46f050b | 105 | seconds = (unsigned long) difftime(begin_time, 0); |
ShaolinPoutine | 0:8b27cd0189c9 | 106 | |
ShaolinPoutine | 0:8b27cd0189c9 | 107 | // lecture des échantillons numériques |
ShaolinPoutine | 0:8b27cd0189c9 | 108 | newstate[0] = !en_1; |
ShaolinPoutine | 0:8b27cd0189c9 | 109 | newstate[1] = !en_2; |
ShaolinPoutine | 0:8b27cd0189c9 | 110 | |
ShaolinPoutine | 0:8b27cd0189c9 | 111 | // prise en charge du phénomène de rebond |
ShaolinPoutine | 0:8b27cd0189c9 | 112 | changed[0] = newstate[0] != state[0]; |
ShaolinPoutine | 0:8b27cd0189c9 | 113 | changed[1] = newstate[1] != state[1]; |
ShaolinPoutine | 0:8b27cd0189c9 | 114 | |
ShaolinPoutine | 1:8475f46f050b | 115 | int *test = numericpool.alloc(); |
ShaolinPoutine | 1:8475f46f050b | 116 | *test = 42; |
ShaolinPoutine | 1:8475f46f050b | 117 | printf("%d", test); |
ShaolinPoutine | 1:8475f46f050b | 118 | numericpool.free(test); |
ShaolinPoutine | 1:8475f46f050b | 119 | |
ShaolinPoutine | 0:8b27cd0189c9 | 120 | if (changed[0] || changed [1]) |
ShaolinPoutine | 0:8b27cd0189c9 | 121 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 122 | pnumeric_thread->signal_clr(signal50ms); |
ShaolinPoutine | 0:8b27cd0189c9 | 123 | //flag isp |
ShaolinPoutine | 0:8b27cd0189c9 | 124 | Thread::signal_wait(signal50ms); |
ShaolinPoutine | 0:8b27cd0189c9 | 125 | |
ShaolinPoutine | 0:8b27cd0189c9 | 126 | newstate[0] = !en_1; |
ShaolinPoutine | 0:8b27cd0189c9 | 127 | newstate[1] = !en_2; |
ShaolinPoutine | 0:8b27cd0189c9 | 128 | if(newstate[0] != state[0] && changed[0]) |
ShaolinPoutine | 0:8b27cd0189c9 | 129 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 130 | led1 = newstate[0]; |
ShaolinPoutine | 0:8b27cd0189c9 | 131 | state[0] = newstate[0]; |
ShaolinPoutine | 0:8b27cd0189c9 | 132 | // génération éventuelle d'un événement |
ShaolinPoutine | 0:8b27cd0189c9 | 133 | int *state = numericpool.alloc(); |
ShaolinPoutine | 0:8b27cd0189c9 | 134 | *state = state[0]; |
ShaolinPoutine | 0:8b27cd0189c9 | 135 | message->type = typeNumeric; |
ShaolinPoutine | 0:8b27cd0189c9 | 136 | message->id = 1; |
ShaolinPoutine | 0:8b27cd0189c9 | 137 | message->position = state; |
ShaolinPoutine | 1:8475f46f050b | 138 | message->seconds = seconds; |
ShaolinPoutine | 0:8b27cd0189c9 | 139 | queue.put(message); |
ShaolinPoutine | 0:8b27cd0189c9 | 140 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 141 | if(newstate[1] != state[1] && changed[1]) |
ShaolinPoutine | 0:8b27cd0189c9 | 142 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 143 | led2 = newstate[1]; |
ShaolinPoutine | 0:8b27cd0189c9 | 144 | state[1] = newstate[1]; |
ShaolinPoutine | 0:8b27cd0189c9 | 145 | // génération éventuelle d'un événement |
ShaolinPoutine | 0:8b27cd0189c9 | 146 | int *state = numericpool.alloc(); |
ShaolinPoutine | 0:8b27cd0189c9 | 147 | *state = state[1]; |
ShaolinPoutine | 0:8b27cd0189c9 | 148 | message->type = typeNumeric; |
ShaolinPoutine | 0:8b27cd0189c9 | 149 | message->id = 2; |
ShaolinPoutine | 0:8b27cd0189c9 | 150 | message->position = state; |
ShaolinPoutine | 1:8475f46f050b | 151 | message->seconds = seconds; |
ShaolinPoutine | 0:8b27cd0189c9 | 152 | queue.put(message); |
ShaolinPoutine | 0:8b27cd0189c9 | 153 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 154 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 155 | else |
ShaolinPoutine | 0:8b27cd0189c9 | 156 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 157 | Thread::yield(); |
ShaolinPoutine | 0:8b27cd0189c9 | 158 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 159 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 160 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 161 | |
ShaolinPoutine | 0:8b27cd0189c9 | 162 | void collection(void const *args) { |
ShaolinPoutine | 0:8b27cd0189c9 | 163 | osEvent event; |
ShaolinPoutine | 0:8b27cd0189c9 | 164 | reading* message; |
ShaolinPoutine | 0:8b27cd0189c9 | 165 | while (true) { |
ShaolinPoutine | 0:8b27cd0189c9 | 166 | |
ShaolinPoutine | 0:8b27cd0189c9 | 167 | // attente et lecture d'un événement |
ShaolinPoutine | 0:8b27cd0189c9 | 168 | event = queue.get(); |
ShaolinPoutine | 0:8b27cd0189c9 | 169 | message = (reading*) event.value.p; |
ShaolinPoutine | 0:8b27cd0189c9 | 170 | if (message->type == typeNumeric) |
ShaolinPoutine | 0:8b27cd0189c9 | 171 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 172 | // écriture de l'événement en sortie (port série) |
ShaolinPoutine | 1:8475f46f050b | 173 | pc.printf("The state on numeric pin %d has changed to %d at %lus\r\n", message->id, (int *) *(message->position), message->seconds); |
ShaolinPoutine | 0:8b27cd0189c9 | 174 | numericpool.free(message->position); |
ShaolinPoutine | 0:8b27cd0189c9 | 175 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 176 | if (message->type == typeAnalog) |
ShaolinPoutine | 0:8b27cd0189c9 | 177 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 178 | // écriture de l'événement en sortie (port série) |
ShaolinPoutine | 1:8475f46f050b | 179 | pc.printf("The state on analog pin %d has changed to %d at %lus\r\n", message->id, *(message->position), message->seconds); |
ShaolinPoutine | 1:8475f46f050b | 180 | analogpool.free((float *) message->position); |
ShaolinPoutine | 0:8b27cd0189c9 | 181 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 182 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 183 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 184 | |
ShaolinPoutine | 0:8b27cd0189c9 | 185 | void signalnumeric() |
ShaolinPoutine | 0:8b27cd0189c9 | 186 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 187 | pnumeric_thread->signal_set(signal100ms | signal50ms); |
ShaolinPoutine | 0:8b27cd0189c9 | 188 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 189 | |
ShaolinPoutine | 0:8b27cd0189c9 | 190 | void signalanalog() |
ShaolinPoutine | 0:8b27cd0189c9 | 191 | { |
ShaolinPoutine | 0:8b27cd0189c9 | 192 | panalog_thread->signal_set(signal250ms); |
ShaolinPoutine | 0:8b27cd0189c9 | 193 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 194 | Ticker flipper; |
ShaolinPoutine | 0:8b27cd0189c9 | 195 | void flip() { |
ShaolinPoutine | 0:8b27cd0189c9 | 196 | led4 = !led4; |
ShaolinPoutine | 0:8b27cd0189c9 | 197 | } |
ShaolinPoutine | 1:8475f46f050b | 198 | int main(){ |
ShaolinPoutine | 1:8475f46f050b | 199 | // set_time(0); |
ShaolinPoutine | 1:8475f46f050b | 200 | begin_time = time(NULL); |
ShaolinPoutine | 0:8b27cd0189c9 | 201 | pc.printf("Welcome\r\n"); |
ShaolinPoutine | 0:8b27cd0189c9 | 202 | flipper.attach(&flip, 0.5); |
ShaolinPoutine | 0:8b27cd0189c9 | 203 | // initialisation du RTC |
ShaolinPoutine | 1:8475f46f050b | 204 | analogtimer.attach(&signalanalog, 0.250); |
ShaolinPoutine | 1:8475f46f050b | 205 | numerictimer.attach(&signalnumeric, 0.5); |
ShaolinPoutine | 0:8b27cd0189c9 | 206 | |
ShaolinPoutine | 0:8b27cd0189c9 | 207 | // démarrage des tâches |
ShaolinPoutine | 1:8475f46f050b | 208 | Thread analog_thread(lecture_analog); |
ShaolinPoutine | 1:8475f46f050b | 209 | panalog_thread = &analog_thread; |
ShaolinPoutine | 0:8b27cd0189c9 | 210 | Thread numeric_thread(lecture_num); |
ShaolinPoutine | 0:8b27cd0189c9 | 211 | pnumeric_thread = &numeric_thread; |
ShaolinPoutine | 0:8b27cd0189c9 | 212 | Thread q(collection); |
ShaolinPoutine | 0:8b27cd0189c9 | 213 | while(1){ |
ShaolinPoutine | 0:8b27cd0189c9 | 214 | } |
ShaolinPoutine | 0:8b27cd0189c9 | 215 | } |