S5Team
/
HelloWorld
none
Fork of HelloWorld by
main.cpp@2:8d4c217c39e5, 2015-01-26 (annotated)
- Committer:
- JacquesEd
- Date:
- Mon Jan 26 21:56:51 2015 +0000
- Revision:
- 2:8d4c217c39e5
- Parent:
- 0:fb6bbc10ffa0
- Child:
- 3:58803195db3f
suck my D, and by D i mean D**k
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:fb6bbc10ffa0 | 1 | #include "mbed.h" |
JacquesEd | 2:8d4c217c39e5 | 2 | //#include "rtos.h" |
JacquesEd | 2:8d4c217c39e5 | 3 | DigitalIn en_1(p15); |
JacquesEd | 2:8d4c217c39e5 | 4 | DigitalIn en_2(p16); |
JacquesEd | 2:8d4c217c39e5 | 5 | AnalogIn ea_1(p19); |
JacquesEd | 2:8d4c217c39e5 | 6 | AnalogIn ea_2(p20); |
JacquesEd | 2:8d4c217c39e5 | 7 | Serial pc (USBTX, USBRX); |
JacquesEd | 2:8d4c217c39e5 | 8 | |
JacquesEd | 2:8d4c217c39e5 | 9 | bool event_n = false; |
JacquesEd | 2:8d4c217c39e5 | 10 | bool event_a = false; |
JacquesEd | 2:8d4c217c39e5 | 11 | |
JacquesEd | 2:8d4c217c39e5 | 12 | //RtosTimer time ; |
JacquesEd | 2:8d4c217c39e5 | 13 | |
JacquesEd | 2:8d4c217c39e5 | 14 | int date = 0; |
JacquesEd | 2:8d4c217c39e5 | 15 | |
JacquesEd | 2:8d4c217c39e5 | 16 | int taille = 100; |
JacquesEd | 2:8d4c217c39e5 | 17 | int compteur = 0; |
JacquesEd | 2:8d4c217c39e5 | 18 | int index = 0; |
JacquesEd | 2:8d4c217c39e5 | 19 | int tableau[100][2]; |
JacquesEd | 2:8d4c217c39e5 | 20 | |
JacquesEd | 2:8d4c217c39e5 | 21 | int flag[2] = {0,1}; |
simon | 0:fb6bbc10ffa0 | 22 | |
JacquesEd | 2:8d4c217c39e5 | 23 | void lecture_analog(void const *args) |
JacquesEd | 2:8d4c217c39e5 | 24 | { |
JacquesEd | 2:8d4c217c39e5 | 25 | int sample[6][2]; |
JacquesEd | 2:8d4c217c39e5 | 26 | int i = 0; |
JacquesEd | 2:8d4c217c39e5 | 27 | for (i = 0; i <6; i ++) { |
JacquesEd | 2:8d4c217c39e5 | 28 | sample[i][0] = args.read_u16(); |
JacquesEd | 2:8d4c217c39e5 | 29 | // sample[i][1] = date; |
JacquesEd | 2:8d4c217c39e5 | 30 | } |
JacquesEd | 2:8d4c217c39e5 | 31 | i = 0; |
JacquesEd | 2:8d4c217c39e5 | 32 | while (true) { |
JacquesEd | 2:8d4c217c39e5 | 33 | |
JacquesEd | 2:8d4c217c39e5 | 34 | // synchronisation sur la période d'échantillonnage |
JacquesEd | 2:8d4c217c39e5 | 35 | flag[0] = 1; |
JacquesEd | 2:8d4c217c39e5 | 36 | while (flag[1] == 1) |
JacquesEd | 2:8d4c217c39e5 | 37 | thread.yield(); |
JacquesEd | 2:8d4c217c39e5 | 38 | |
JacquesEd | 2:8d4c217c39e5 | 39 | // lecture de l'étampe temporelle |
JacquesEd | 2:8d4c217c39e5 | 40 | //date = time.read_ms(); |
JacquesEd | 2:8d4c217c39e5 | 41 | // lecture des échantillons analogiques |
JacquesEd | 2:8d4c217c39e5 | 42 | sample[i][0] = args.read_u16(); |
JacquesEd | 2:8d4c217c39e5 | 43 | i++; |
JacquesEd | 2:8d4c217c39e5 | 44 | |
JacquesEd | 2:8d4c217c39e5 | 45 | |
JacquesEd | 2:8d4c217c39e5 | 46 | // calcul de la nouvelle moyenne courante |
JacquesEd | 2:8d4c217c39e5 | 47 | if ((sample[i][0] - sample[(i+5)%6][0]) > 0x9FFF) { |
JacquesEd | 2:8d4c217c39e5 | 48 | tableau[i][0] = 1; // 1 pr analog |
JacquesEd | 2:8d4c217c39e5 | 49 | //tableau[i][1] = date; |
JacquesEd | 2:8d4c217c39e5 | 50 | } |
simon | 0:fb6bbc10ffa0 | 51 | |
JacquesEd | 2:8d4c217c39e5 | 52 | // génération éventuelle d'un événement |
JacquesEd | 2:8d4c217c39e5 | 53 | if (i >= 6) i = 0; |
JacquesEd | 2:8d4c217c39e5 | 54 | } |
JacquesEd | 2:8d4c217c39e5 | 55 | flag[0] = 0; |
JacquesEd | 2:8d4c217c39e5 | 56 | } |
JacquesEd | 2:8d4c217c39e5 | 57 | |
JacquesEd | 2:8d4c217c39e5 | 58 | void lecture_num(void const *args) |
JacquesEd | 2:8d4c217c39e5 | 59 | { |
JacquesEd | 2:8d4c217c39e5 | 60 | flag[1] = 1; |
JacquesEd | 2:8d4c217c39e5 | 61 | while (flag[0] == 1) |
JacquesEd | 2:8d4c217c39e5 | 62 | thread.yield(); |
JacquesEd | 2:8d4c217c39e5 | 63 | int value[2][2]; |
JacquesEd | 2:8d4c217c39e5 | 64 | int i = 0; |
JacquesEd | 2:8d4c217c39e5 | 65 | value[i][0] = args.read(); |
JacquesEd | 2:8d4c217c39e5 | 66 | //value[i][1] = date; |
JacquesEd | 2:8d4c217c39e5 | 67 | while (true) { |
JacquesEd | 2:8d4c217c39e5 | 68 | // synchronisation sur la période d'échantillonnage |
JacquesEd | 2:8d4c217c39e5 | 69 | |
JacquesEd | 2:8d4c217c39e5 | 70 | // lecture de l'étampe temporelle |
JacquesEd | 2:8d4c217c39e5 | 71 | //date = time.read_ms(); |
JacquesEd | 2:8d4c217c39e5 | 72 | // lecture des échantillons numériques |
JacquesEd | 2:8d4c217c39e5 | 73 | wait_ms(100); |
JacquesEd | 2:8d4c217c39e5 | 74 | i++; |
JacquesEd | 2:8d4c217c39e5 | 75 | value[i][0] = args.read(); |
JacquesEd | 2:8d4c217c39e5 | 76 | //value[i][1] = date; |
JacquesEd | 2:8d4c217c39e5 | 77 | // prise en charge du phénomène de rebond |
JacquesEd | 2:8d4c217c39e5 | 78 | if (value[0][0] != value[1][0]) { |
JacquesEd | 2:8d4c217c39e5 | 79 | wait_ms(50); |
JacquesEd | 2:8d4c217c39e5 | 80 | int *temp = new int; |
JacquesEd | 2:8d4c217c39e5 | 81 | temp = args.read(); |
JacquesEd | 2:8d4c217c39e5 | 82 | // génération éventuelle d'un événement |
JacquesEd | 2:8d4c217c39e5 | 83 | if (temp == value[i]) { |
JacquesEd | 2:8d4c217c39e5 | 84 | tableau[compteur][0] = 0; //0 pr numérique |
JacquesEd | 2:8d4c217c39e5 | 85 | // tableau[compteur][1] = value[i][1]; |
JacquesEd | 2:8d4c217c39e5 | 86 | compteur++; |
JacquesEd | 2:8d4c217c39e5 | 87 | } |
JacquesEd | 2:8d4c217c39e5 | 88 | wait_ms(50); |
JacquesEd | 2:8d4c217c39e5 | 89 | } |
JacquesEd | 2:8d4c217c39e5 | 90 | if (i >=2) i = 0; |
JacquesEd | 2:8d4c217c39e5 | 91 | } |
JacquesEd | 2:8d4c217c39e5 | 92 | flag[1] = 0; |
JacquesEd | 2:8d4c217c39e5 | 93 | } |
JacquesEd | 2:8d4c217c39e5 | 94 | |
JacquesEd | 2:8d4c217c39e5 | 95 | void collection(void const *args) |
JacquesEd | 2:8d4c217c39e5 | 96 | { |
JacquesEd | 2:8d4c217c39e5 | 97 | int i = 0; |
JacquesEd | 2:8d4c217c39e5 | 98 | while (true) { |
JacquesEd | 2:8d4c217c39e5 | 99 | // attente et lecture d'un événement |
JacquesEd | 2:8d4c217c39e5 | 100 | |
JacquesEd | 2:8d4c217c39e5 | 101 | |
JacquesEd | 2:8d4c217c39e5 | 102 | // écriture de l'événement en sortie (port série) |
JacquesEd | 2:8d4c217c39e5 | 103 | if (tableau[i][0] == 0) |
JacquesEd | 2:8d4c217c39e5 | 104 | pc.printf("event_n at: "); |
JacquesEd | 2:8d4c217c39e5 | 105 | else |
JacquesEd | 2:8d4c217c39e5 | 106 | pc.printf("event_a at: "); |
JacquesEd | 2:8d4c217c39e5 | 107 | pc.printf ("%i", tableau[i][1]); |
JacquesEd | 2:8d4c217c39e5 | 108 | i++; |
simon | 0:fb6bbc10ffa0 | 109 | } |
simon | 0:fb6bbc10ffa0 | 110 | } |
JacquesEd | 2:8d4c217c39e5 | 111 | int main() |
JacquesEd | 2:8d4c217c39e5 | 112 | { |
JacquesEd | 2:8d4c217c39e5 | 113 | // initialisation du RTC |
JacquesEd | 2:8d4c217c39e5 | 114 | // time.start(); |
JacquesEd | 2:8d4c217c39e5 | 115 | // démarrage des tâches |
JacquesEd | 2:8d4c217c39e5 | 116 | while(1) { |
JacquesEd | 2:8d4c217c39e5 | 117 | // creation des threads |
JacquesEd | 2:8d4c217c39e5 | 118 | Thread event_n1 = new Thread(new lecture_num(en_1)); |
JacquesEd | 2:8d4c217c39e5 | 119 | Thread event_n2 = new Thread(new lecture_num(en_2)); |
JacquesEd | 2:8d4c217c39e5 | 120 | Thread event_a1 = new Thread(new lecture_analog(ea_1)); |
JacquesEd | 2:8d4c217c39e5 | 121 | Thread event_a2 = new Thread(new lecture_analog(ea_2)); |
JacquesEd | 2:8d4c217c39e5 | 122 | |
JacquesEd | 2:8d4c217c39e5 | 123 | event_n1.start(); |
JacquesEd | 2:8d4c217c39e5 | 124 | event_n2.start(); |
JacquesEd | 2:8d4c217c39e5 | 125 | event_a1.start(); |
JacquesEd | 2:8d4c217c39e5 | 126 | event_a2.start(); |
JacquesEd | 2:8d4c217c39e5 | 127 | } |
JacquesEd | 2:8d4c217c39e5 | 128 | // time.stop(); |
JacquesEd | 2:8d4c217c39e5 | 129 | } |