Commit 84f4145c authored by Ajay Meena's avatar Ajay Meena

led matrix with ble code

parent 8dcf3d02
#include <Arduino.h>
#include <bluefruit.h>
#define LED_MATRIX_WIDTH 8
#define LED_MATRIX_HEIGHT 8
// Shift register array pins
int DATA = 3;
int DATA_OUTPUT_ENABLE = 4;
int LATCH = 5;
int CLOCK = 6;
int RESET = 7;
byte Screen[8][8] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}};
void sendBit(uint8_t aData)
{
digitalWrite(DATA, aData);
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
digitalWrite(DATA, LOW);
}
void sendDataToDisplay()
{
byte lineIndex;
byte rowIndex;
for(lineIndex = 0; lineIndex < 8; lineIndex++)
{
//Set Anode Line
for(rowIndex = 0; rowIndex < 8; rowIndex++)
{
if (lineIndex == rowIndex)
{
sendBit(HIGH);
}
else
{
sendBit(LOW);
}
}
//Set Green Row
for(rowIndex = 0; rowIndex < 8; rowIndex++)
{
if ((Screen[lineIndex][7-rowIndex] & 1) == 0) //fix
{
sendBit(HIGH);
}
else
{
sendBit(LOW);
}
}
//Set Red Row
for(rowIndex = 0; rowIndex < 8; rowIndex++)
{
if ((Screen[lineIndex][rowIndex] & 2) == 0)
{
sendBit(HIGH);
}
else
{
sendBit(LOW);
}
}
//Set Blue Row
for(rowIndex = 0; rowIndex < 8; rowIndex++)
{
if ((Screen[lineIndex][rowIndex] & 4) == 0)
{
sendBit(HIGH);
}
else
{
sendBit(LOW);
}
}
//Send data to output
digitalWrite(LATCH, HIGH);
digitalWrite(LATCH, LOW);
}
}
void Clear()
{
byte lineIndex;
byte rowIndex;
for(lineIndex = 0; lineIndex < 8; lineIndex++)
{
for(rowIndex = 0; rowIndex < 8; rowIndex++)
{
Screen[lineIndex][rowIndex] = 0;
}
}
}
// BLE Service
BLEDis bledis;
BLEUart bleuart;
void setup()
{
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
Serial.println("\r\nElectronut Labs LED8X8 Matrix");
Serial.println("----------------------------");
Serial.println();
Serial.println("Please connect to BLIP, URL -> ");
// Init Bluefruit
Bluefruit.begin();
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.setName("BLIP");
Bluefruit.Periph.setConnectCallback(connect_callback);
// Configure and Start Device Information Service
bledis.setManufacturer("Electronut Labs");
bledis.begin();
// Configure and start BLE UART service
bleuart.begin();
// Set up and start advertising
startAdv();
pinMode(DATA, OUTPUT);
pinMode(DATA_OUTPUT_ENABLE, OUTPUT);
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(RESET, OUTPUT);
digitalWrite(DATA_OUTPUT_ENABLE, LOW);
digitalWrite(RESET, HIGH);
digitalWrite(CLOCK, LOW);
digitalWrite(LATCH, LOW);
digitalWrite(DATA, LOW);
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
// Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(bleuart);
// Secondary Scan Response packet (optional)
// Since there is no room for 'Name' in Advertising packet
Bluefruit.ScanResponse.addName();
/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
*
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char central_name[32] = { 0 };
connection->getPeerName(central_name, sizeof(central_name));
Serial.println();
Serial.print("Connected to ");
Serial.println(central_name);
Serial.println("Please transfer led matrix pattern");
}
int char_received = 0;
bool flush_led_matrix = false;
void loop()
{
if ( Bluefruit.connected() && bleuart.notifyEnabled() )
{
if ( bleuart.available() )
{
char ch = (char) bleuart.read();
// start of led matrix data string
if(ch == '8')
{
Serial.println("start");
char_received = 0;
flush_led_matrix = false;
Clear();
}
// end of led matrix data string
else if ( ch == '\n' )
{
Serial.print(ch);
Serial.println("\r\nend");
for (int i = 0; i < LED_MATRIX_WIDTH; i++) {
for (int j = 0; j < LED_MATRIX_HEIGHT; j++) {
Serial.print(Screen[i][j]);
}
Serial.println();
}
Serial.println();
flush_led_matrix = true;
}
else
{
Screen[char_received/LED_MATRIX_WIDTH][char_received%LED_MATRIX_WIDTH] = ch - '0';
Serial.print(char_received);
Serial.print("-");
Serial.print(ch);
Serial.print(", ");
char_received++;
}
}
}
if(flush_led_matrix)
{
sendDataToDisplay();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment