| | 126 | == Communication entre Capteurs et Base == |
| | 127 | |
| | 128 | Pour cet exercice, vous allez faire communiquer deux arduinos, l'un en capteur, l'autre en afficheur. |
| | 129 | Pour vous aider, je vais vous donner la structure de l'application. |
| | 130 | |
| | 131 | ** Application capteur ** |
| | 132 | |
| | 133 | {{{#!c |
| | 134 | #include <SPI.h> |
| | 135 | #include <Wire.h> |
| | 136 | #include <Adafruit_GFX.h> |
| | 137 | #include <Adafruit_SSD1306.h> |
| | 138 | #include "RF24.h" |
| | 139 | #include "printf.h" |
| | 140 | |
| | 141 | // unsigned int waitFor(timer, period) |
| | 142 | // Timer pour taches périodique |
| | 143 | // arguments : |
| | 144 | // - timer : numéro de timer entre 0 et MAX_WAIT_FOR_TIMER-1 |
| | 145 | // - period : période souhaitée |
| | 146 | // retour : |
| | 147 | // - nombre de période écoulée depuis le dernier appel |
| | 148 | // |
| | 149 | #define MAX_WAIT_FOR_TIMER 16 |
| | 150 | unsigned int waitFor(int timer, unsigned long period){ |
| | 151 | static unsigned long waitForTimer[MAX_WAIT_FOR_TIMER]; |
| | 152 | unsigned long newTime = micros() / period; |
| | 153 | int delta = newTime - waitForTimer[timer]; |
| | 154 | if ( delta < 0 ) delta += 1 + (0xFFFFFFFF / period); |
| | 155 | if ( delta ) waitForTimer[timer] = newTime; |
| | 156 | return delta; |
| | 157 | } |
| | 158 | |
| | 159 | // Configuration des broches |
| | 160 | #define PIN_LUMI 1 |
| | 161 | #define OLED_RESET 4 |
| | 162 | Adafruit_SSD1306 display(OLED_RESET); |
| | 163 | RF24 radio(9,10); // radio(CE,CS) |
| | 164 | byte addresses[][6] = {/* a completer */}; |
| | 165 | |
| | 166 | // Variables globales pour la communication inter-taches |
| | 167 | byte lumi, lumiFull; |
| | 168 | |
| | 169 | void Lumi (int timer, unsigned long period, byte pin, byte *lumi, byte *lumiFull) |
| | 170 | { |
| | 171 | // a completer |
| | 172 | } |
| | 173 | |
| | 174 | void SensRF (byte *mess, byte *full) { |
| | 175 | // a completer |
| | 176 | } |
| | 177 | |
| | 178 | void setup() { |
| | 179 | Serial.begin(115200); |
| | 180 | display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) |
| | 181 | display.clearDisplay(); |
| | 182 | display.setTextSize(1); |
| | 183 | display.setTextColor(WHITE); |
| | 184 | |
| | 185 | printf_begin(); |
| | 186 | radio.begin(); |
| | 187 | radio.setPALevel(RF24_PA_LOW); |
| | 188 | radio.openWritingPipe(addresses[1]); |
| | 189 | radio.printDetails(); |
| | 190 | } |
| | 191 | |
| | 192 | void loop() { |
| | 193 | Lumi (0,1000000, PIN_LUMI, &lumi, &lumiFull); |
| | 194 | SensRF (&lumi, &lumiFull); |
| | 195 | } |
| | 196 | }}} |
| | 197 | |
| | 198 | ** Application base ** |
| | 199 | |
| | 200 | {{{#!c |
| | 201 | #include <SPI.h> |
| | 202 | #include <Wire.h> |
| | 203 | #include <Adafruit_GFX.h> |
| | 204 | #include <Adafruit_SSD1306.h> |
| | 205 | #include "RF24.h" |
| | 206 | #include "printf.h" |
| | 207 | |
| | 208 | // unsigned int waitFor(timer, period) |
| | 209 | // Timer pour taches périodique |
| | 210 | // arguments : |
| | 211 | // - timer : numéro de timer entre 0 et MAX_WAIT_FOR_TIMER-1 |
| | 212 | // - period : période souhaitée |
| | 213 | // retour : |
| | 214 | // - nombre de période écoulée depuis le dernier appel |
| | 215 | // |
| | 216 | #define MAX_WAIT_FOR_TIMER 16 |
| | 217 | unsigned int waitFor(int timer, unsigned long period){ |
| | 218 | static unsigned long waitForTimer[MAX_WAIT_FOR_TIMER]; |
| | 219 | unsigned long newTime = micros() / period; |
| | 220 | int delta = newTime - waitForTimer[timer]; |
| | 221 | if ( delta < 0 ) delta += 1 + (0xFFFFFFFF / period); |
| | 222 | if ( delta ) waitForTimer[timer] = newTime; |
| | 223 | return delta; |
| | 224 | } |
| | 225 | |
| | 226 | // Configuration des broches |
| | 227 | #define PIN_LUMI 1 |
| | 228 | #define OLED_RESET 4 |
| | 229 | Adafruit_SSD1306 display(OLED_RESET); |
| | 230 | RF24 radio(9,10); |
| | 231 | byte addresses[][6] = {"0Sens","1Sens","2Sens","3Sens","4Node","5Sens"}; |
| | 232 | |
| | 233 | // Variables globales pour la communication inter-taches |
| | 234 | byte lumi, lumiFull; |
| | 235 | |
| | 236 | void BaseRF (byte *lumi, byte *lumiFull) |
| | 237 | { |
| | 238 | // a completer |
| | 239 | } |
| | 240 | |
| | 241 | void Oled1 (byte *mess, byte *full) { |
| | 242 | // a completer |
| | 243 | } |
| | 244 | |
| | 245 | void setup() { |
| | 246 | Serial.begin(115200); |
| | 247 | |
| | 248 | display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) |
| | 249 | display.clearDisplay(); |
| | 250 | display.setTextSize(1); |
| | 251 | display.setTextColor(WHITE); |
| | 252 | |
| | 253 | printf_begin(); |
| | 254 | radio.begin(); |
| | 255 | radio.setPALevel(RF24_PA_LOW); |
| | 256 | radio.openReadingPipe(1,addresses[1]); |
| | 257 | radio.startListening(); |
| | 258 | radio.printDetails(); |
| | 259 | } |
| | 260 | |
| | 261 | void loop() { |
| | 262 | BaseRF(&lumi, &lumiFull); |
| | 263 | Oled1 (&lumi, &lumiFull); |
| | 264 | }}}} |
| | 265 | |