1
0

simpler UART

This commit is contained in:
2019-03-15 23:23:31 -04:00
parent fe7ac2ff42
commit f6f67e382e
3 changed files with 24 additions and 142 deletions

View File

@@ -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();
}