back up of work during May 2019
Dependencies: microbit
Revision 6:f372773ad32f, committed 2019-06-05
- Comitter:
- tht216
- Date:
- Wed Jun 05 15:21:14 2019 +0000
- Branch:
- class_implmentation
- Parent:
- 5:fc79a1d13c9c
- Commit message:
- TODO:; 1. multi keypresses; 2. integration
Changed in this revision
diff -r fc79a1d13c9c -r f372773ad32f bitboard.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bitboard.cpp Wed Jun 05 15:21:14 2019 +0000 @@ -0,0 +1,211 @@ +#include "bitboard.h" + +bitboard::bitboard(/**MicroBit &_uBit,**/ BLE &_ble, KeyboardService* _kbdServicePtr, const char* _DEVICE_NAME, const char* _SHORT_DEVICE_NAME): /**uBit(_uBit),**/ ble(_ble), kbdServicePtr(_kbdServicePtr), DEVICE_NAME(_DEVICE_NAME), SHORT_DEVICE_NAME(_SHORT_DEVICE_NAME){ + Buffer = 0; //might change data type to allow for multi keypress + } + +bitboard::~bitboard(){ + delete DEVICE_NAME; + delete SHORT_DEVICE_NAME; + } + +void bitboard::init(){ +// uBit.init(); +// uBit.serial.send("uBit init...\r\n"); +// uBit.serial.send("BLE init...\r\n"); + ble.init(); + + ble.gap().onDisconnection(this, &bitboard::onDisconnect); + ble.gap().onConnection(this, &bitboard::onConnect); +// uBit.serial.send("Security init...\r\n"); + initializeSecurity(ble); + + KeyboardService kbdService(ble); + kbdServicePtr = &kbdService; +// uBit.serial.send("HOGP init...\r\n"); + initializeHOGP(ble); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME)); + ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME); +// uBit.serial.send("advertising...\r\n"); + ble.gap().startAdvertising(); + } + +void bitboard::onDisconnect(const Gap::DisconnectionCallbackParams_t *params){ + ble.gap().startAdvertising(); // restart advertising +// uBit.serial.send("Disconnected, advertising...\r\n"); + } + +void bitboard::onConnect(const Gap::ConnectionCallbackParams_t *params){ +// uBit.serial.send("Connected...\r\n"); + } + +void bitboard::onButtonA(MicroBitEvent e){ +// uBit.display.print("A"); +// uBit.serial.send("Button A pressed"); +// uBit.serial.send(e.value); + } + +void bitboard::send_keypress(){ + inputReportData[0] = 0; + inputReportData[2] = Buffer; + kbdServicePtr->send(inputReportData); + kbdServicePtr->send(emptyInputReportData); + wait(0.1); + } +uint8_t bitboard::get_keycode(char c){ + switch (c){ + case 'A': + case 'a': + return 0x04; + case 'B': + case 'b': + return 0x05; + case 'C': + case 'c': + return 0x06; + case 'D': + case 'd': + return 0x07; + case 'E': + case 'e': + return 0x08; + case 'F': + case 'f': + return 0x09; + case 'G': + case 'g': + return 0x0a; + case 'H': + case 'h': + return 0x0b; + case 'I': + case 'i': + return 0x0c; + case 'J': + case 'j': + return 0x0d; + case 'K': + case 'k': + return 0x0e; + case 'L': + case 'l': + return 0x0f; + case 'M': + case 'm': + return 0x10; + case 'N': + case 'n': + return 0x11; + case 'O': + case 'o': + return 0x12; + case 'P': + case 'p': + return 0x13; + case 'Q': + case 'q': + return 0x14; + case 'R': + case 'r': + return 0x15; + case 'S': + case 's': + return 0x16; + case 'T': + case 't': + return 0x17; + case 'U': + case 'u': + return 0x18; + case 'V': + case 'v': + return 0x19; + case 'W': + case 'w': + return 0x1a; + case 'X': + case 'x': + return 0x1b; + case 'Y': + case 'y': + return 0x1c; + case 'Z': + case 'z': + return 0x1d; + case '!': + case '1': + return 0x1e; + case '@': + case '2': + return 0x1f; + case '#': + case '3': + return 0x20; + case '$': + case '4': + return 0x21; + case '%': + case '5': + return 0x22; + case '^': + case '6': + return 0x23; + case '&': + case '7': + return 0x24; + case '*': + case '8': + return 0x25; + case '(': + case '9': + return 0x26; + case ')': + case '0': + return 0x27; + case '\n': // LF + return 0x28; + case '\b': // BS + return 0x2a; + case '\t': // TAB + return 0x2b; + case ' ': + return 0x2c; + case '_': + case '-': + return 0x2d; + case '+': + case '=': + return 0x2e; + case '{': + case '[': + return 0x2f; + case '}': + case ']': + return 0x30; + case '|': + case '\\': + return 0x31; + case ':': + case ';': + return 0x33; + case '"': + case '\'': + return 0x34; + case '~': + case '`': + return 0x35; + case '<': + case ',': + return 0x36; + case '>': + case '.': + return 0x37; + case '?': + case '/': + return 0x38; + default: + return 0; + } +}
diff -r fc79a1d13c9c -r f372773ad32f bitboard.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bitboard.h Wed Jun 05 15:21:14 2019 +0000 @@ -0,0 +1,38 @@ +#ifndef BITBOARD_H +#define BITBOARD_H + +#include "MicroBit.h" +#include "mbed.h" +#include "ble/BLE.h" +#include "KeyboardService.h" +#include "MicroBitPin.h" +#include "examples_common.h" +#include "MicroBit.h" +#include "keymap.h" + +class bitboard { +protected: + KeyboardService* kbdServicePtr; + const char* DEVICE_NAME; + const char* SHORT_DEVICE_NAME; + uint8_t Buffer; +public: +// MicroBit &uBit; + BLE &ble; + bitboard(/**MicroBit &_uBit,**/ BLE &_ble, KeyboardService* _kbdServicePtr, const char* _DEVICE_NAME, const char* _SHORT_DEVICE_NAME); + ~bitboard(); + + void init(); +// void start_service(); +// void start_broadcast(); + void onDisconnect(const Gap::DisconnectionCallbackParams_t *params); + void onConnect(const Gap::ConnectionCallbackParams_t *params); + void onButtonA(MicroBitEvent e); + void send_keypress(); + uint8_t get_keycode(char c); + +}; + + + +#endif
diff -r fc79a1d13c9c -r f372773ad32f main.cpp --- a/main.cpp Tue Jun 04 19:08:25 2019 +0000 +++ b/main.cpp Wed Jun 05 15:21:14 2019 +0000 @@ -1,246 +1,43 @@ -/* mbed Microcontroller Library - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "mbed.h" -#include "ble/BLE.h" -#include "KeyboardService.h" -#include "MicroBitPin.h" -#include "examples_common.h" -#include "MicroBit.h" -/** - * This program implements a complete HID-over-Gatt Profile: - * - HID is provided by KeyboardService - * - Battery Service - * - Device Information Service - * - * Complete strings can be sent over BLE using printf. Please note, however, than a 12char string - * will take about 500ms to transmit, principally because of the limited notification rate in BLE. - * KeyboardService uses a circular buffer to store the strings to send, and calls to putc will fail - * once this buffer is full. This will result in partial strings being sent to the client. - */ - - //The micro:bit has a matrixed display, this is a simple way to use some LEDs on it -DigitalOut col9(P0_12, 0); - -DigitalOut waiting_led(P0_13); -DigitalOut connected_led(P0_15); - +#include "bitboard.h" + InterruptIn button1(BUTTON_A); InterruptIn button2(BUTTON_B); - -//InterruptIn key1(MICROBIT_PIN_P0); -// Try to use external signal, rather than button a&b as keys - + + +//MicroBit uBit; +//MicroBit* uBitPtr = &uBit; BLE ble; -KeyboardService *kbdServicePtr; - -static const char DEVICE_NAME[] = "micro:bit xx316"; -static const char SHORT_DEVICE_NAME[] = "kbd1"; - -static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) -{ - HID_DEBUG("disconnected\r\n"); - connected_led = 0; - - ble.gap().startAdvertising(); // restart advertising -} - -static void onConnect(const Gap::ConnectionCallbackParams_t *params) +//BLE* blePtr = &ble; +KeyboardService* kbdServicePtr; + +//void onButtonA(MicroBitEvent e) +//{ +// uBit.display.print("A"); +// uBit.serial.send("Button A pressed"); +// uBit.serial.send(e.value); +//} +const char* DEVICE_NAME = "THOMAS MICROBIT"; +const char* SHORT_DEVICE_NAME = "UBIT"; + +int main() { - HID_DEBUG("connected\r\n"); - waiting_led = false; -} - -static void waiting() { - if (!kbdServicePtr->isConnected()) - waiting_led = !waiting_led; - else - connected_led = !connected_led; -} - -void send_string(const char * c) { - if (!kbdServicePtr) - return; - - if (!kbdServicePtr->isConnected()) { - HID_DEBUG("we haven't connected yet..."); - } else { - int len = strlen(c); - kbdServicePtr->printf(c); - HID_DEBUG("sending %d chars\r\n", len); + + bitboard btboard(/**uBit,**/ ble,kbdServicePtr, DEVICE_NAME, SHORT_DEVICE_NAME); + + bitboard* btboardPtr; + btboardPtr = &btboard; + btboard.init(); + + button1.rise(btboardPtr, &bitboard::send_keypress); +// button2.rise(btboardPtr, &bitboard::switchkey); + //btboard.uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, btboard->onButtonA()); +// btboard.uBit.messageBus.listen(MICROBIT_ID_IO_P0, MICROBIT_EVT_ANY, onButtonA); + // We don't want to drop out of main! + while(1){ + ble.waitForEvent(); + wait(1); +// btboard.send(); +// btboard.uBit.sleep(500); +// btboard.uBit.serial.send("waiting for ble event...\r\n"); } -} - -void send_stuff() { - send_string("n"); - wait(0.1); -} - -void send_more_stuff() { - send_string("p"); - wait(0.1); -} - - -void send_one(){ - inputReportData[0] = 0; - inputReportData[2] = 0x1e; - kbdServicePtr->send(inputReportData); //key down event? - kbdServicePtr->send(emptyInputReportData); // key up event? - wait(0.1); -} - -void send_zero(){// testing input from pins - inputReportData[0] = 0; - inputReportData[2] = 0x27; - kbdServicePtr->send(inputReportData); //key down event? - kbdServicePtr->send(emptyInputReportData); // key up event? - wait(0.1); -} - -void send_up(){// testing input from pins - inputReportData[0] = 0; - inputReportData[2] = 0x52; - kbdServicePtr->send(inputReportData); //key down event? - kbdServicePtr->send(emptyInputReportData); // key up event? - wait(0.1); -} - -void send_down(){// testing input from pins - // kbdServicePtr->putc(DownArrow); - inputReportData[0] = 0; - inputReportData[2] = 0x51; - kbdServicePtr->send(inputReportData); - kbdServicePtr->send(emptyInputReportData); - wait(0.1); -} - -void send_left(){// testing input from pins - inputReportData[0] = 0; - inputReportData[2] = 0x50; - kbdServicePtr->send(inputReportData); - kbdServicePtr->send(emptyInputReportData); - wait(0.1); -} - -void send_right(){// testing input from pins - inputReportData[0] = 0; - inputReportData[2] = 0x4f; - kbdServicePtr->send(inputReportData); - kbdServicePtr->send(emptyInputReportData); - wait(0.1); -} - -void send_space(){// testing input from pins - inputReportData[0] = 0; - inputReportData[2] = 0x2c; - kbdServicePtr->send(inputReportData); - kbdServicePtr->send(emptyInputReportData); - wait(0.1); -} - -void send_release(){ - kbdServicePtr->send(emptyInputReportData); - wait(0.1); -} - - -//#define MICROBIT_PIN_P13 P0_23 //SCK -//InterruptIn key2(MICROBIT_PIN_P13); -DigitalIn touch1(MICROBIT_PIN_P13); -//#define MICROBIT_PIN_P0 P0_3 -AnalogIn x_axis(MICROBIT_PIN_P0); -//#define MICROBIT_PIN_P1 P0_2 -AnalogIn y_axis(MICROBIT_PIN_P1); - -//Thread joystick_control_thread; // "Identifier "Thread" is undefined"??? - -void JoystickControl(){ - float x,y; -// -// if (touch1 == 1) send_one(); -// if (touch1 == 0) send_zero(); - - x = x_axis.read(); - if (x < 0.05f) send_left(); - if (x > 0.95f) send_right(); - - y = y_axis.read(); - if (y < 0.05f) send_down(); - if (y > 0.95f) send_up(); - - wait(0.05); -} - -void touch_control(){ - if (touch1 == 1) send_space(); - wait(0.05); -} - -//void BLE_fiber(){ -// while (true) { -// ble.waitForEvent(); -// } -//} - -int main(){ - //create_fiber(JoystickControl); -// create_fiber(BLE_fiber); - Ticker heartbeat; - - button1.rise(send_one); - button2.rise(send_zero); - - HID_DEBUG("initialising ticker\r\n"); - - heartbeat.attach(waiting, 1); - - HID_DEBUG("initialising ble\r\n"); - ble.init(); - - ble.gap().onDisconnection(onDisconnect); - ble.gap().onConnection(onConnect); - - initializeSecurity(ble); - - HID_DEBUG("adding hid service\r\n"); - KeyboardService kbdService(ble); - kbdServicePtr = &kbdService; - - HID_DEBUG("adding device info and battery service\r\n"); - initializeHOGP(ble); - - HID_DEBUG("setting up gap\r\n"); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, - (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME)); - ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME); - - HID_DEBUG("advertising\r\n"); - ble.gap().startAdvertising(); - - float x,y; - - while (true) { - ble.waitForEvent(); - - JoystickControl(); - touch_control(); - } -// release_fiber(); } \ No newline at end of file
diff -r fc79a1d13c9c -r f372773ad32f test.txt --- a/test.txt Tue Jun 04 19:08:25 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -110101011111111 -1110011111001 -10101101010101010100101110 -101001001010000101010010100 -1010000101000110011001100011100011100011100 -10 -11 -11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 -111111111111111111111111111111111111111111111111111 -111111111111111111111111111111111111 -0 -1010101010100010000111000 \ No newline at end of file