// ================================== Bluetooth Definitions ========================================
#include <EEPROM.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
// #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
#define EEPROM_SIZE 500
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
String bleString;
String lastProtocolString = " ";
bool isNewProtocol = false;
// ========================================================================================
// =============================== Display Definitions =============================================
#define PANEL_RES_X 32 // Number of pixels wide of each INDIVIDUAL panel module.
#define PANEL_RES_Y 16 // Number of pixels tall of each INDIVIDUAL panel module.
#define NUM_ROWS 1 // Number of rows of chained INDIVIDUAL PANELS
#define NUM_COLS 3 // Number of INDIVIDUAL PANELS per ROW
#define PANEL_CHAIN NUM_ROWS *NUM_COLS // total number of panels chained one to another
// library includes
#include <Adafruit_GFX.h>
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
#include <ESP32-VirtualMatrixPanel-I2S-DMA.h>
#include "BLEInputHandler.h"
int vertical_offset = 1;
// placeholder for the matrix object
MatrixPanel_I2S_DMA *dma_display = nullptr;
// placeholder for the virtual display object
// Display number one from 1 to 68 pixels (0th to 71st)
VirtualMatrixPanel *virtualDisp1 = nullptr;
// Display number one from 1 to 96 pixels (0th to 96th)
VirtualMatrixPanel *virtualDisp2 = nullptr;
// String varialbles to hold the input protocol text
String protocolText;
String defaultFirstTimeText = "Prahari";
// BLEInputHandler
BLEInputHandler *bleInputHandler;
String scrollText;
uint16_t scrollTextColor;
uint16_t scrollTextBgColor;
char firstChar;
uint16_t firstCharTextColor;
uint16_t firstCharBgColor;
char secondChar;
uint16_t secondCharTextColor;
uint16_t secondCharBgColor;
// =================================================================================================
// ===================================== Utility Functions =========================================
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer *pServer) {
deviceConnected = false;
pServer->getAdvertising()->start();
}
};
class MyCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
String value = pCharacteristic->getValue().c_str();
if (value.length() > 0) {
Serial.println("*********");
Serial.print("New protocol text: ");
Serial.println(value);
Serial.println("*********");
// Set the new value to the lastProtocol value
lastProtocolString = value;
isNewProtocol = true;
}
}
};
void displayAllThreeDataOnScreen() {
// First display the two letters with the text color and their respective background color
// virtualDisp2->fillRect(virtualDisp1->width() + 1, 0, ((virtualDisp2->width() - virtualDisp1->width()) / 2) - 1, virtualDisp2->height(), bleInputHandler->getFirstSingleCharBgColor());
// virtualDisp2->fillRect(virtualDisp1->width() + ((virtualDisp2->width() - virtualDisp1->width()) / 2) - 1, 0, (virtualDisp2->width() - virtualDisp1->width()) / 2, virtualDisp2->height(), bleInputHandler->getSecondSingleCharBgColor());
virtualDisp2->fillRect(73, 0, 4, 16, bleInputHandler->getFirstSingleCharBgColor());
virtualDisp2->fillRect(85, 0, 4, 16, bleInputHandler->getSecondSingleCharBgColor());
// Display first single character
virtualDisp2->setCursor(73, vertical_offset);
virtualDisp2->setTextColor(bleInputHandler->getFirstSingleCharTextColor());
virtualDisp2->print(bleInputHandler->getFirstSingleLetter());
// Display second single character
virtualDisp2->setCursor(84, vertical_offset);
virtualDisp2->setTextColor(bleInputHandler->getSecondSingleCharTextColor());
virtualDisp2->print(bleInputHandler->getSecondSingleLetter());
// Display the scrolling text
virtualDisp1->setTextColor(bleInputHandler->getScrollTextColor());
scroll_text(*&virtualDisp1, vertical_offset, bleInputHandler->getScrollText(),0);
}
void _1_displaySingleScreenColor(String protocolText) {
// Get the color data from the protocol input string
int r = protocolText.substring(11, 14).toInt();
int g = protocolText.substring(14, 17).toInt();
int b = protocolText.substring(17, 20).toInt();
Serial.println("Got the following colors ");
Serial.printf("Red %d, Green %d, Blue %d ", r, g, b);
Serial.println();
// Store the Color input to somewhere
bleInputHandler->changeSingleScreenColor(r, g, b);
// Set the protocol command flag
EEPROM.write(0, 1); // This means that the last command got from the BLE was 1 - displaySingleColor
EEPROM.commit();
delay(100);
while (1) {
// Listen for active input stream from the bluetooth
if (deviceConnected) {
if (isNewProtocol) {
char breakFlag = lastProtocolString.charAt(0);
if (breakFlag == '~') {
Serial.print("Break flag 3 ::: ");
Serial.println(breakFlag);
virtualDisp2->fillScreen(0);
break; // Break the loop
}
}
}
// Fill full screen
// virtualDisp2->fillScreen(bleInputHandler->getSingleScreenColor());
// Fill middle panel only
virtualDisp2->fillRect(32, 0, 32, 16, bleInputHandler->getSingleScreenColor());
}
}
void _2_changeScrollTextDetails(String protocolText) {
// Get the data from the protocol input string
int bg_r = protocolText.substring(11, 14).toInt();
int bg_g = protocolText.substring(14, 17).toInt();
int bg_b = protocolText.substring(17, 20).toInt();
int txt_r = protocolText.substring(20, 23).toInt();
int txt_g = protocolText.substring(23, 26).toInt();
int txt_b = protocolText.substring(26, 29).toInt();
String scrollText = protocolText.substring(29);
bleInputHandler->changeScrollTextBGColor(bg_r, bg_g, bg_b);
bleInputHandler->changeScrollTextColor(txt_r, txt_g, txt_b);
bleInputHandler->changeScrollText(scrollText);
// Set the protocol command flag
EEPROM.write(0, 2);
EEPROM.commit();
}
void _3_changeFirstCharDetails(String protocolText) {
// Get the data from the protocol input string
int bg_r = protocolText.substring(11, 14).toInt();
int bg_g = protocolText.substring(14, 17).toInt();
int bg_b = protocolText.substring(17, 20).toInt();
int txt_r = protocolText.substring(20, 23).toInt();
int txt_g = protocolText.substring(23, 26).toInt();
int txt_b = protocolText.substring(26, 29).toInt();
char firstSingleChar = protocolText[29];
bleInputHandler->changeFirstSingleChar(firstSingleChar);
bleInputHandler->changeFirstCharBGColor(bg_r, bg_g, bg_b);
bleInputHandler->changeFirstCharTextColor(txt_r, txt_g, txt_b);
// Set the protocol command flag
EEPROM.write(0, 3);
EEPROM.commit();
}
void _4_changeSecondCharDetails(String protocolText) {
// Get the data from the protocol input string
int bg_r = protocolText.substring(11, 14).toInt();
int bg_g = protocolText.substring(14, 17).toInt();
int bg_b = protocolText.substring(17, 20).toInt();
int txt_r = protocolText.substring(20, 23).toInt();
int txt_g = protocolText.substring(23, 26).toInt();
int txt_b = protocolText.substring(26, 29).toInt();
char firstSingleChar = protocolText[29];
bleInputHandler->changeSecondSingleChar(firstSingleChar);
bleInputHandler->changeSecondCharBGColor(bg_r, bg_g, bg_b);
bleInputHandler->changeSecondCharTextColor(txt_r, txt_g, txt_b);
// Set the protocol command flag
EEPROM.write(0, 4);
EEPROM.commit();
}
void _5_addNewUsersPhoneNumber(String protocolText) {
Serial.print("Adding the phone number ");
Serial.println(protocolText.substring(1, 11));
bleInputHandler->setupNewUserWithPhone(protocolText.substring(1, 11));
// Set the protocol command flag
EEPROM.write(0, 8); // Set the flag to 8 - means the phone number is there but no data is avaialble
EEPROM.commit();
scroll_text(*&virtualDisp2, vertical_offset, defaultFirstTimeText,5);
}
void _6_changeUsersPhoneNumber(String protocolText) {
Serial.print("Updating the phone number to");
Serial.println(protocolText.substring(1, 21));
bleInputHandler->changePhoneNumber(protocolText.substring(1, 21));
// Set the protocol command flag
EEPROM.write(0, 6); // Set the flag to 6
EEPROM.commit();
}
void _7_changeFullScrollTextDetails(String protocolText) {
// Get the data from the protocol input string
int bg_r = protocolText.substring(11, 14).toInt();
int bg_g = protocolText.substring(14, 17).toInt();
int bg_b = protocolText.substring(17, 20).toInt();
int txt_r = protocolText.substring(20, 23).toInt();
int txt_g = protocolText.substring(23, 26).toInt();
int txt_b = protocolText.substring(26, 29).toInt();
String scrollText = protocolText.substring(29);
bleInputHandler->changeScrollTextBGColor(bg_r, bg_g, bg_b);
bleInputHandler->changeScrollTextColor(txt_r, txt_g, txt_b);
bleInputHandler->changeScrollText(scrollText);
// Set the protocol command flag
EEPROM.write(0, 7);
EEPROM.commit();
virtualDisp2->fillRect(0, 0, virtualDisp2->width(), virtualDisp2->height(), bleInputHandler->getScrollTextBGColor());
virtualDisp2->setTextColor(bleInputHandler->getScrollTextColor());
scroll_text(*&virtualDisp2, vertical_offset, bleInputHandler->getScrollText(),7);
}
void _8_changeFullFixTextDetails(String protocolText) {
// Get the data from the protocol input string
int bg_r = protocolText.substring(11, 14).toInt();
int bg_g = protocolText.substring(14, 17).toInt();
int bg_b = protocolText.substring(17, 20).toInt();
int txt_r = protocolText.substring(20, 23).toInt();
int txt_g = protocolText.substring(23, 26).toInt();
int txt_b = protocolText.substring(26, 29).toInt();
String fixText = protocolText.substring(29);
bleInputHandler->changeScrollTextBGColor(bg_r, bg_g, bg_b);
bleInputHandler->changeScrollTextColor(txt_r, txt_g, txt_b);
bleInputHandler->changeScrollText(fixText);
// Set the protocol command flag
EEPROM.write(0, 8);
EEPROM.commit();
//virtualDisp2->fillScreen(0);
virtualDisp2->fillRect(0, 0, virtualDisp2->width(), virtualDisp2->height(), bleInputHandler->getScrollTextBGColor());
virtualDisp2->setCursor(0, vertical_offset);
virtualDisp2->setTextColor(bleInputHandler->getScrollTextColor());
virtualDisp2->print( bleInputHandler->getScrollText());
}
void _9_changeDynamicDetails(String protocolText) {
// Get the data from the protocol input string
uint8_t colors[72];
Serial.println("XXX");
for (int i=0;i<72;i++){
colors[i] = protocolText.substring(11+(i*3), 11+(i*3)+3).toInt();
}
int txt_r = protocolText.substring(227, 230).toInt();
int txt_g = protocolText.substring(230, 233).toInt();
int txt_b = protocolText.substring(233, 236).toInt();
String scrollText = protocolText.substring(236);
//bleInputHandler->changeScrollTextBGColor(bg_r, bg_g, bg_b);
bleInputHandler->setDynamicBGColor(colors);
bleInputHandler->changeScrollTextColor(txt_r, txt_g, txt_b);
bleInputHandler->changeScrollText(scrollText);
// Set the protocol command flag
EEPROM.write(0, 9);
EEPROM.commit();
delay(500);
scroll_text(*&virtualDisp2, vertical_offset, bleInputHandler->getScrollText(),9);
}
void _0_changeScrollSpeed(String protocolText) {
Serial.print("CHANGING SPEED");
Serial.println(protocolText.substring(11, 14));
bleInputHandler->changeScrollSpeed(protocolText.substring(11, 14).toInt());
if(EEPROM.read(0) == 2 || EEPROM.read(0) == 7 || EEPROM.read(0) == 9)
scroll_text(*&virtualDisp2, vertical_offset, bleInputHandler->getScrollText(),EEPROM.read(0));
}
/// Scroll Text on the display function
void scroll_text(VirtualMatrixPanel *virtualDisplay, int16_t y, String inputText,int protocol) {
int w_text = virtualDisplay->width(); // screen width
int len = inputText.length() * 12; // 12 is the max width of character when font size is 2
uint8_t colors[72];
while (1) {
// Listen for active input stream from the bluetooth
if (deviceConnected) {
if (isNewProtocol) {
char breakFlag = lastProtocolString.charAt(0);
if (breakFlag == '~') {
Serial.print("Break flag 2 ::: ");
Serial.println(breakFlag);
virtualDisplay->fillScreen(0);
break; // Break the loop
}
}
}
// display inputText
virtualDisplay->flipDMABuffer();
if(protocol == 9) {
bleInputHandler->getDynamicBGColor(colors);
for (int i=0;i<72;i++)Serial.print(colors[i]);
//delay(500);
for(int i=0;i<sizeof(colors)/3;i++){
virtualDisp2->fillRect((i*4), 0, 4, 16, virtualDisp2->color565(colors[(i*3)],colors[(i*3)+1], colors[(i*3)+2]));
}
}else virtualDisplay->fillRect(0, 0, virtualDisplay->width(), virtualDisplay->height(), bleInputHandler->getScrollTextBGColor());
virtualDisplay->setCursor(w_text, y);
virtualDisplay->setTextColor(bleInputHandler->getScrollTextColor());
virtualDisplay->print(inputText);
w_text--;
if (w_text + len == 0) {
w_text = virtualDisplay->width();
}
delay(bleInputHandler->getScrollSpeed());
}
}
// =================================================================================================
// ================== Handle input protocol text and decoding data function ========================
bool processProtocolInputText(String protocolText) {
if (protocolText.length() > 0) {
Serial.print("Protocol Text ");
Serial.println(protocolText);
isNewProtocol = false;
char ptclNum = protocolText[0];
if (EEPROM.read(0) != 0) {
Serial.print("Last running protocol was ==> ");
Serial.println(EEPROM.read(0));
if (bleInputHandler->isValidUser(protocolText.substring(1, 11))) {
switch (ptclNum) {
case '1': // Display single color on full-screen
_1_displaySingleScreenColor(protocolText);
break;
case '2': // Change scroll text with background and text colors
_2_changeScrollTextDetails(protocolText);
displayAllThreeDataOnScreen();
break;
case '3': // Change first single letter with background and text colors
_3_changeFirstCharDetails(protocolText);
displayAllThreeDataOnScreen();
break;
case '4': // Change second single letter with background and text colors
_4_changeSecondCharDetails(protocolText);
displayAllThreeDataOnScreen();
break;
case '5': // Calling the 5th protocol while the number has been saved but no new protocol after it has been received
// _5_addNewUsersPhoneNumber(protocolText);
displayAllThreeDataOnScreen();
break;
case '6': // Change user's phone number
_6_changeUsersPhoneNumber(protocolText);
displayAllThreeDataOnScreen();
break;
case '7':
_7_changeFullScrollTextDetails(protocolText);
break;
case '8':
_8_changeFullFixTextDetails(protocolText);
break;
case '9':
_9_changeDynamicDetails(protocolText);
break;
case '0':
_0_changeScrollSpeed(protocolText);
break;
default:
return false;
}
return true;
}
} else {
// Add new user's phone for the first time (Never Called again after the phone number is stored once)
if (ptclNum == '5') {
Serial.println("Calling the phone number adding function");
_5_addNewUsersPhoneNumber(protocolText);
return true;
}
}
Serial.println("***** No protocol ran :(");
Serial.println("Displaying the old state on the screen");
displayAllThreeDataOnScreen();
} else {
// pServer->startAdvertising();
scroll_text(*&virtualDisp2, vertical_offset, defaultFirstTimeText,0);
}
return false;
}
// =================================================================================================
void setup() {
Serial.begin(115200);
// initialize EEPROM with predefined size
EEPROM.begin(EEPROM_SIZE);
// ============================= BLE Input Data Setup =====================================
bleInputHandler = new BLEInputHandler(virtualDisp2);
// ========================================================================================
// ================================ Bluetooth Setup =======================================
BLEDevice::init("Brahma-ESP32-Test");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks()); // Adding callbacks for the server connection
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE characteristic
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("BLE is all set! Now you can interact through it with your phone!");
// ========================================================================================
// ================================== Display Setup =======================================
HUB75_I2S_CFG mxconfig(
PANEL_RES_X, // module width
PANEL_RES_Y, // module height
PANEL_CHAIN // chain length
);
//mxconfig.driver = HUB75_I2S_CFG::FM6126A; // in case that we use panels based on FM6126A chip, we can set it here before creating MatrixPanel_I2S_DMA object
// OK, now we can create our matrix object
// mxconfig.double_buff = true;
dma_display = new MatrixPanel_I2S_DMA(mxconfig);
// let's adjust default brightness to about 75%
dma_display->setBrightness8(64); // range is 0-255, 0 - 0%, 255 - 100%
// Allocate memory and start DMA display
if (not dma_display->begin())
Serial.println("****** !KABOOM! I2S memory allocation failed ***********");
// Uncomment the three lines below to test the behaviour of a brand new firmware
// EEPROM.write(0, 15);
// EEPROM.commit();
// delay(500);
// Read the EEPROM for existing string stored in case if the display is powered off
// The first bit will be the flag(0, 1 or 2)
// 0 means -> the firmware is new and ready for user to be authenticated
// 8 means -> the user is authenticated but no data available to show
// [1-7] means -> the data is available (on reboot, last data would be taken)
// After set to [1-7] the flag would always be used for storing the flag for the last running protocol
// Note: In this program flag == 5 and flag == 7 are never used. (We can add more conditions using these flags in future if required)
int flag = EEPROM.read(0);
Serial.print("Exists Flag ==> ");
Serial.println(flag);
if (flag == 0) {
// This means that the EEPROM is empty
Serial.println("The firmware is brand new and needs to be setup!");
} else if (flag == 10) {
Serial.println("The user number is entered but no data available yet!");
} else if (flag != 0 && flag != 1 && flag != 2 && flag != 3 && flag != 4 && flag != 5 && flag != 6 && flag != 7 && flag != 8 && flag != 9 && flag != 10) {
// If the flag is some other value than the possible flag values
// that means the EEPROM is corrupted or the EEPROM is new and holds garbage values
// Then clear the whole EEPROM at once and set the whole EEPROM address to integer 0.
for (int i = 0; i < EEPROM_SIZE; i++) {
EEPROM.write(i, 0);
EEPROM.commit();
}
delay(500);
} else {
Serial.println("The user number is present and some past data is also available");
}
// create virtualDisp1lay object based on our newly created dma_display object
virtualDisp1 = new VirtualMatrixPanel((*dma_display), 1, 1, 74, PANEL_RES_Y);
virtualDisp2 = new VirtualMatrixPanel((*dma_display), NUM_ROWS, NUM_COLS, PANEL_RES_X, PANEL_RES_Y);
virtualDisp1->fillScreen(0);
virtualDisp1->setTextSize(2);
virtualDisp2->fillScreen(0);
virtualDisp2->setTextSize(2);
// ========================================================================================
}
void loop() {
// ================================== Bluetooth Loop =======================================
if (deviceConnected) {
bleString = lastProtocolString;
bleString.remove(0, 1); // removes the ~ for further processing
processProtocolInputText(bleString);
} else {
Serial.println("Please connect to Brahma-ESP32-Test bluetooth");
// pServer->getAdvertising()->start(); // restart advertising
int flag = EEPROM.read(0);
if (flag == 0 || flag == 10) {
// If there is no past data available as well, then show default text
scroll_text(*&virtualDisp2, vertical_offset, defaultFirstTimeText,0);
} else {
if (flag == 1) {
while (1) {
// Listen for active input stream from the bluetooth
if (deviceConnected) {
if (isNewProtocol) {
char breakFlag = lastProtocolString.charAt(0);
if (breakFlag == '~') {
Serial.print("Break flag 1 ::: ");
Serial.println(breakFlag);
virtualDisp2->fillScreen(0);
break; // Break the loop
}
}
}
virtualDisp2->fillScreen(bleInputHandler->getSingleScreenColor());
}
} else {
if(flag == 9) scroll_text(*&virtualDisp2, vertical_offset, bleInputHandler->getScrollText(),9);
else if(flag == 8){
virtualDisp2->fillRect(0, 0, virtualDisp2->width(), virtualDisp2->height(), bleInputHandler->getScrollTextBGColor());
virtualDisp2->setCursor(0, vertical_offset);
virtualDisp2->setTextColor(bleInputHandler->getScrollTextColor());
virtualDisp2->print( bleInputHandler->getScrollText());
}else if(flag == 7){
virtualDisp2->fillRect(0, 0, virtualDisp2->width(), virtualDisp2->height(), bleInputHandler->getScrollTextBGColor());
virtualDisp2->setTextColor(bleInputHandler->getScrollTextColor());
scroll_text(*&virtualDisp2, vertical_offset, bleInputHandler->getScrollText(),7);
}else displayAllThreeDataOnScreen();
}
}
}
}