Takashi Matsuoka / Mbed 2 deprecated ChibibitFirmata

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MicrobitHw.h"
00003 #include "BLE.h"
00004 #include "UARTService.h"
00005 #include "Firmata.h"
00006 
00007 #define DLM "\r\n"
00008 
00009 static const char DEVICE_NAME[] = "chibi:bit";
00010 
00011 static Serial pc(USBTX, USBRX);
00012 
00013 static BLEDevice  ble;
00014 static UARTService* uart;
00015 
00016 static bool LedDisplay[3][9];
00017 
00018 void BleConnectionCallback(const Gap::ConnectionCallbackParams_t* params)
00019 {
00020     pc.printf("Connected."DLM);
00021 
00022     Gap::ConnectionParams_t gap_conn_params;
00023     gap_conn_params.minConnectionInterval        = 6;   // 7.5[msec.] / 1.25
00024     gap_conn_params.maxConnectionInterval        = 6;   // 7.5[msec.] / 1.25
00025     gap_conn_params.slaveLatency                 = 1;
00026     gap_conn_params.connectionSupervisionTimeout = 500; // 5000[msec.] / 10
00027 
00028     if (ble.updateConnectionParams(params->handle, &gap_conn_params) != BLE_ERROR_NONE) {
00029         pc.printf("Failed to update connection paramter."DLM);
00030     }
00031 }
00032 
00033 void BleDisconnectionCallback(const Gap::DisconnectionCallbackParams_t* params)
00034 {
00035     pc.printf("Disconnected."DLM);
00036     ble.startAdvertising();
00037 }
00038 
00039 void BleTimeoutCallback(const Gap::TimeoutSource_t source)
00040 {
00041     pc.printf("Timeout."DLM);
00042     ble.startAdvertising();
00043 }
00044 
00045 void BleOnDataWritten(const GattWriteCallbackParams* params)
00046 {
00047     if (uart == NULL || params->handle != uart->getTXCharacteristicHandle()) return;
00048 
00049     uint8_t payload[40];
00050     uint16_t len = params->len;
00051     ble.readCharacteristicValue(uart->getTXCharacteristicHandle(), payload, &len);
00052 
00053     // Display for rx data.
00054     pc.printf("RX(%2d): ", params->len);
00055     for (int i = 0; i < len; i++) {
00056         pc.printf("%02x ", payload[i]);
00057     }
00058     pc.printf(DLM);
00059 
00060     if (len == 3 && (payload[0] & 0xf0) == DIGITAL_MESSAGE)
00061     {
00062         int port = payload[0] & 0x0f;
00063         uint8_t value = (payload[1] & 0x7f) | (payload[2] & 0x01) << 7;
00064         pc.printf("DIGITAL_MESSAGE port=%d, value=%02x"DLM, port, value);
00065         switch (port)
00066         {
00067         case 0:
00068             LedDisplay[0][0] = value & 0x01 ? true : false;
00069             LedDisplay[1][3] = value & 0x02 ? true : false;
00070             LedDisplay[0][1] = value & 0x04 ? true : false;
00071             LedDisplay[1][4] = value & 0x08 ? true : false;
00072             LedDisplay[0][2] = value & 0x10 ? true : false;
00073             LedDisplay[2][3] = value & 0x20 ? true : false;
00074             LedDisplay[2][4] = value & 0x40 ? true : false;
00075             LedDisplay[2][5] = value & 0x80 ? true : false;
00076             break;
00077         case 1:
00078             LedDisplay[2][6] = value & 0x01 ? true : false;
00079             LedDisplay[2][7] = value & 0x02 ? true : false;
00080             LedDisplay[1][1] = value & 0x04 ? true : false;
00081             LedDisplay[0][8] = value & 0x08 ? true : false;
00082             LedDisplay[1][2] = value & 0x10 ? true : false;
00083             LedDisplay[2][8] = value & 0x20 ? true : false;
00084             LedDisplay[1][0] = value & 0x40 ? true : false;
00085             LedDisplay[0][7] = value & 0x80 ? true : false;
00086             break;
00087         case 2:
00088             LedDisplay[0][6] = value & 0x01 ? true : false;
00089             LedDisplay[0][5] = value & 0x02 ? true : false;
00090             LedDisplay[0][4] = value & 0x04 ? true : false;
00091             LedDisplay[0][3] = value & 0x08 ? true : false;
00092             LedDisplay[2][2] = value & 0x10 ? true : false;
00093             LedDisplay[1][6] = value & 0x20 ? true : false;
00094             LedDisplay[2][0] = value & 0x40 ? true : false;
00095             LedDisplay[1][5] = value & 0x80 ? true : false;
00096             break;
00097         case 3:
00098             LedDisplay[2][1] = value & 0x01 ? true : false;
00099             break;
00100         default:
00101             break;
00102         }
00103     }
00104     else if (len == 3 && payload[0] == START_SYSEX && payload[1] == CAPABILITY_QUERY && payload[2] == END_SYSEX) {
00105         pc.printf("CAPABILITY_QUERY"DLM);
00106         const uint8_t buf[] = { START_SYSEX, CAPABILITY_RESPONSE, 1, 1, 0x7f, 1, 1, 0x7f, END_SYSEX, };
00107         ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), buf, sizeof (buf));
00108     }
00109 }
00110 
00111 void BleInitialize()
00112 {
00113     ble.init();
00114     ble.initializeSecurity();
00115     ble.setDeviceName((const uint8_t*)DEVICE_NAME);
00116 
00117     ble.onConnection(BleConnectionCallback);
00118     ble.onDisconnection(BleDisconnectionCallback);
00119     ble.onTimeout(BleTimeoutCallback);
00120     ble.onDataWritten(BleOnDataWritten);
00121 
00122     uart = new UARTService(ble);
00123 
00124     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00125     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t*)DEVICE_NAME, sizeof (DEVICE_NAME) - 1);
00126     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t*)UARTServiceUUID_reversed, sizeof (UARTServiceUUID_reversed));
00127 
00128     ble.setAdvertisingInterval(160);
00129 }
00130 
00131 void flip()
00132 {
00133     static int row = 0;
00134     row++;
00135     if (row >= 3) row = 0;
00136 
00137     int value = (LedDisplay[row][0] ? 0x0001 : 0) |
00138                 (LedDisplay[row][1] ? 0x0002 : 0) |
00139                 (LedDisplay[row][2] ? 0x0004 : 0) |
00140                 (LedDisplay[row][3] ? 0x0008 : 0) |
00141                 (LedDisplay[row][4] ? 0x0010 : 0) |
00142                 (LedDisplay[row][5] ? 0x0020 : 0) |
00143                 (LedDisplay[row][6] ? 0x0040 : 0) |
00144                 (LedDisplay[row][7] ? 0x0080 : 0) |
00145                 (LedDisplay[row][8] ? 0x0100 : 0);
00146                 
00147     MicrobitHwLedMatrix(row, value);
00148 }
00149 
00150 int main()
00151 {
00152     MicrobitHwInitialize();
00153     
00154     pc.baud(115200);
00155     pc.printf("Start a chibi:bit firmata."DLM);
00156     
00157     Ticker ticker;
00158     ticker.attach_us(flip, 1000);
00159 
00160     BleInitialize();
00161     ble.startAdvertising();
00162     for(;;) {
00163         ble.waitForEvent();
00164     }
00165 }