Components and supplies
Arduino UNO
BLE Shield
WS2811 Addressable LED strip (1m, 60 LEDs)
Project description
Code
Code snippet #2
arduino
1 case 'N': // set PWM 2 { 3 byte pin = ble_read(); 4 byte value = ble_read(); 5 6 /*______________Mods to use it as light controller____________*/ 7 if(pin == RED) 8 { 9 redValue = value; 10 EEPROM.write(0, redValue); 11 } 12 13 if(pin == GREEN) 14 { 15 greenValue = value; 16 EEPROM.write(1, greenValue); 17 } 18 19 if(pin == BLUE) 20 { 21 blueValue = value; 22 EEPROM.write(2, blueValue); 23 } 24 if (pin == LEDsPin || pin == RED || pin == GREEN || pin == BLUE) //refresh the light only if something change 25 { 26 for (int i = 0; i < NUMPIXELS; i++) { 27 // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 28 pixels.setPixelColor(i, pixels.Color(redValue, greenValue, blueValue)); 29 pixels.show(); // This sends the updated pixel color to the hardware. 30 delayMicroseconds(500); // Delay for a period of time 31 } 32 } 33 34 /*____________________________________________________________*/ 35 36 analogWrite(PIN_TO_PWM(pin), value); 37 pin_pwm[pin] = value; 38 reportPinPWMData(pin); 39 } 40 break;
Code snippet #3
arduino
1/* 2 3 Copyright (c) 2012, 2013 RedBearLab 4 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 7 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 11 modified on 01/12/2014 by 12 Arturo Guadalupi 13 <a.guadalupi@arduino.cc> 14 */ 15 16#include <Servo.h> 17#include <SPI.h> 18#include <boards.h> 19#include <RBL_nRF8001.h> 20#include <services.h> 21#include "Boards.h" 22 23/*______________Mods to use it as light controller____________*/ 24#include <Adafruit_NeoPixel.h> 25#include <EEPROM.h> //EEPROM is used to store user's value in order to have the chosen light at startup 26/*____________________________________________________________*/ 27 28#define PROTOCOL_MAJOR_VERSION 0 // 29#define PROTOCOL_MINOR_VERSION 0 // 30#define PROTOCOL_BUGFIX_VERSION 2 // bugfix 31 32#define PIN_CAPABILITY_NONE 0x00 33#define PIN_CAPABILITY_DIGITAL 0x01 34#define PIN_CAPABILITY_ANALOG 0x02 35#define PIN_CAPABILITY_PWM 0x04 36#define PIN_CAPABILITY_SERVO 0x08 37#define PIN_CAPABILITY_I2C 0x10 38 39// pin modes 40//#define INPUT 0x00 // defined in wiring.h 41//#define OUTPUT 0x01 // defined in wiring.h 42#define ANALOG 0x02 // analog pin in analogInput mode 43#define PWM 0x03 // digital pin in PWM output mode 44#define SERVO 0x04 // digital pin in Servo output mode 45 46byte pin_mode[TOTAL_PINS]; 47byte pin_state[TOTAL_PINS]; 48byte pin_pwm[TOTAL_PINS]; 49byte pin_servo[TOTAL_PINS]; 50 51Servo servos[MAX_SERVOS]; 52 53 54/*______________Mods to use it as light controller____________*/ 55const int RED = 3; 56const int GREEN = 5; 57const int BLUE = 6; 58 59const int NUMPIXELS = 60; 60const int LEDsPin = 9; // LEDs connected to digital pin 9 61 62int redValue = 0, greenValue = 0, blueValue = 0; 63 64Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LEDsPin, NEO_GRB + NEO_KHZ800); 65/*____________________________________________________________*/ 66 67void setup() 68{ 69 Serial.begin(57600); 70 Serial.println("BLE Arduino Slave"); 71 72 /* Default all to digital input */ 73 for (int pin = 0; pin < TOTAL_PINS; pin++) 74 { 75 /*______________Mods to use it as light controller____________*/ 76 if (pin == LEDsPin || pin == RED || pin == GREEN || pin == BLUE) //these pin are needed for the project and we don't want to change them 77 pin++; //skip them 78 /*____________________________________________________________*/ 79 80 // Set pin to input with internal pull up 81 pinMode(pin, INPUT); 82 digitalWrite(pin, HIGH); 83 84 // Save pin mode and state 85 pin_mode[pin] = INPUT; 86 pin_state[pin] = LOW; 87 } 88 89 // Default pins set to 9 and 8 for REQN and RDYN 90 // Set your REQN and RDYN here before ble_begin() if you need 91 //ble_set_pins(3, 2); 92 93 // Set your BLE Shield name here, max. length 10 94 //ble_set_name("My Name"); 95 96 // Init. and start BLE library. 97 ble_begin(); 98 99 /*______________Mods to use it as light controller____________*/ 100 pinMode(RED, OUTPUT); 101 pinMode(GREEN, OUTPUT); 102 pinMode(BLUE, OUTPUT); 103 pinMode(LEDsPin, OUTPUT); 104 105 redValue = EEPROM.read(0); 106 greenValue = EEPROM.read(1); 107 blueValue = EEPROM.read(2); 108 109 110 pin_pwm[RED] = redValue; 111 reportPinPWMData(RED); 112 pin_mode[RED] = PWM; 113 114 pin_pwm[GREEN] = greenValue; 115 reportPinPWMData(GREEN); 116 pin_mode[GREEN] = PWM; 117 118 pin_pwm[BLUE] = blueValue; 119 reportPinPWMData(BLUE); 120 pin_mode[BLUE] = PWM; 121 122 pin_mode[LEDsPin] = OUTPUT; 123 124 125 for (int i = 0; i < NUMPIXELS; i++) { 126 // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 127 pixels.setPixelColor(i, pixels.Color(redValue, greenValue, blueValue)); 128 pixels.show(); // This sends the updated pixel color to the hardware. 129 delayMicroseconds(500); // Delay for a period of time 130 } 131 132 /*____________________________________________________________*/ 133 134 135} 136 137static byte buf_len = 0; 138 139void ble_write_string(byte *bytes, uint8_t len) 140{ 141 if (buf_len + len > 20) 142 { 143 for (int j = 0; j < 15000; j++) 144 ble_do_events(); 145 146 buf_len = 0; 147 } 148 149 for (int j = 0; j < len; j++) 150 { 151 ble_write(bytes[j]); 152 buf_len++; 153 } 154 155 if (buf_len == 20) 156 { 157 for (int j = 0; j < 15000; j++) 158 ble_do_events(); 159 160 buf_len = 0; 161 } 162} 163 164byte reportDigitalInput() 165{ 166 if (!ble_connected()) 167 return 0; 168 169 static byte pin = 0; 170 byte report = 0; 171 172 if (!IS_PIN_DIGITAL(pin)) 173 { 174 pin++; 175 if (pin >= TOTAL_PINS) 176 pin = 0; 177 return 0; 178 } 179 180 if (pin_mode[pin] == INPUT) 181 { 182 byte current_state = digitalRead(pin); 183 184 if (pin_state[pin] != current_state) 185 { 186 pin_state[pin] = current_state; 187 byte buf[] = { 188 'G', pin, INPUT, current_state }; 189 ble_write_string(buf, 4); 190 191 report = 1; 192 } 193 } 194 195 pin++; 196 if (pin >= TOTAL_PINS) 197 pin = 0; 198 199 return report; 200} 201 202void reportPinCapability(byte pin) 203{ 204 byte buf[] = { 205 'P', pin, 0x00 }; 206 byte pin_cap = 0; 207 208 if (IS_PIN_DIGITAL(pin)) 209 pin_cap |= PIN_CAPABILITY_DIGITAL; 210 211 if (IS_PIN_ANALOG(pin)) 212 pin_cap |= PIN_CAPABILITY_ANALOG; 213 214 if (IS_PIN_PWM(pin)) 215 pin_cap |= PIN_CAPABILITY_PWM; 216 217 if (IS_PIN_SERVO(pin)) 218 pin_cap |= PIN_CAPABILITY_SERVO; 219 220 buf[2] = pin_cap; 221 ble_write_string(buf, 3); 222} 223 224void reportPinServoData(byte pin) 225{ 226 // if (IS_PIN_SERVO(pin)) 227 // servos[PIN_TO_SERVO(pin)].write(value); 228 // pin_servo[pin] = value; 229 230 byte value = pin_servo[pin]; 231 byte mode = pin_mode[pin]; 232 byte buf[] = { 233 'G', pin, mode, value }; 234 ble_write_string(buf, 4); 235} 236 237byte reportPinAnalogData() 238{ 239 if (!ble_connected()) 240 return 0; 241 242 static byte pin = 0; 243 byte report = 0; 244 245 if (!IS_PIN_DIGITAL(pin)) 246 { 247 pin++; 248 if (pin >= TOTAL_PINS) 249 pin = 0; 250 return 0; 251 } 252 253 if (pin_mode[pin] == ANALOG) 254 { 255 uint16_t value = analogRead(pin); 256 byte value_lo = value; 257 byte value_hi = value>>8; 258 259 byte mode = pin_mode[pin]; 260 mode = (value_hi << 4) | mode; 261 262 byte buf[] = { 263 'G', pin, mode, value_lo }; 264 ble_write_string(buf, 4); 265 } 266 267 pin++; 268 if (pin >= TOTAL_PINS) 269 pin = 0; 270 271 return report; 272} 273 274void reportPinDigitalData(byte pin) 275{ 276 byte state = digitalRead(pin); 277 byte mode = pin_mode[pin]; 278 byte buf[] = { 279 'G', pin, mode, state }; 280 ble_write_string(buf, 4); 281} 282 283void reportPinPWMData(byte pin) 284{ 285 byte value = pin_pwm[pin]; 286 byte mode = pin_mode[pin]; 287 byte buf[] = { 288 'G', pin, mode, value }; 289 ble_write_string(buf, 4); 290} 291 292void sendCustomData(uint8_t *buf, uint8_t len) 293{ 294 uint8_t data[20] = "Z"; 295 memcpy(&data[1], buf, len); 296 ble_write_string(data, len+1); 297} 298 299byte queryDone = false; 300 301void loop() 302{ 303 while(ble_available()) 304 { 305 byte cmd; 306 cmd = ble_read(); 307 Serial.write(cmd); 308 309 // Parse data here 310 switch (cmd) 311 { 312 case 'V': // query protocol version 313 { 314 byte buf[] = { 315 'V', 0x00, 0x00, 0x01 }; 316 ble_write_string(buf, 4); 317 } 318 break; 319 320 case 'C': // query board total pin count 321 { 322 byte buf[2]; 323 buf[0] = 'C'; 324 buf[1] = TOTAL_PINS; 325 ble_write_string(buf, 2); 326 } 327 break; 328 329 case 'M': // query pin mode 330 { 331 byte pin = ble_read(); 332 byte buf[] = { 333 'M', pin, pin_mode[pin] }; // report pin mode 334 ble_write_string(buf, 3); 335 } 336 break; 337 338 case 'S': // set pin mode 339 { 340 byte pin = ble_read(); 341 byte mode = ble_read(); 342 343 /*______________Mods to use it as light controller____________*/ 344 if (pin == LEDsPin || pin == RED || pin == GREEN || pin == BLUE) //these pin are needed for the project and we don't want to change them 345 break; 346 /*____________________________________________________________*/ 347 348 if (IS_PIN_SERVO(pin) && mode != SERVO && servos[PIN_TO_SERVO(pin)].attached()) 349 servos[PIN_TO_SERVO(pin)].detach(); 350 351 /* ToDo: check the mode is in its capability or not */ 352 /* assume always ok */ 353 if (mode != pin_mode[pin]) 354 { 355 pinMode(pin, mode); 356 pin_mode[pin] = mode; 357 358 if (mode == OUTPUT) 359 { 360 digitalWrite(pin, LOW); 361 pin_state[pin] = LOW; 362 } 363 else if (mode == INPUT) 364 { 365 digitalWrite(pin, HIGH); 366 pin_state[pin] = HIGH; 367 } 368 else if (mode == ANALOG) 369 { 370 if (IS_PIN_ANALOG(pin)) { 371 if (IS_PIN_DIGITAL(pin)) { 372 pinMode(PIN_TO_DIGITAL(pin), LOW); 373 } 374 } 375 } 376 else if (mode == PWM) 377 { 378 if (IS_PIN_PWM(pin)) 379 { 380 pinMode(PIN_TO_PWM(pin), OUTPUT); 381 analogWrite(PIN_TO_PWM(pin), 0); 382 pin_pwm[pin] = 0; 383 pin_mode[pin] = PWM; 384 } 385 } 386 else if (mode == SERVO) 387 { 388 if (IS_PIN_SERVO(pin)) 389 { 390 pin_servo[pin] = 0; 391 pin_mode[pin] = SERVO; 392 if (!servos[PIN_TO_SERVO(pin)].attached()) 393 servos[PIN_TO_SERVO(pin)].attach(PIN_TO_DIGITAL(pin)); 394 } 395 } 396 } 397 398 // if (mode == ANALOG) 399 // reportPinAnalogData(pin); 400 if ( (mode == INPUT) || (mode == OUTPUT) ) 401 reportPinDigitalData(pin); 402 else if (mode == PWM) 403 reportPinPWMData(pin); 404 else if (mode == SERVO) 405 reportPinServoData(pin); 406 } 407 break; 408 409 case 'G': // query pin data 410 { 411 byte pin = ble_read(); 412 reportPinDigitalData(pin); 413 } 414 break; 415 416 case 'T': // set pin digital state 417 { 418 byte pin = ble_read(); 419 byte state = ble_read(); 420 421 digitalWrite(pin, state); 422 reportPinDigitalData(pin); 423 } 424 break; 425 426 case 'N': // set PWM 427 { 428 byte pin = ble_read(); 429 byte value = ble_read(); 430 431 /*______________Mods to use it as light controller____________*/ 432 if(pin == RED) 433 { 434 redValue = value; 435 EEPROM.write(0, redValue); 436 } 437 438 if(pin == GREEN) 439 { 440 greenValue = value; 441 EEPROM.write(1, greenValue); 442 } 443 444 if(pin == BLUE) 445 { 446 blueValue = value; 447 EEPROM.write(2, blueValue); 448 } 449 if (pin == LEDsPin || pin == RED || pin == GREEN || pin == BLUE) //refresh the light only if something change 450 { 451 for (int i = 0; i < NUMPIXELS; i++) { 452 // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 453 pixels.setPixelColor(i, pixels.Color(redValue, greenValue, blueValue)); 454 pixels.show(); // This sends the updated pixel color to the hardware. 455 delayMicroseconds(500); // Delay for a period of time 456 } 457 } 458 459 /*____________________________________________________________*/ 460 461 analogWrite(PIN_TO_PWM(pin), value); 462 pin_pwm[pin] = value; 463 reportPinPWMData(pin); 464 } 465 break; 466 467 case 'O': // set Servo 468 { 469 byte pin = ble_read(); 470 byte value = ble_read(); 471 472 if (IS_PIN_SERVO(pin)) 473 servos[PIN_TO_SERVO(pin)].write(value); 474 pin_servo[pin] = value; 475 reportPinServoData(pin); 476 } 477 break; 478 479 case 'A': // query all pin status 480 for (int pin = 0; pin < TOTAL_PINS; pin++) 481 { 482 reportPinCapability(pin); 483 if ( (pin_mode[pin] == INPUT) || (pin_mode[pin] == OUTPUT) ) 484 reportPinDigitalData(pin); 485 else if (pin_mode[pin] == PWM) 486 reportPinPWMData(pin); 487 else if (pin_mode[pin] == SERVO) 488 reportPinServoData(pin); 489 } 490 491 queryDone = true; 492 { 493 uint8_t str[] = "ABC"; 494 sendCustomData(str, 3); 495 } 496 497 break; 498 499 case 'P': // query pin capability 500 { 501 byte pin = ble_read(); 502 reportPinCapability(pin); 503 } 504 break; 505 506 case 'Z': 507 { 508 byte len = ble_read(); 509 byte buf[len]; 510 for (int i=0;i<len;i++) 511 buf[i] = ble_read(); 512 Serial.println("->"); 513 Serial.print("Received: "); 514 Serial.print(len); 515 Serial.println(" byte(s)"); 516 Serial.print(" Hex: "); 517 for (int i=0;i<len;i++) 518 Serial.print(buf[i], HEX); 519 Serial.println(); 520 } 521 } 522 523 // send out any outstanding data 524 ble_do_events(); 525 buf_len = 0; 526 527 return; // only do this task in this loop 528 } 529 530 // process text data 531 if (Serial.available()) 532 { 533 byte d = 'Z'; 534 ble_write(d); 535 536 delay(5); 537 while(Serial.available()) 538 { 539 d = Serial.read(); 540 ble_write(d); 541 } 542 543 ble_do_events(); 544 buf_len = 0; 545 546 return; 547 } 548 549 // No input data, no commands, process analog data 550 if (!ble_connected()) 551 queryDone = false; // reset query state 552 553 if (queryDone) // only report data after the query state 554 { 555 byte input_data_pending = reportDigitalInput(); 556 if (input_data_pending) 557 { 558 ble_do_events(); 559 buf_len = 0; 560 561 return; // only do this task in this loop 562 } 563 564 reportPinAnalogData(); 565 566 ble_do_events(); 567 buf_len = 0; 568 569 return; 570 } 571 572 ble_do_events(); 573 buf_len = 0; 574}
Code snippet #1
arduino
1/*______________Mods to use it as light controller____________*/ 2.... 3.... 4.... 5.... 6/*____________________________________________________________*/
Code snippet #1
arduino
1/*______________Mods to use it as light controller____________*/ 2.... 3.... 4.... 5.... 6/*____________________________________________________________*/
Code snippet #3
arduino
1/* 2 3 Copyright (c) 2012, 2013 RedBearLab 4 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 7 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 11 modified on 01/12/2014 by 12 Arturo Guadalupi 13 <a.guadalupi@arduino.cc> 14 */ 15 16#include <Servo.h> 17#include <SPI.h> 18#include <boards.h> 19#include <RBL_nRF8001.h> 20#include <services.h> 21#include "Boards.h" 22 23/*______________Mods to use it as light controller____________*/ 24#include <Adafruit_NeoPixel.h> 25#include <EEPROM.h> //EEPROM is used to store user's value in order to have the chosen light at startup 26/*____________________________________________________________*/ 27 28#define PROTOCOL_MAJOR_VERSION 0 // 29#define PROTOCOL_MINOR_VERSION 0 // 30#define PROTOCOL_BUGFIX_VERSION 2 // bugfix 31 32#define PIN_CAPABILITY_NONE 0x00 33#define PIN_CAPABILITY_DIGITAL 0x01 34#define PIN_CAPABILITY_ANALOG 0x02 35#define PIN_CAPABILITY_PWM 0x04 36#define PIN_CAPABILITY_SERVO 0x08 37#define PIN_CAPABILITY_I2C 0x10 38 39// pin modes 40//#define INPUT 0x00 // defined in wiring.h 41//#define OUTPUT 0x01 // defined in wiring.h 42#define ANALOG 0x02 // analog pin in analogInput mode 43#define PWM 0x03 // digital pin in PWM output mode 44#define SERVO 0x04 // digital pin in Servo output mode 45 46byte pin_mode[TOTAL_PINS]; 47byte pin_state[TOTAL_PINS]; 48byte pin_pwm[TOTAL_PINS]; 49byte pin_servo[TOTAL_PINS]; 50 51Servo servos[MAX_SERVOS]; 52 53 54/*______________Mods to use it as light controller____________*/ 55const int RED = 3; 56const int GREEN = 5; 57const int BLUE = 6; 58 59const int NUMPIXELS = 60; 60const int LEDsPin = 9; // LEDs connected to digital pin 9 61 62int redValue = 0, greenValue = 0, blueValue = 0; 63 64Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LEDsPin, NEO_GRB + NEO_KHZ800); 65/*____________________________________________________________*/ 66 67void setup() 68{ 69 Serial.begin(57600); 70 Serial.println("BLE Arduino Slave"); 71 72 /* Default all to digital input */ 73 for (int pin = 0; pin < TOTAL_PINS; pin++) 74 { 75 /*______________Mods to use it as light controller____________*/ 76 if (pin == LEDsPin || pin == RED || pin == GREEN || pin == BLUE) //these pin are needed for the project and we don't want to change them 77 pin++; //skip them 78 /*____________________________________________________________*/ 79 80 // Set pin to input with internal pull up 81 pinMode(pin, INPUT); 82 digitalWrite(pin, HIGH); 83 84 // Save pin mode and state 85 pin_mode[pin] = INPUT; 86 pin_state[pin] = LOW; 87 } 88 89 // Default pins set to 9 and 8 for REQN and RDYN 90 // Set your REQN and RDYN here before ble_begin() if you need 91 //ble_set_pins(3, 2); 92 93 // Set your BLE Shield name here, max. length 10 94 //ble_set_name("My Name"); 95 96 // Init. and start BLE library. 97 ble_begin(); 98 99 /*______________Mods to use it as light controller____________*/ 100 pinMode(RED, OUTPUT); 101 pinMode(GREEN, OUTPUT); 102 pinMode(BLUE, OUTPUT); 103 pinMode(LEDsPin, OUTPUT); 104 105 redValue = EEPROM.read(0); 106 greenValue = EEPROM.read(1); 107 blueValue = EEPROM.read(2); 108 109 110 pin_pwm[RED] = redValue; 111 reportPinPWMData(RED); 112 pin_mode[RED] = PWM; 113 114 pin_pwm[GREEN] = greenValue; 115 reportPinPWMData(GREEN); 116 pin_mode[GREEN] = PWM; 117 118 pin_pwm[BLUE] = blueValue; 119 reportPinPWMData(BLUE); 120 pin_mode[BLUE] = PWM; 121 122 pin_mode[LEDsPin] = OUTPUT; 123 124 125 for (int i = 0; i < NUMPIXELS; i++) { 126 // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 127 pixels.setPixelColor(i, pixels.Color(redValue, greenValue, blueValue)); 128 pixels.show(); // This sends the updated pixel color to the hardware. 129 delayMicroseconds(500); // Delay for a period of time 130 } 131 132 /*____________________________________________________________*/ 133 134 135} 136 137static byte buf_len = 0; 138 139void ble_write_string(byte *bytes, uint8_t len) 140{ 141 if (buf_len + len > 20) 142 { 143 for (int j = 0; j < 15000; j++) 144 ble_do_events(); 145 146 buf_len = 0; 147 } 148 149 for (int j = 0; j < len; j++) 150 { 151 ble_write(bytes[j]); 152 buf_len++; 153 } 154 155 if (buf_len == 20) 156 { 157 for (int j = 0; j < 15000; j++) 158 ble_do_events(); 159 160 buf_len = 0; 161 } 162} 163 164byte reportDigitalInput() 165{ 166 if (!ble_connected()) 167 return 0; 168 169 static byte pin = 0; 170 byte report = 0; 171 172 if (!IS_PIN_DIGITAL(pin)) 173 { 174 pin++; 175 if (pin >= TOTAL_PINS) 176 pin = 0; 177 return 0; 178 } 179 180 if (pin_mode[pin] == INPUT) 181 { 182 byte current_state = digitalRead(pin); 183 184 if (pin_state[pin] != current_state) 185 { 186 pin_state[pin] = current_state; 187 byte buf[] = { 188 'G', pin, INPUT, current_state }; 189 ble_write_string(buf, 4); 190 191 report = 1; 192 } 193 } 194 195 pin++; 196 if (pin >= TOTAL_PINS) 197 pin = 0; 198 199 return report; 200} 201 202void reportPinCapability(byte pin) 203{ 204 byte buf[] = { 205 'P', pin, 0x00 }; 206 byte pin_cap = 0; 207 208 if (IS_PIN_DIGITAL(pin)) 209 pin_cap |= PIN_CAPABILITY_DIGITAL; 210 211 if (IS_PIN_ANALOG(pin)) 212 pin_cap |= PIN_CAPABILITY_ANALOG; 213 214 if (IS_PIN_PWM(pin)) 215 pin_cap |= PIN_CAPABILITY_PWM; 216 217 if (IS_PIN_SERVO(pin)) 218 pin_cap |= PIN_CAPABILITY_SERVO; 219 220 buf[2] = pin_cap; 221 ble_write_string(buf, 3); 222} 223 224void reportPinServoData(byte pin) 225{ 226 // if (IS_PIN_SERVO(pin)) 227 // servos[PIN_TO_SERVO(pin)].write(value); 228 // pin_servo[pin] = value; 229 230 byte value = pin_servo[pin]; 231 byte mode = pin_mode[pin]; 232 byte buf[] = { 233 'G', pin, mode, value }; 234 ble_write_string(buf, 4); 235} 236 237byte reportPinAnalogData() 238{ 239 if (!ble_connected()) 240 return 0; 241 242 static byte pin = 0; 243 byte report = 0; 244 245 if (!IS_PIN_DIGITAL(pin)) 246 { 247 pin++; 248 if (pin >= TOTAL_PINS) 249 pin = 0; 250 return 0; 251 } 252 253 if (pin_mode[pin] == ANALOG) 254 { 255 uint16_t value = analogRead(pin); 256 byte value_lo = value; 257 byte value_hi = value>>8; 258 259 byte mode = pin_mode[pin]; 260 mode = (value_hi << 4) | mode; 261 262 byte buf[] = { 263 'G', pin, mode, value_lo }; 264 ble_write_string(buf, 4); 265 } 266 267 pin++; 268 if (pin >= TOTAL_PINS) 269 pin = 0; 270 271 return report; 272} 273 274void reportPinDigitalData(byte pin) 275{ 276 byte state = digitalRead(pin); 277 byte mode = pin_mode[pin]; 278 byte buf[] = { 279 'G', pin, mode, state }; 280 ble_write_string(buf, 4); 281} 282 283void reportPinPWMData(byte pin) 284{ 285 byte value = pin_pwm[pin]; 286 byte mode = pin_mode[pin]; 287 byte buf[] = { 288 'G', pin, mode, value }; 289 ble_write_string(buf, 4); 290} 291 292void sendCustomData(uint8_t *buf, uint8_t len) 293{ 294 uint8_t data[20] = "Z"; 295 memcpy(&data[1], buf, len); 296 ble_write_string(data, len+1); 297} 298 299byte queryDone = false; 300 301void loop() 302{ 303 while(ble_available()) 304 { 305 byte cmd; 306 cmd = ble_read(); 307 Serial.write(cmd); 308 309 // Parse data here 310 switch (cmd) 311 { 312 case 'V': // query protocol version 313 { 314 byte buf[] = { 315 'V', 0x00, 0x00, 0x01 }; 316 ble_write_string(buf, 4); 317 } 318 break; 319 320 case 'C': // query board total pin count 321 { 322 byte buf[2]; 323 buf[0] = 'C'; 324 buf[1] = TOTAL_PINS; 325 ble_write_string(buf, 2); 326 } 327 break; 328 329 case 'M': // query pin mode 330 { 331 byte pin = ble_read(); 332 byte buf[] = { 333 'M', pin, pin_mode[pin] }; // report pin mode 334 ble_write_string(buf, 3); 335 } 336 break; 337 338 case 'S': // set pin mode 339 { 340 byte pin = ble_read(); 341 byte mode = ble_read(); 342 343 /*______________Mods to use it as light controller____________*/ 344 if (pin == LEDsPin || pin == RED || pin == GREEN || pin == BLUE) //these pin are needed for the project and we don't want to change them 345 break; 346 /*____________________________________________________________*/ 347 348 if (IS_PIN_SERVO(pin) && mode != SERVO && servos[PIN_TO_SERVO(pin)].attached()) 349 servos[PIN_TO_SERVO(pin)].detach(); 350 351 /* ToDo: check the mode is in its capability or not */ 352 /* assume always ok */ 353 if (mode != pin_mode[pin]) 354 { 355 pinMode(pin, mode); 356 pin_mode[pin] = mode; 357 358 if (mode == OUTPUT) 359 { 360 digitalWrite(pin, LOW); 361 pin_state[pin] = LOW; 362 } 363 else if (mode == INPUT) 364 { 365 digitalWrite(pin, HIGH); 366 pin_state[pin] = HIGH; 367 } 368 else if (mode == ANALOG) 369 { 370 if (IS_PIN_ANALOG(pin)) { 371 if (IS_PIN_DIGITAL(pin)) { 372 pinMode(PIN_TO_DIGITAL(pin), LOW); 373 } 374 } 375 } 376 else if (mode == PWM) 377 { 378 if (IS_PIN_PWM(pin)) 379 { 380 pinMode(PIN_TO_PWM(pin), OUTPUT); 381 analogWrite(PIN_TO_PWM(pin), 0); 382 pin_pwm[pin] = 0; 383 pin_mode[pin] = PWM; 384 } 385 } 386 else if (mode == SERVO) 387 { 388 if (IS_PIN_SERVO(pin)) 389 { 390 pin_servo[pin] = 0; 391 pin_mode[pin] = SERVO; 392 if (!servos[PIN_TO_SERVO(pin)].attached()) 393 servos[PIN_TO_SERVO(pin)].attach(PIN_TO_DIGITAL(pin)); 394 } 395 } 396 } 397 398 // if (mode == ANALOG) 399 // reportPinAnalogData(pin); 400 if ( (mode == INPUT) || (mode == OUTPUT) ) 401 reportPinDigitalData(pin); 402 else if (mode == PWM) 403 reportPinPWMData(pin); 404 else if (mode == SERVO) 405 reportPinServoData(pin); 406 } 407 break; 408 409 case 'G': // query pin data 410 { 411 byte pin = ble_read(); 412 reportPinDigitalData(pin); 413 } 414 break; 415 416 case 'T': // set pin digital state 417 { 418 byte pin = ble_read(); 419 byte state = ble_read(); 420 421 digitalWrite(pin, state); 422 reportPinDigitalData(pin); 423 } 424 break; 425 426 case 'N': // set PWM 427 { 428 byte pin = ble_read(); 429 byte value = ble_read(); 430 431 /*______________Mods to use it as light controller____________*/ 432 if(pin == RED) 433 { 434 redValue = value; 435 EEPROM.write(0, redValue); 436 } 437 438 if(pin == GREEN) 439 { 440 greenValue = value; 441 EEPROM.write(1, greenValue); 442 } 443 444 if(pin == BLUE) 445 { 446 blueValue = value; 447 EEPROM.write(2, blueValue); 448 } 449 if (pin == LEDsPin || pin == RED || pin == GREEN || pin == BLUE) //refresh the light only if something change 450 { 451 for (int i = 0; i < NUMPIXELS; i++) { 452 // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 453 pixels.setPixelColor(i, pixels.Color(redValue, greenValue, blueValue)); 454 pixels.show(); // This sends the updated pixel color to the hardware. 455 delayMicroseconds(500); // Delay for a period of time 456 } 457 } 458 459 /*____________________________________________________________*/ 460 461 analogWrite(PIN_TO_PWM(pin), value); 462 pin_pwm[pin] = value; 463 reportPinPWMData(pin); 464 } 465 break; 466 467 case 'O': // set Servo 468 { 469 byte pin = ble_read(); 470 byte value = ble_read(); 471 472 if (IS_PIN_SERVO(pin)) 473 servos[PIN_TO_SERVO(pin)].write(value); 474 pin_servo[pin] = value; 475 reportPinServoData(pin); 476 } 477 break; 478 479 case 'A': // query all pin status 480 for (int pin = 0; pin < TOTAL_PINS; pin++) 481 { 482 reportPinCapability(pin); 483 if ( (pin_mode[pin] == INPUT) || (pin_mode[pin] == OUTPUT) ) 484 reportPinDigitalData(pin); 485 else if (pin_mode[pin] == PWM) 486 reportPinPWMData(pin); 487 else if (pin_mode[pin] == SERVO) 488 reportPinServoData(pin); 489 } 490 491 queryDone = true; 492 { 493 uint8_t str[] = "ABC"; 494 sendCustomData(str, 3); 495 } 496 497 break; 498 499 case 'P': // query pin capability 500 { 501 byte pin = ble_read(); 502 reportPinCapability(pin); 503 } 504 break; 505 506 case 'Z': 507 { 508 byte len = ble_read(); 509 byte buf[len]; 510 for (int i=0;i<len;i++) 511 buf[i] = ble_read(); 512 Serial.println("->"); 513 Serial.print("Received: "); 514 Serial.print(len); 515 Serial.println(" byte(s)"); 516 Serial.print(" Hex: "); 517 for (int i=0;i<len;i++) 518 Serial.print(buf[i], HEX); 519 Serial.println(); 520 } 521 } 522 523 // send out any outstanding data 524 ble_do_events(); 525 buf_len = 0; 526 527 return; // only do this task in this loop 528 } 529 530 // process text data 531 if (Serial.available()) 532 { 533 byte d = 'Z'; 534 ble_write(d); 535 536 delay(5); 537 while(Serial.available()) 538 { 539 d = Serial.read(); 540 ble_write(d); 541 } 542 543 ble_do_events(); 544 buf_len = 0; 545 546 return; 547 } 548 549 // No input data, no commands, process analog data 550 if (!ble_connected()) 551 queryDone = false; // reset query state 552 553 if (queryDone) // only report data after the query state 554 { 555 byte input_data_pending = reportDigitalInput(); 556 if (input_data_pending) 557 { 558 ble_do_events(); 559 buf_len = 0; 560 561 return; // only do this task in this loop 562 } 563 564 reportPinAnalogData(); 565 566 ble_do_events(); 567 buf_len = 0; 568 569 return; 570 } 571 572 ble_do_events(); 573 buf_len = 0; 574}
Downloadable files
untitled
untitled
untitled
untitled
Comments
Only logged in users can leave comments
Arduino_Scuola
0 Followers
•0 Projects
Table of contents
Intro
0
0