g

Dependencies:   mbed-rtos mbed

Committer:
ShaolinPoutine
Date:
Sat Jan 28 06:22:46 2017 +0000
Revision:
0:8b27cd0189c9
Child:
1:8475f46f050b
g

Who changed what in which revision?

UserRevisionLine numberNew 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 0:8b27cd0189c9 20
ShaolinPoutine 0:8b27cd0189c9 21 MemoryPool<int, 16> analogpool;
ShaolinPoutine 0:8b27cd0189c9 22 // TODO
ShaolinPoutine 0:8b27cd0189c9 23 MemoryPool<int, 16> numericpool;
ShaolinPoutine 0:8b27cd0189c9 24
ShaolinPoutine 0:8b27cd0189c9 25 Ticker analogtimer;
ShaolinPoutine 0:8b27cd0189c9 26 Ticker numerictimer;
ShaolinPoutine 0:8b27cd0189c9 27
ShaolinPoutine 0:8b27cd0189c9 28 Thread* panalog_thread;
ShaolinPoutine 0:8b27cd0189c9 29 Thread* pnumeric_thread;
ShaolinPoutine 0:8b27cd0189c9 30
ShaolinPoutine 0:8b27cd0189c9 31
ShaolinPoutine 0:8b27cd0189c9 32 typedef struct
ShaolinPoutine 0:8b27cd0189c9 33 {
ShaolinPoutine 0:8b27cd0189c9 34 int type; // Which type of event
ShaolinPoutine 0:8b27cd0189c9 35 int id; // Which pins changed
ShaolinPoutine 0:8b27cd0189c9 36 int* position; // Position in the memory pool
ShaolinPoutine 0:8b27cd0189c9 37 int timestamp; // Timestamp when the event occured
ShaolinPoutine 0:8b27cd0189c9 38 } reading;
ShaolinPoutine 0:8b27cd0189c9 39
ShaolinPoutine 0:8b27cd0189c9 40 Queue<reading, 16> queue;
ShaolinPoutine 0:8b27cd0189c9 41
ShaolinPoutine 0:8b27cd0189c9 42 void lecture_analog(void const *args) {
ShaolinPoutine 0:8b27cd0189c9 43 pc.printf("Debut des lectures analogiques\r\n");
ShaolinPoutine 0:8b27cd0189c9 44 int state[2] = {0,0};
ShaolinPoutine 0:8b27cd0189c9 45 int newstate[2] = {0,0};
ShaolinPoutine 0:8b27cd0189c9 46 int mean1[5] = {0,0,0,0,0};
ShaolinPoutine 0:8b27cd0189c9 47 int mean2[5] = {0,0,0,0,0};
ShaolinPoutine 0:8b27cd0189c9 48 int i = 0;
ShaolinPoutine 0:8b27cd0189c9 49 int timestamp;
ShaolinPoutine 0:8b27cd0189c9 50 reading *message;
ShaolinPoutine 0:8b27cd0189c9 51 while (true) {
ShaolinPoutine 0:8b27cd0189c9 52 // synchronisation sur la période d'échantillonnage
ShaolinPoutine 0:8b27cd0189c9 53 Thread::signal_wait(signal250ms);
ShaolinPoutine 0:8b27cd0189c9 54 // lecture de l'étampe temporelle
ShaolinPoutine 0:8b27cd0189c9 55 timestamp = 0;
ShaolinPoutine 0:8b27cd0189c9 56 // lecture des échantillons analogiques
ShaolinPoutine 0:8b27cd0189c9 57 mean1[i] = ea_1;
ShaolinPoutine 0:8b27cd0189c9 58 mean2[i] = ea_2;
ShaolinPoutine 0:8b27cd0189c9 59 // calcul de la nouvelle moyenne courante
ShaolinPoutine 0:8b27cd0189c9 60 newstate[0] = (mean1[0] + mean1[1] + mean1[2] + mean1[3] + mean1[4]) / 5;
ShaolinPoutine 0:8b27cd0189c9 61 newstate[1] = (mean2[0] + mean2[1] + mean2[2] + mean2[3] + mean2[4]) / 5;
ShaolinPoutine 0:8b27cd0189c9 62 // génération éventuelle d'un événement
ShaolinPoutine 0:8b27cd0189c9 63 if (abs(newstate[0] - state[0]) > 1/8)
ShaolinPoutine 0:8b27cd0189c9 64 {
ShaolinPoutine 0:8b27cd0189c9 65 state[0] = newstate[0];
ShaolinPoutine 0:8b27cd0189c9 66 int *state = analogpool.alloc();
ShaolinPoutine 0:8b27cd0189c9 67 *state = state[0];
ShaolinPoutine 0:8b27cd0189c9 68 message->type = typeAnalog;
ShaolinPoutine 0:8b27cd0189c9 69 message->id = 1;
ShaolinPoutine 0:8b27cd0189c9 70 message->position = state;
ShaolinPoutine 0:8b27cd0189c9 71 message->timestamp = timestamp;
ShaolinPoutine 0:8b27cd0189c9 72 queue.put(message);
ShaolinPoutine 0:8b27cd0189c9 73 }
ShaolinPoutine 0:8b27cd0189c9 74 if (abs(newstate[1] - state[1]) > 1/8)
ShaolinPoutine 0:8b27cd0189c9 75 {
ShaolinPoutine 0:8b27cd0189c9 76 state[1] = newstate[1];
ShaolinPoutine 0:8b27cd0189c9 77 int *state = analogpool.alloc();
ShaolinPoutine 0:8b27cd0189c9 78 *state = state[1];
ShaolinPoutine 0:8b27cd0189c9 79 message->type = typeAnalog;
ShaolinPoutine 0:8b27cd0189c9 80 message->id = 2;
ShaolinPoutine 0:8b27cd0189c9 81 message->position = state;
ShaolinPoutine 0:8b27cd0189c9 82 message->timestamp = timestamp;
ShaolinPoutine 0:8b27cd0189c9 83 queue.put(message);
ShaolinPoutine 0:8b27cd0189c9 84 }
ShaolinPoutine 0:8b27cd0189c9 85 i = (i + 1) % 5;
ShaolinPoutine 0:8b27cd0189c9 86 }
ShaolinPoutine 0:8b27cd0189c9 87 }
ShaolinPoutine 0:8b27cd0189c9 88
ShaolinPoutine 0:8b27cd0189c9 89 void lecture_num(void const *args) {
ShaolinPoutine 0:8b27cd0189c9 90 pc.printf("Debut des lectures numeriques\r\n");
ShaolinPoutine 0:8b27cd0189c9 91 bool state[2] = {false, false};
ShaolinPoutine 0:8b27cd0189c9 92 bool newstate[2] = {false, false};
ShaolinPoutine 0:8b27cd0189c9 93 bool changed[2] = {false, false};
ShaolinPoutine 0:8b27cd0189c9 94 int timestamp = 0;
ShaolinPoutine 0:8b27cd0189c9 95 reading *message;
ShaolinPoutine 0:8b27cd0189c9 96 while (true) {
ShaolinPoutine 0:8b27cd0189c9 97 // synchronisation sur la période d'échantillonnage
ShaolinPoutine 0:8b27cd0189c9 98 //numeric_thread.signal_clr(signal100ms);
ShaolinPoutine 0:8b27cd0189c9 99 Thread::signal_wait(signal100ms);
ShaolinPoutine 0:8b27cd0189c9 100
ShaolinPoutine 0:8b27cd0189c9 101 // lecture de l'étampe temporelle
ShaolinPoutine 0:8b27cd0189c9 102 timestamp = 0;
ShaolinPoutine 0:8b27cd0189c9 103
ShaolinPoutine 0:8b27cd0189c9 104 // lecture des échantillons numériques
ShaolinPoutine 0:8b27cd0189c9 105 newstate[0] = !en_1;
ShaolinPoutine 0:8b27cd0189c9 106 newstate[1] = !en_2;
ShaolinPoutine 0:8b27cd0189c9 107
ShaolinPoutine 0:8b27cd0189c9 108 // prise en charge du phénomène de rebond
ShaolinPoutine 0:8b27cd0189c9 109 changed[0] = newstate[0] != state[0];
ShaolinPoutine 0:8b27cd0189c9 110 changed[1] = newstate[1] != state[1];
ShaolinPoutine 0:8b27cd0189c9 111
ShaolinPoutine 0:8b27cd0189c9 112 if (changed[0] || changed [1])
ShaolinPoutine 0:8b27cd0189c9 113 {
ShaolinPoutine 0:8b27cd0189c9 114 pnumeric_thread->signal_clr(signal50ms);
ShaolinPoutine 0:8b27cd0189c9 115 //flag isp
ShaolinPoutine 0:8b27cd0189c9 116 Thread::signal_wait(signal50ms);
ShaolinPoutine 0:8b27cd0189c9 117
ShaolinPoutine 0:8b27cd0189c9 118 newstate[0] = !en_1;
ShaolinPoutine 0:8b27cd0189c9 119 newstate[1] = !en_2;
ShaolinPoutine 0:8b27cd0189c9 120 if(newstate[0] != state[0] && changed[0])
ShaolinPoutine 0:8b27cd0189c9 121 {
ShaolinPoutine 0:8b27cd0189c9 122 led1 = newstate[0];
ShaolinPoutine 0:8b27cd0189c9 123 state[0] = newstate[0];
ShaolinPoutine 0:8b27cd0189c9 124 // génération éventuelle d'un événement
ShaolinPoutine 0:8b27cd0189c9 125 int *state = numericpool.alloc();
ShaolinPoutine 0:8b27cd0189c9 126 *state = state[0];
ShaolinPoutine 0:8b27cd0189c9 127 message->type = typeNumeric;
ShaolinPoutine 0:8b27cd0189c9 128 message->id = 1;
ShaolinPoutine 0:8b27cd0189c9 129 message->position = state;
ShaolinPoutine 0:8b27cd0189c9 130 message->timestamp = timestamp;
ShaolinPoutine 0:8b27cd0189c9 131 queue.put(message);
ShaolinPoutine 0:8b27cd0189c9 132 }
ShaolinPoutine 0:8b27cd0189c9 133 if(newstate[1] != state[1] && changed[1])
ShaolinPoutine 0:8b27cd0189c9 134 {
ShaolinPoutine 0:8b27cd0189c9 135 led2 = newstate[1];
ShaolinPoutine 0:8b27cd0189c9 136 state[1] = newstate[1];
ShaolinPoutine 0:8b27cd0189c9 137 // génération éventuelle d'un événement
ShaolinPoutine 0:8b27cd0189c9 138 int *state = numericpool.alloc();
ShaolinPoutine 0:8b27cd0189c9 139 *state = state[1];
ShaolinPoutine 0:8b27cd0189c9 140 message->type = typeNumeric;
ShaolinPoutine 0:8b27cd0189c9 141 message->id = 2;
ShaolinPoutine 0:8b27cd0189c9 142 message->position = state;
ShaolinPoutine 0:8b27cd0189c9 143 message->timestamp = timestamp;
ShaolinPoutine 0:8b27cd0189c9 144 queue.put(message);
ShaolinPoutine 0:8b27cd0189c9 145 }
ShaolinPoutine 0:8b27cd0189c9 146 }
ShaolinPoutine 0:8b27cd0189c9 147 else
ShaolinPoutine 0:8b27cd0189c9 148 {
ShaolinPoutine 0:8b27cd0189c9 149 Thread::yield();
ShaolinPoutine 0:8b27cd0189c9 150 }
ShaolinPoutine 0:8b27cd0189c9 151 }
ShaolinPoutine 0:8b27cd0189c9 152 }
ShaolinPoutine 0:8b27cd0189c9 153
ShaolinPoutine 0:8b27cd0189c9 154 void collection(void const *args) {
ShaolinPoutine 0:8b27cd0189c9 155 osEvent event;
ShaolinPoutine 0:8b27cd0189c9 156 reading* message;
ShaolinPoutine 0:8b27cd0189c9 157 while (true) {
ShaolinPoutine 0:8b27cd0189c9 158
ShaolinPoutine 0:8b27cd0189c9 159 // attente et lecture d'un événement
ShaolinPoutine 0:8b27cd0189c9 160 event = queue.get();
ShaolinPoutine 0:8b27cd0189c9 161 message = (reading*) event.value.p;
ShaolinPoutine 0:8b27cd0189c9 162 if (message->type == typeNumeric)
ShaolinPoutine 0:8b27cd0189c9 163 {
ShaolinPoutine 0:8b27cd0189c9 164 // écriture de l'événement en sortie (port série)
ShaolinPoutine 0:8b27cd0189c9 165 pc.printf("The state on numeric pin %d has changed to %d at %d\r\n", message->id, *(message->position), message->timestamp);
ShaolinPoutine 0:8b27cd0189c9 166 numericpool.free(message->position);
ShaolinPoutine 0:8b27cd0189c9 167 }
ShaolinPoutine 0:8b27cd0189c9 168 if (message->type == typeAnalog)
ShaolinPoutine 0:8b27cd0189c9 169 {
ShaolinPoutine 0:8b27cd0189c9 170 // écriture de l'événement en sortie (port série)
ShaolinPoutine 0:8b27cd0189c9 171 pc.printf("The state on analog pin %d has changed to %d at %d\r\n", message->id, *(message->position), message->timestamp);
ShaolinPoutine 0:8b27cd0189c9 172 numericpool.free(message->position);
ShaolinPoutine 0:8b27cd0189c9 173 }
ShaolinPoutine 0:8b27cd0189c9 174 }
ShaolinPoutine 0:8b27cd0189c9 175 }
ShaolinPoutine 0:8b27cd0189c9 176
ShaolinPoutine 0:8b27cd0189c9 177 void signalnumeric()
ShaolinPoutine 0:8b27cd0189c9 178 {
ShaolinPoutine 0:8b27cd0189c9 179 led3 = !led3;
ShaolinPoutine 0:8b27cd0189c9 180 pnumeric_thread->signal_set(signal100ms | signal50ms);
ShaolinPoutine 0:8b27cd0189c9 181 }
ShaolinPoutine 0:8b27cd0189c9 182
ShaolinPoutine 0:8b27cd0189c9 183 void signalanalog()
ShaolinPoutine 0:8b27cd0189c9 184 {
ShaolinPoutine 0:8b27cd0189c9 185 panalog_thread->signal_set(signal250ms);
ShaolinPoutine 0:8b27cd0189c9 186 }
ShaolinPoutine 0:8b27cd0189c9 187 Ticker flipper;
ShaolinPoutine 0:8b27cd0189c9 188 void flip() {
ShaolinPoutine 0:8b27cd0189c9 189 led4 = !led4;
ShaolinPoutine 0:8b27cd0189c9 190 }
ShaolinPoutine 0:8b27cd0189c9 191 int main() {
ShaolinPoutine 0:8b27cd0189c9 192 pc.printf("Welcome\r\n");
ShaolinPoutine 0:8b27cd0189c9 193 flipper.attach(&flip, 0.5);
ShaolinPoutine 0:8b27cd0189c9 194 // initialisation du RTC
ShaolinPoutine 0:8b27cd0189c9 195 //analogtimer.attach(&signalanalog, 0.250);
ShaolinPoutine 0:8b27cd0189c9 196 numerictimer.attach(&signalnumeric, 0.05);
ShaolinPoutine 0:8b27cd0189c9 197
ShaolinPoutine 0:8b27cd0189c9 198 // démarrage des tâches
ShaolinPoutine 0:8b27cd0189c9 199 //Thread analog_thread(lecture_analog);
ShaolinPoutine 0:8b27cd0189c9 200 Thread numeric_thread(lecture_num);
ShaolinPoutine 0:8b27cd0189c9 201 pnumeric_thread = &numeric_thread;
ShaolinPoutine 0:8b27cd0189c9 202 Thread q(collection);
ShaolinPoutine 0:8b27cd0189c9 203 while(1){
ShaolinPoutine 0:8b27cd0189c9 204 }
ShaolinPoutine 0:8b27cd0189c9 205 }