Commit e771446a authored by Tavish's avatar Tavish

Merge branch 'patch-1' into 'master'

Patch 1

See merge request !1
parents 8dcf3d02 211b82d2
#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();
}
}
<html ng-app="blipApp">
<head>
<title>Led Matrix</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js"></script>
<script src="./web-bluetooth-demo.js"></script>
<style>
.circle {
width: 35px;
height: 35px;
margin: 4px;
background: gray;
border-radius: 50%;
/* border-right: 1px solid white; */
/* float: left; */
}
.box {
background: black;
}
.color {
width: 40px;
height: 40px;
background: black;
margin: 3px;
border: 1px solid gray;
}
.lift {
box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
;
}
.green {
background: green;
}
.red {
background: red;
}
.yellow {
background: yellow;
}
.blue {
background: blue;
}
.cyan {
background: cyan;
}
.magenta {
background: magenta;
}
.white {
background: white;
}
.border {
border-right: 1px solid gray;
border-bottom: 1px solid gray;
}
</style>
</head>
<body ng-controller="ledMatrixController" ng-mouseup="mouseUp()">
<button id="connect">Connect</button>
<button id="send">Send</button>
<button id="clear">Clear</button>
<div style="margin: 0 auto; text-align: -webkit-center;">
<div style="margin: 0 auto; text-align: -webkit-center;">
<table>
<tbody>
<tr class="box" style="display: flex;" ng-repeat="items in arr" ng-init="parentIndex = $index">
<td class="border" ng-repeat="item in items" ng-init="childIndex = $index">
<!-- {{$index}} -->
<div class="circle" ng-class="item.color" ng-mousedown="mouseDown(parentIndex,childIndex)"
ng-mouseup="mouseUp()" ng-mouseover="mouseOver(parentIndex,childIndex)" ng-click="fillColor(parentIndex,childIndex)">
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div style="margin: 20 auto; text-align: -webkit-center;">
<table>
<tbody>
<tr style="display: flex;">
<td class="color off" ng-class="selectedColor == 'off'? 'lift' : ''" ng-click="selectColor('off')"></td>
<td class="color green" ng-class="selectedColor == 'green'? 'lift' : ''" ng-click="selectColor('green')"></td>
<td class="color red" ng-class="selectedColor == 'red'? 'lift' : ''" ng-click="selectColor('red')"></td>
<td class="color yellow" ng-class="selectedColor == 'yellow'? 'lift' : ''" ng-click="selectColor('yellow')"></td>
<td class="color blue" ng-class="selectedColor == 'blue'? 'lift' : ''" ng-click="selectColor('blue')"></td>
<td class="color cyan" ng-class="selectedColor == 'cyan'? 'lift' : ''" ng-click="selectColor('cyan')"></td>
<td class="color magenta" ng-class="selectedColor == 'magenta'? 'lift' : ''" ng-click="selectColor('magenta')"></td>
<td class="color white" ng-class="selectedColor == 'white'? 'lift' : ''" ng-click="selectColor('white')"></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
\ No newline at end of file
angular.module('blipApp', [])
.controller('ledMatrixController', function ($scope) {
var RXcharacteristic;
var connect = document.getElementById("connect");
var send = document.getElementById("send");
var clear = document.getElementById("clear");
connect.addEventListener('click', function (event) {
navigator.bluetooth.requestDevice({
acceptAllDevices: true,
optionalServices: ['6e400001-b5a3-f393-e0a9-e50e24dcca9e']
})
.then(device => {
// Human-readable name of the device.
console.log('Connecting to GATT Server...' + device.name);
// Attempts to connect to remote GATT Server.
return device.gatt.connect();
})
.then(server => {
// Note that we could also get all services that match a specific UUID by
// passing it to getPrimaryServices().
console.log('Getting Services...');
return server.getPrimaryService('6e400001-b5a3-f393-e0a9-e50e24dcca9e');
})
.then(service => {
service.getCharacteristics().then(characteristics => {
console.log(characteristics);
console.log('> Service: ' + service.uuid);
console.log(characteristics[0].uuid)
var TXcharacteristic = characteristics[0];
RXcharacteristic = characteristics[1];
TXcharacteristic.startNotifications().then(_ => {
console.log('> Notifications started');
console.log(characteristics[1].uuid)
});
})
})
.catch(error => {
console.log('Argh! ' + error);
});
});
send.addEventListener('click', function (event) {
var LedMatrix = Create2DArray(8, 8);
console.log(LedMatrix);
if (RXcharacteristic) {
RXcharacteristic.writeValue(str2ab('8' + ArrayToString(LedMatrix) + '\n'));
} else {
alert("Device not connected")
}
});
clear.addEventListener('click', function (event) {
$scope.arr = angular.copy($scope.allClear);
$scope.$apply();
});
function str2ab(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function ArrayToString(arr) {
var str = "";
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
str += arr[i][j].toString();
}
}
return str;
}
var map = {
off: 0,
green: 1,
red: 2,
yellow: 3,
blue: 4,
cyan: 5,
magenta: 6,
white: 7,
}
function Create2DArray(columns, rows) {
var arr = [];
for (var i = 0; i < rows; i++) {
if (!arr[i]) {
arr[i] = [
map[$scope.arr[i][7].color],
map[$scope.arr[i][6].color],
map[$scope.arr[i][5].color],
map[$scope.arr[i][4].color],
map[$scope.arr[i][3].color],
map[$scope.arr[i][2].color],
map[$scope.arr[i][1].color],
map[$scope.arr[i][0].color],
];
}
}
return arr;
}
$scope.widthArr = [1, 2, 3, 4, 5, 6, 7, 8];
$scope.heightArr = [1, 2, 3, 4, 5, 6, 7, 8];
$scope.arr = [];
for (var i = 0; i < 8; i++) {
if (!$scope.arr[i]) {
$scope.arr[i] = [];
}
for (var j = 0; j < 8; j++) {
if (!$scope.arr[i][j]) {
$scope.arr[i][j] = {
color: "off"
};
}
}
}
$scope.allClear = angular.copy($scope.arr);
$scope.fillColor = function (i, j) {
// $scope.selectedColor = color;
$scope.arr[i][j].color = $scope.selectedColor;
}
$scope.selectColor = function (color) {
$scope.selectedColor = color;
}
$scope.mouseDown = function (i, j) {
$scope.fillColor(i, j)
$scope.mouseOver = function (parentIndex, childIndex) {
$scope.fillColor(parentIndex, childIndex)
}
}
$scope.mouseUp = function () {
$scope.mouseOver = function (parentIndex, childIndex) {
}
}
});
\ No newline at end of file
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