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