![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
g
Diff: main.cpp
- Revision:
- 1:8475f46f050b
- Parent:
- 0:8b27cd0189c9
- Child:
- 2:2d84e5bf72bd
diff -r 8b27cd0189c9 -r 8475f46f050b main.cpp --- a/main.cpp Sat Jan 28 06:22:46 2017 +0000 +++ b/main.cpp Tue Jan 31 15:43:41 2017 +0000 @@ -17,8 +17,9 @@ int typeAnalog = 0; int typeNumeric = 1; +time_t begin_time = 0; -MemoryPool<int, 16> analogpool; +MemoryPool<float, 16> analogpool; // TODO MemoryPool<int, 16> numericpool; @@ -31,55 +32,57 @@ typedef struct { - int type; // Which type of event - int id; // Which pins changed - int* position; // Position in the memory pool - int timestamp; // Timestamp when the event occured + int type; // Which type of event + int id; // Which pins changed + int* position; // Position in the memory pool + unsigned long seconds; // Timestamp when the event occured } reading; Queue<reading, 16> queue; void lecture_analog(void const *args) { pc.printf("Debut des lectures analogiques\r\n"); - int state[2] = {0,0}; - int newstate[2] = {0,0}; - int mean1[5] = {0,0,0,0,0}; - int mean2[5] = {0,0,0,0,0}; + float state[2] = {0,0}; + float newstate[2] = {0,0}; + float mean1[5] = {0,0,0,0,0}; + float mean2[5] = {0,0,0,0,0}; int i = 0; - int timestamp; + time_t timestamp = time(NULL); + unsigned long seconds = 0; reading *message; while (true) { // synchronisation sur la période d'échantillonnage Thread::signal_wait(signal250ms); // lecture de l'étampe temporelle - timestamp = 0; + seconds = (unsigned long) difftime(begin_time, 0); // lecture des échantillons analogiques mean1[i] = ea_1; mean2[i] = ea_2; // calcul de la nouvelle moyenne courante - newstate[0] = (mean1[0] + mean1[1] + mean1[2] + mean1[3] + mean1[4]) / 5; - newstate[1] = (mean2[0] + mean2[1] + mean2[2] + mean2[3] + mean2[4]) / 5; + newstate[0] = (mean1[0] + mean1[1] + mean1[2] + mean1[3] + mean1[4]) / 5.0; + newstate[1] = (mean2[0] + mean2[1] + mean2[2] + mean2[3] + mean2[4]) / 5.0; // génération éventuelle d'un événement - if (abs(newstate[0] - state[0]) > 1/8) + led3 = !led3; + if (abs(newstate[0] - state[0]) > 1.0/8.0) { state[0] = newstate[0]; - int *state = analogpool.alloc(); + float *state = analogpool.alloc(); *state = state[0]; message->type = typeAnalog; message->id = 1; - message->position = state; - message->timestamp = timestamp; + message->position = (int *) state; + message->seconds = seconds; queue.put(message); } - if (abs(newstate[1] - state[1]) > 1/8) + if (abs(newstate[1] - state[1]) > 1.0/8.0) { state[1] = newstate[1]; - int *state = analogpool.alloc(); + float *state = analogpool.alloc(); *state = state[1]; message->type = typeAnalog; message->id = 2; - message->position = state; - message->timestamp = timestamp; + message->position = (int *) state; + message->seconds = seconds; queue.put(message); } i = (i + 1) % 5; @@ -91,7 +94,7 @@ bool state[2] = {false, false}; bool newstate[2] = {false, false}; bool changed[2] = {false, false}; - int timestamp = 0; + unsigned long seconds = 0; reading *message; while (true) { // synchronisation sur la période d'échantillonnage @@ -99,7 +102,7 @@ Thread::signal_wait(signal100ms); // lecture de l'étampe temporelle - timestamp = 0; + seconds = (unsigned long) difftime(begin_time, 0); // lecture des échantillons numériques newstate[0] = !en_1; @@ -109,6 +112,11 @@ changed[0] = newstate[0] != state[0]; changed[1] = newstate[1] != state[1]; + int *test = numericpool.alloc(); + *test = 42; + printf("%d", test); + numericpool.free(test); + if (changed[0] || changed [1]) { pnumeric_thread->signal_clr(signal50ms); @@ -127,7 +135,7 @@ message->type = typeNumeric; message->id = 1; message->position = state; - message->timestamp = timestamp; + message->seconds = seconds; queue.put(message); } if(newstate[1] != state[1] && changed[1]) @@ -140,7 +148,7 @@ message->type = typeNumeric; message->id = 2; message->position = state; - message->timestamp = timestamp; + message->seconds = seconds; queue.put(message); } } @@ -162,21 +170,20 @@ if (message->type == typeNumeric) { // écriture de l'événement en sortie (port série) - pc.printf("The state on numeric pin %d has changed to %d at %d\r\n", message->id, *(message->position), message->timestamp); + pc.printf("The state on numeric pin %d has changed to %d at %lus\r\n", message->id, (int *) *(message->position), message->seconds); numericpool.free(message->position); } if (message->type == typeAnalog) { // écriture de l'événement en sortie (port série) - pc.printf("The state on analog pin %d has changed to %d at %d\r\n", message->id, *(message->position), message->timestamp); - numericpool.free(message->position); + pc.printf("The state on analog pin %d has changed to %d at %lus\r\n", message->id, *(message->position), message->seconds); + analogpool.free((float *) message->position); } } } void signalnumeric() { - led3 = !led3; pnumeric_thread->signal_set(signal100ms | signal50ms); } @@ -188,15 +195,18 @@ void flip() { led4 = !led4; } -int main() { +int main(){ + // set_time(0); + begin_time = time(NULL); pc.printf("Welcome\r\n"); flipper.attach(&flip, 0.5); // initialisation du RTC - //analogtimer.attach(&signalanalog, 0.250); - numerictimer.attach(&signalnumeric, 0.05); + analogtimer.attach(&signalanalog, 0.250); + numerictimer.attach(&signalnumeric, 0.5); // démarrage des tâches - //Thread analog_thread(lecture_analog); + Thread analog_thread(lecture_analog); + panalog_thread = &analog_thread; Thread numeric_thread(lecture_num); pnumeric_thread = &numeric_thread; Thread q(collection);