Last active
February 29, 2016 03:30
-
-
Save joelsimonoff/c547acaee6f8089ac9aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Pins Defined | |
// | |
#define RDY_0 2 | |
#define REQUEST_0 3 | |
#define SS_0 5 | |
#define RDY_1 6 | |
#define REQUEST_1 9 | |
#define SS_1 10 | |
#define RDY_2 11 | |
#define REQUEST_2 12 | |
#define SS_2 A0 | |
#define RDY_3 A1 | |
#define REQUEST_3 A2 | |
#define SS_3 A3 | |
#define RDY_PIN_0 0 | |
#define REQUEST_PIN_0 1 | |
#define SS_PIN_0 2 | |
#define RDY_PIN_1 3 | |
#define REQUEST_PIN_1 4 | |
#define SS_PIN_1 5 | |
#define RDY_PIN_2 6 | |
#define REQUEST_PIN_2 7 | |
#define SS_PIN_2 8 | |
#define RDY_PIN_3 9 | |
#define REQUEST_PIN_3 10 | |
#define SS_PIN_3 11 | |
#define NUM_I_PINS 8 | |
#define NUM_O_PINS 4 | |
typedef struct{ | |
uint8_t id; | |
uint8_t hardwareVersion; | |
uint8_t softwareVersion; | |
uint8_t rdyPin; | |
uint8_t reqPin; | |
uint8_t ssPin; | |
} module; | |
const int iPins[NUM_I_PINS] = {RDY_0,REQUEST_0,RDY_1,REQUEST_1,RDY_2,REQUEST_2,RDY_3,REQUEST_3}; | |
const int oPins[NUM_O_PINS] = {SS_0, SS_1, SS_2, SS_3}; | |
int pinState[8] = {HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH}; | |
int numConnectedModules = 0; | |
module modules[5] = {NULL,NULL,NULL,NULL,NULL}; | |
// COMMON SETTINGS | |
// ---------------------------------------------------------------------------------------------- | |
// These settings are used in both SW UART, HW UART and SPI mode | |
// ---------------------------------------------------------------------------------------------- | |
#define BUFSIZE 128 // Size of the read buffer for incoming data | |
#define VERBOSE_MODE true // If set to 'true' enables debug output | |
// SHARED SPI SETTINGS | |
// ---------------------------------------------------------------------------------------------- | |
// The following macros declare the pins to use for HW and SW SPI communication. | |
// SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when | |
// using HW SPI. This should be used with nRF51822 based Bluefruit LE modules | |
// that use SPI (Bluefruit LE SPI Friend). | |
// ---------------------------------------------------------------------------------------------- | |
#define BLUEFRUIT_SPI_CS 8 | |
#define BLUEFRUIT_SPI_IRQ 7 | |
#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused | |
/********************************************************************* | |
Blue Car Version 1 | |
MIT license, check LICENSE for more information | |
All text above, and the splash screen below must be included in | |
any redistribution | |
Modified By Joel Simonoff November 2015 for BlueCar. Joels edits will | |
be covered under the MIT License. | |
*********************************************************************/ | |
#include <Arduino.h> | |
#include <SPI.h> | |
#include "Adafruit_BLE.h" | |
#include "Adafruit_BluefruitLE_SPI.h" | |
#define MODE_LED_BEHAVIOUR "MODE" | |
#define LED 13 | |
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); | |
void setup(void){ | |
pinMode(LED, OUTPUT); | |
digitalWrite(LED, LOW); | |
//while (!Serial); // required for Flora & Micro | |
delay(500); | |
Serial.begin(115200); | |
Serial.println(F("Adafruit Bluefruit Command <-> Data Mode Example")); | |
Serial.println(F("------------------------------------------------")); | |
/* Initialise the module */ | |
Serial.print(F("Initialising the Bluefruit LE module: ")); | |
if ( !ble.begin(VERBOSE_MODE) ) | |
{ | |
Serial.println(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); | |
} | |
Serial.println( F("OK!") ); | |
/* Perform a factory reset to make sure everything is in a known state */ | |
Serial.println(F("Performing a factory reset: ")); | |
if ( ! ble.factoryReset() ){ | |
Serial.println(F("Couldn't factory reset")); | |
} | |
/* Disable command echo from Bluefruit */ | |
ble.echo(false); | |
Serial.println("Requesting Bluefruit info:"); | |
/* Print Bluefruit information */ | |
ble.info(); | |
// debug info is a little annoying after this point! | |
ble.verbose(false); | |
// Set device name | |
Serial.println("Set Device Name to Blue Car"); | |
ble.sendCommandCheckOK("AT+GAPDEVNAME=Blue Car"); | |
Serial.println("Set Up Pins and Update Connections"); | |
setupPins(); | |
updateConnections(); | |
/* Wait for connection */ | |
Serial.println("Waiting For Connection"); | |
while (! ble.isConnected()) { | |
digitalWrite(LED, HIGH); | |
delay(250); | |
digitalWrite(LED, LOW); | |
delay(250); | |
} | |
Serial.println(F("******************************")); | |
// Change Mode LED Activity | |
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR)); | |
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR); | |
// Set module to DATA mode | |
Serial.println( F("Switching to DATA mode!") ); | |
ble.setMode(BLUEFRUIT_MODE_DATA); | |
Serial.println(F("******************************")); | |
} | |
// | |
// Setup Pins | |
// | |
void setupPins(){ | |
for (int i = 0; i < NUM_I_PINS; i++){ | |
pinMode(iPins[i], INPUT_PULLUP); | |
} | |
for (int i = 0; i < NUM_O_PINS; i++){ | |
pinMode(oPins[i], OUTPUT); | |
} | |
} | |
void loop(){ | |
// Check for input from serial port | |
char n, inputs[BUFSIZE+1]; | |
if (Serial.available()){ | |
n = Serial.readBytes(inputs, BUFSIZE); | |
inputs[n] = 0; | |
// Send characters to Bluefruit | |
Serial.print("Sending: "); | |
Serial.print(inputs); | |
// Send input data to host via Bluefruit | |
ble.write(inputs); | |
} | |
// Echo received data | |
while (ble.available()){ | |
processMessage(); | |
} | |
} | |
// | |
// Checks who is connected | |
// | |
void updateConnections(){ | |
bool didUpdate = false; | |
//first get pin states | |
for (int i = 0; i < NUM_I_PINS; i++){ | |
if (digitalRead(iPins[i]) != HIGH){ | |
if (pinState[i] != LOW){ | |
didUpdate = true; | |
pinState[i] = HIGH; | |
//update module info | |
if (i%2 == 0){ | |
modules[i/2] = module{NULL, NULL, NULL, i, i+1, i+2}; | |
Serial.println("Requesting data from nano"); | |
modules[i/2].id = SPI.transfer(0x01); | |
Serial.println(modules[i/2].id); | |
modules[i/2].hardwareVersion = SPI.transfer(0x02); | |
modules[i/2].softwareVersion = SPI.transfer(0x03); | |
} | |
} | |
} else { | |
if (pinState[i] != HIGH){ | |
didUpdate = true; | |
pinState[i] = LOW; | |
} | |
} | |
} | |
//update number of connected modules | |
if (didUpdate){ | |
numConnectedModules = 0; | |
for (int i = 0; i < NUM_I_PINS; i+=2){ | |
if (pinState[i] = LOW){ | |
numConnectedModules++; | |
} | |
} | |
Serial.println("----------- Did Update Connections -----------"); | |
Serial.print("Number of connected modules: "); | |
Serial.println(numConnectedModules); | |
} | |
} | |
// | |
// Handles a message | |
// | |
void processMessage(){ | |
byte c = ble.read(); | |
Serial.print("MessageID: "); | |
Serial.println(c, HEX); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, this is public :)