simpler UART
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
#define BUFSIZE 128 // Size of the read buffer for incoming data
|
||||
#define VERBOSE_MODE true // If set to 'true' enables debug output
|
||||
#define BLE_READPACKET_TIMEOUT 400 // Timeout in ms waiting to read a response
|
||||
#define BLE_READPACKET_TIMEOUT 10 // Timeout in ms waiting to read a response
|
||||
|
||||
|
||||
// SOFTWARE UART SETTINGS
|
||||
|
||||
@@ -12,67 +12,52 @@
|
||||
|
||||
#include "BluefruitConfig.h"
|
||||
|
||||
#define BT_SCAN_MS 200
|
||||
#define MINIMUM_FIRMWARE_VERSION "0.7.0"
|
||||
#define FACTORYRESET_ENABLE 1
|
||||
#define DIM_FACTOR 50
|
||||
|
||||
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
|
||||
float parsefloat(uint8_t *buffer);
|
||||
void printHex(const uint8_t * data, const uint32_t numBytes);
|
||||
|
||||
// the packet buffer
|
||||
extern uint8_t packetbuffer[];
|
||||
|
||||
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
|
||||
|
||||
volatile boolean irq_event_available = false;
|
||||
void DfuIrqHandle(void) {
|
||||
irq_event_available = true;
|
||||
}
|
||||
|
||||
void bluetooth_setup(){
|
||||
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?"));
|
||||
while(1); // halt
|
||||
}
|
||||
if ( FACTORYRESET_ENABLE ) {
|
||||
Serial.println(F("Performing a factory reset"));
|
||||
if ( ! ble.factoryReset() ){
|
||||
Serial.println(F("Couldn't factory reset"));
|
||||
while(1); // halt
|
||||
}
|
||||
}
|
||||
if ( !ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) ){
|
||||
Serial.print(F("Callback requires at least"));
|
||||
Serial.println(F(MINIMUM_FIRMWARE_VERSION));
|
||||
while(1); // halt
|
||||
}
|
||||
pinMode(BLUEFRUIT_SPI_IRQ, INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(BLUEFRUIT_SPI_IRQ), DfuIrqHandle, FALLING);
|
||||
ble.echo(false); // disable command echo
|
||||
//ble.echo(false); // disable command echo
|
||||
//ble.verbose(false); // disable debug info
|
||||
ble.sendCommandCheckOK(F("AT+GAPDEVNAME=Suit LEDs")); // Change name displayed in bluetooth scans
|
||||
ble.sendCommandCheckOK(F("AT+DFUIRQ=on")); // Change DFU Pin to IRQ Mode
|
||||
ble.verbose(false); // disable debug info
|
||||
//Serial.println("Requesting Bluefruit info:");
|
||||
|
||||
//ble.info();
|
||||
ble.setConnectCallback(on_connect);
|
||||
ble.setDisconnectCallback(on_disconnect);
|
||||
ble.setBleUartRxCallback(on_rx);
|
||||
ble.setBleUartRxCallback(BleUartRX);
|
||||
ble.setMode(BLUEFRUIT_MODE_DATA);
|
||||
Serial.println(F("Bluetooth ready"));
|
||||
}
|
||||
|
||||
void on_connect(){
|
||||
Serial.println(F("on_connect"));
|
||||
void on_connect(void){
|
||||
Serial.println(F("Bluetooth Connected"));
|
||||
}
|
||||
void on_disconnect(){
|
||||
Serial.println(F("on_disconnect"));
|
||||
void on_disconnect(void){
|
||||
Serial.println(F("Bluetooth Disconnected"));
|
||||
}
|
||||
void on_rx(){
|
||||
Serial.println(F("on_rx"));
|
||||
/*uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
|
||||
if (len == 0) return;
|
||||
Serial.println(F("data"));
|
||||
if (packetbuffer[1] == 'P'){
|
||||
Serial.println(char(packetbuffer[2]));
|
||||
} else {
|
||||
printHex(packetbuffer, len);
|
||||
}*/
|
||||
|
||||
void BleUartRX(char data[], uint16_t len){
|
||||
Serial.print( F("[BLE UART RX] " ) );
|
||||
Serial.write(data, len);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
uint32_t pack_color(uint8_t r, uint8_t g, uint8_t b) {
|
||||
@@ -81,18 +66,6 @@ uint32_t pack_color(uint8_t r, uint8_t g, uint8_t b) {
|
||||
uint32_t pack_color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
|
||||
return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
|
||||
}
|
||||
uint8_t red(uint32_t color){
|
||||
return (color >> 16) & 0xFF;
|
||||
}
|
||||
uint8_t green(uint32_t color){
|
||||
return (color >> 8) & 0xFF;
|
||||
}
|
||||
uint8_t blue(uint32_t color){
|
||||
return color & 0xFF;
|
||||
}
|
||||
uint8_t white(uint32_t color){
|
||||
return (color >> 24) & 0xFF;
|
||||
}
|
||||
|
||||
class Strip {
|
||||
private:
|
||||
@@ -189,16 +162,14 @@ class Strip {
|
||||
Strip strip = Strip(6, 144, 100);
|
||||
|
||||
void setup(void) {
|
||||
//strip.off()
|
||||
while (!Serial); // required for Flora & Micro
|
||||
Serial.begin(115200);
|
||||
bluetooth_setup();
|
||||
strip.off();
|
||||
Serial.println(F("setup() finished"));
|
||||
Serial.println(F("Ready"));
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
if (irq_event_available) {
|
||||
ble.handleDfuIrq();
|
||||
}
|
||||
ble.update(BT_SCAN_MS);
|
||||
strip.update();
|
||||
}
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
#include <string.h>
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
|
||||
#include <SoftwareSerial.h>
|
||||
#endif
|
||||
|
||||
#include "Adafruit_BLE.h"
|
||||
#include "Adafruit_BluefruitLE_SPI.h"
|
||||
#include "Adafruit_BluefruitLE_UART.h"
|
||||
|
||||
#define PACKET_PATTERN_LEN (3)
|
||||
#define PACKET_COLOR_LEN (6)
|
||||
|
||||
// READ_BUFSIZE Size of the read buffer for incoming packets
|
||||
#define READ_BUFSIZE (20)
|
||||
|
||||
|
||||
/* Buffer to hold incoming characters */
|
||||
uint8_t packetbuffer[READ_BUFSIZE+1];
|
||||
|
||||
void printHex(const uint8_t * data, const uint32_t numBytes) {
|
||||
uint32_t szPos;
|
||||
for (szPos=0; szPos < numBytes; szPos++) {
|
||||
Serial.print(F("0x"));
|
||||
Serial.print(data[szPos], HEX);
|
||||
// Add a trailing space if appropriate
|
||||
if ((numBytes > 1) && (szPos != numBytes - 1)) {
|
||||
Serial.print(F(" "));
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout)
|
||||
{
|
||||
uint16_t origtimeout = timeout, replyidx = 0;
|
||||
|
||||
memset(packetbuffer, 0, READ_BUFSIZE);
|
||||
|
||||
while (timeout--) {
|
||||
if (replyidx >= 20) break;
|
||||
if ((packetbuffer[1] == 'P') && (replyidx == PACKET_PATTERN_LEN))
|
||||
break;
|
||||
if ((packetbuffer[1] == 'C') && (replyidx == PACKET_COLOR_LEN))
|
||||
break;
|
||||
|
||||
while (ble->available()) {
|
||||
char c = ble->read();
|
||||
if (c == '!') {
|
||||
replyidx = 0;
|
||||
}
|
||||
packetbuffer[replyidx] = c;
|
||||
//Serial.print(c);
|
||||
replyidx++;
|
||||
timeout = origtimeout;
|
||||
}
|
||||
|
||||
if (timeout == 0) break;
|
||||
delay(1);
|
||||
}
|
||||
|
||||
packetbuffer[replyidx] = 0; // null term
|
||||
|
||||
if (!replyidx) // no data or timeout
|
||||
return 0;
|
||||
if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning
|
||||
return 0;
|
||||
|
||||
// check checksum!
|
||||
uint8_t xsum = 0;
|
||||
uint8_t checksum = packetbuffer[replyidx-1];
|
||||
|
||||
for (uint8_t i=0; i<replyidx-1; i++) {
|
||||
xsum += packetbuffer[i];
|
||||
}
|
||||
xsum = ~xsum;
|
||||
|
||||
// Throw an error message if the checksum's don't match
|
||||
if (xsum != checksum){
|
||||
Serial.print("Checksum mismatch in packet : ");
|
||||
printHex(packetbuffer, replyidx+1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// checksum passed!
|
||||
return replyidx;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user