Hello!
I'm very new to this Arduino world but I'm loving every minute of it. I purchase most of my products from Adafruit so I mostly use their libraries. I've taken their code and modified it to meet my needs but still having problem with one thing.
I'm using a Feather Bluefruit to connect to my cellphone via Bluetooth Uart which allows me to enter text on my cellphone and then display it on the matrix. Everything is working well except when you enter long string of text, the scrolling gets cut off. I made my own scrolling function which takes in the text, length and scroll speed. The problem is I cannot figure out a way of determining the length of the text which is being pulled in from the ble.buffer. So right now, I'm using a fixed length. This causes a long delay after a short text or with a long text, it gets cut off.
Any help/guidance would be appreciated!
/*********************************************************************
This is an example for our nRF51822 based Bluefruit LE modules
Pick one up today in the adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#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"
#include "BluefruitConfig.h"
#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#define PIN 6
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(16, 16, 6,
NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
uint32_t GREEN = matrix.Color(0,255,0);
uint32_t ORANGE = matrix.Color(255,125,0);
uint32_t PINK = matrix.Color(255,0,200);
uint32_t YELLOW = matrix.Color(255,255,0);
uint32_t WHITE = matrix.Color(255,255,255);
uint32_t BLUE = matrix.Color(0,0,255);
uint32_t TEAL = matrix.Color(0,255,233);
uint32_t RED = matrix.Color(255,0,0);
int len;
const uint16_t colors[] = {
matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255) };
#define FACTORYRESET_ENABLE 1
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
#define MODE_LED_BEHAVIOUR "MODE"
/*=========================================================================*/
// Create the bluefruit object, either software serial...uncomment these lines
/*
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
*/
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
// while (!Serial); // required for Flora & Micro
delay(500);
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit Command Mode Example"));
Serial.println(F("---------------------------------------"));
matrix.begin();
matrix.setTextWrap(false);
matrix.setBrightness(10);
matrix.setTextColor(colors[0]);
/* Initialise the module */
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ){
error(F("Couldn't factory reset"));
}
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
Serial.println(F("Then Enter characters to send to Bluefruit"));
Serial.println();
ble.verbose(false); // debug info is a little annoying after this point!
/* Wait for connection */
while (! ble.isConnected()) {
delay(500);
}
// LED Activity command is only supported from 0.6.6
if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
{
// Change Mode LED Activity
Serial.println(F("******************************"));
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
Serial.println(F("******************************"));
}
}
/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
unsigned long t = millis();
ribbon();
// Check for user input
char inputs[BUFSIZE+1];
if ( getUserInput(inputs, BUFSIZE) )
{
// Send characters to Bluefruit
Serial.print("[Send] ");
Serial.println(inputs);
ble.print("AT+BLEUARTTX=");
ble.println(inputs);
// check response stastus
if (! ble.waitForOK() ) {
Serial.println(F("Failed to send?"));
}
}
// Check for incoming characters from Bluefruit
ble.println("AT+BLEUARTRX");
ble.readline();
if (strcmp(ble.buffer, "OK") == 0) {
// no data
return;
}
// Some data was found, its in the buffer
Serial.print(F("[Recv] ")); Serial.println(ble.buffer);
//len=(ble.buffer.length());
Scroll(ble.buffer,100,50);
ble.waitForOK();
}
/**************************************************************************/
/*!
@brief Checks for user input (via the Serial Monitor)
*/
/**************************************************************************/
bool getUserInput(char buffer[], uint8_t maxSize)
{
// timeout in 100 milliseconds
TimeoutTimer timeout(100);
memset(buffer, 0, maxSize);
while( (Serial.peek() < 0) && !timeout.expired() ) {}
if ( timeout.expired() ) return false;
delay(2);
uint8_t count=0;
do
{
count += Serial.readBytes(buffer+count, maxSize);
delay(2);
} while( (count < maxSize) && !(Serial.peek() < 0) );
return true;
}
// Scroll Function
void Scroll(char* text, int lgth, int scrollspeed) {
matrix.setTextSize(1);
matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely
for (int16_t x =15; x>=-lgth; x--) {
matrix.clear();
matrix.setCursor(x,4);
matrix.print(text);
matrix.show();
delay(scrollspeed);
}
}