Skip to content

Instantly share code, notes, and snippets.

@goran-mahovlic
Created September 1, 2016 07:05
Show Gist options
  • Save goran-mahovlic/aa4fc62f45de2a1e434e4b7289d4a045 to your computer and use it in GitHub Desktop.
Save goran-mahovlic/aa4fc62f45de2a1e434e4b7289d4a045 to your computer and use it in GitHub Desktop.
/* BLE bracelet hack
* http://forum.espruino.com/conversations/280747/
*
*/
//P0_18 TX -- I have borken pin so Im using RX for TX
//P0_17 RX
//accelerometer
// http://kionixfs.kionix.com/cn/datasheet/KX022-1020%20Specifications%20Rev4.0%20cn.pdf
//P0_16 SCL -- default HIGH probably pullup
//P0_15 SDO/ADR -- default HIGH probably pullup
//P0_14 SDI/SDA -- default HIGH probably pullup
//P0_13 Trigger -- default LOW
//P0_12 INT1 -- default LOW
//P0_11 INT2 -- default HIGH probably pullup
//P0_10 nCS -- default HIGH probably pullup -- from datasheet is this pin is high I2C is enabled so this is ok
// The Slave Address associated with the KX022 is 0011111X,
// where the programmable bit, X, is determined by the assignment of ADDR (pin 1) to GND or IO_Vdd.
// So in this case we have adress 0011111
// 1Fh 3Fh 00111111
//P0_7 Vibro motor
//Oled
//http://www.buydisplay.com/download/manual/ER-OLED0.49-1_Series_Datasheet.pdf
//P0_0 DC
//P0_1 RST
//P0_1 CS
//P0_29 MOSI
//P0_30 SCK
//Button P0_4
#include "mbed.h"
#include "ble/BLE.h"
#include "SFE_MicroOLED.h"
#include "ble/services/UARTService.h"
#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
* it will have an impact on code-size and power consumption. */
#if NEED_CONSOLE_OUTPUT
#define DEBUG(...) { printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */
#include "wire.h"
#define SCL 16
#define SDA 14
BLEDevice ble;
DigitalOut led1(P0_7);
DigitalIn button1(P0_4);
UARTService *uartServicePtr;
Serial mySerial(P0_17,P0_18);
SPI my_spi(P0_29,NC,P0_30);
MicroOLED my_oled(my_spi, P0_1,P0_0,P0_2);
//KX022 acc(I2C_SDA, I2C_SCL);
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
DEBUG("Disconnected!\n\r");
DEBUG("Restarting the advertising process\n\r");
ble.startAdvertising();
}
void onDataWritten(const GattWriteCallbackParams *params)
{
if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
uint16_t bytesRead = params->len;
DEBUG("received %u bytes\n\r", bytesRead);
ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
}
}
void periodicCallback(void)
{
led1 = !led1;
// mySerial.printf("x=%f, y=%f, z=%f\n", acc.getAccX(), acc.getAccY(), acc.getAccZ());
// my_oled.init(0, 400000);
my_oled.clear(ALL);
my_oled.puts("Hello all!");
// my_oled.circle(16, 31, 16);
// my_oled.line(0, 9, 63, 47);
// my_oled.rectFill(33, 32, 8, 8);
my_oled.display();
}
int main(void)
{
button1.mode(PullUp);
mySerial.printf("Started");
mySerial.printf("Started");
led1 = 1;
Ticker ticker;
ticker.attach(periodicCallback, 1);
my_oled.init(0, 8000000);
DEBUG("Initialising the nRF51822\n\r");
ble.init();
ble.onDisconnection(disconnectionCallback);
ble.onDataWritten(onDataWritten);
/* setup advertising */
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
(const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
ble.startAdvertising();
UARTService uartService(ble);
uartServicePtr = &uartService;
while (true) {
ble.waitForEvent();
// mySerial.printf("x=%f\n", acc.getAccX());
// my_oled.init(0, 8000000);
// my_oled.clear(ALL);
// my_oled.puts("Hello all!");
// my_oled.circle(16, 31, 16);
// my_oled.line(0, 9, 63, 47);
// my_oled.rectFill(33, 32, 8, 8);
// my_oled.display();
led1 = !led1;
//wait(0.3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment