Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API microbit_ble_mouse mbed nRF51822
Fork of microbit_mouse_BLE by
Revision 5:c1e87a1869cd, committed 2018-05-27
- Comitter:
- suntopbd
- Date:
- Sun May 27 20:29:53 2018 +0000
- Parent:
- 4:b58633511f96
- Commit message:
- connects but does not work !
Changed in this revision
| MMA8652.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r b58633511f96 -r c1e87a1869cd MMA8652.lib --- a/MMA8652.lib Sun May 27 14:27:20 2018 +0000 +++ b/MMA8652.lib Sun May 27 20:29:53 2018 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/JimCarver/code/MMA8652/#29c2dd97ca95 +https://os.mbed.com/users/suntopbd/code/microbit_ble_mouse/#62325c5fe8a8
diff -r b58633511f96 -r c1e87a1869cd main.cpp
--- a/main.cpp Sun May 27 14:27:20 2018 +0000
+++ b/main.cpp Sun May 27 20:29:53 2018 +0000
@@ -16,127 +16,229 @@
#include "mbed.h"
#include "ble/BLE.h"
+#include "HIDServiceBase.h"
//#include "KeyboardService.h"
#include "MouseService.h"
#include "examples_common.h"
#include "MMA8652.h" // accelerometer
-
+//Serial pc(USBTX, USBRX);
/**
- * This program implements a complete HID-over-Gatt Profile:
- * - HID is provided by MouseService
+ * This program implements a complete //////////////////////////HID-over-Gatt Profile:
+ * - //////HID is provided by MouseService
* - 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
-// turn off LED saves 1mA current
+//The micro:bit has a matrixed display, this is a simple way to use some LEDs on it
+// turn off LED saves 1 mA current
-//DigitalOut col9(P0_12, 0);
+ DigitalOut col9(P0_12, 0);
//DigitalOut waiting_led(P0_13);
//DigitalOut connected_led(P0_15);
-MMA8652 accelerometer( P0_19, P0_20); // i2c pin assign
+MMA8652 accelerometer(P0_20, P0_19); // i2c pin assign
+//I2C i2c(P0_19, P0_20);
+//const int accel_addr = 0x3A;
+
+ButtonState click;
+MouseButton LR;
+
+int Xpos = 0;int Ypos = 0;
+int OldXpos = 0; int OldYpos = 0;
+int dX = 0; int dY = 0;
+
float accelerometer_data[3];
float x,y,z;
+Ticker movement,timeout ;
+
InterruptIn button1(BUTTON_A);
InterruptIn button2(BUTTON_B);
BLE ble;
//KeyboardService *kbdServicePtr;
+//MouseService musServicePtr(ble);
MouseService *musServicePtr;
-static const char DEVICE_NAME[] = "micro:bit BLE Mouse";
-static const char SHORT_DEVICE_NAME[] = "BT Mouse";
+static const char DEVICE_NAME[] = "micro:bit_BLE_Mouse";
+static const char SHORT_DEVICE_NAME[] = "uBit-mUs";
static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params)
{
- HID_DEBUG("disconnected\r\n");
+ //////////HID_DEBUG("disconnected\r\n");
// connected_led = 0;
-
ble.gap().startAdvertising(); // restart advertising
}
static void onConnect(const Gap::ConnectionCallbackParams_t *params)
{
- HID_DEBUG("connected\r\n");
+ ////HID_DEBUG("connected\r\n");
// waiting_led = false;
}
-static void waiting() {
- if (!musServicePtr->isConnected())
+static void waiting()
+{
+ // if (!musServicePtr->isConnected())
{}// waiting_led = !waiting_led;
- else
+ // else
{}// connected_led = !connected_led;
}
-void send_string(const char * c) {
+
+ void stop_mouse_move(void)
+ {
+ // Set mouse state to immobile
+ // musServicePtr->.setButton(MOUSE_BUTTON_LEFT, MOUSE_UP);
+ if (!musServicePtr)
+ return;
+ if (!musServicePtr->isConnected())
+ {
+ //////HID_DEBUG("we haven't connected yet...\n");
+ } else {
+ musServicePtr->setSpeed(0, 0, 0);
+ }
+ }
+
+ void start_mouse_move(void)
+ {
+ // Move left with a left button down. If the focus is on a drawing
+ // software, for instance, this should draw a line.
+ // mouse.setButton(MOUSE_BUTTON_LEFT, MOUSE_DOWN);
+ musServicePtr->setSpeed(0, 0, 0);
+ // timeout.attach(stop_mouse_move, 0.2);
+ }
+
+
+void register_Lclick()
+{
+ // send click left
if (!musServicePtr)
return;
+ if (!musServicePtr->isConnected())
+ {
+ //////HID_DEBUG("we haven't connected yet...\n");
+ } else {
+
+ LR = MOUSE_BUTTON_LEFT;
+ click = BUTTON_DOWN;
+ musServicePtr->setButton(LR,click);
+ }
+}
- if (!musServicePtr->isConnected()) {
- HID_DEBUG("we haven't connected yet...");
+void register_Rclick()
+{
+ // send click rightLR = MOUSE_BUTTON_LEFT;
+ if (!musServicePtr)
+ return;
+ if (!musServicePtr->isConnected())
+ {
+ //////HID_DEBUG("we haven't connected yet...\n");
} else {
- // int len = strlen(c);
- // do somethung here to make mouse work
- // kbdServicePtr->printf(c);
- // HID_DEBUG("sending %d chars\r\n", len);
- }
+
+ LR = MOUSE_BUTTON_RIGHT;
+ click = BUTTON_DOWN;
+ musServicePtr->setButton(LR,click);
+ }
+}
+
+void register_Lclick_release()
+{
+ // send click left_release
+ if (!musServicePtr)
+ return;
+ if (!musServicePtr->isConnected())
+ {
+ //////HID_DEBUG("we haven't connected yet...\n");
+ } else {
+
+ LR = MOUSE_BUTTON_LEFT;
+ click = BUTTON_UP;
+ musServicePtr->setButton(LR,click);
+ }
}
-void send_stuff() {
- // send click left
- // send_string("n");
+void register_Rclick_release()
+{
+ // send click right_release
+ if (!musServicePtr)
+ return;
+ if (!musServicePtr->isConnected())
+ {
+ //////HID_DEBUG("we haven't connected yet...\n");
+ } else {
+ LR = MOUSE_BUTTON_RIGHT;
+ click = BUTTON_UP;
+ musServicePtr->setButton(LR,click);
+ }
+}
+
+void stop_mouse_movement()
+{
+ musServicePtr->setSpeed(0, 0, 0);
}
-void send_more_stuff() {
- // send click right
- // send_string("p");
+void register_mouse_movement()
+{
+ if (!musServicePtr)
+ return;
+ if (!musServicePtr->isConnected())
+ {
+ //////HID_DEBUG("we haven't connected yet...\n");
+ } else {
+
+ accelerometer.ReadXYZ(accelerometer_data);
+ x = accelerometer_data[0];
+ y = accelerometer_data[1];
+ z = accelerometer_data[2];
+ // do some math to adjust movement
+
+ Xpos = x*50.0;
+ Ypos = y*50.0; // float to int conv
+ dX = abs(Xpos - OldXpos); dY = abs(Ypos - OldYpos);
+ OldXpos = Xpos; OldYpos = Ypos;
+ musServicePtr->setSpeed(dX, dY, 0);
+ dX=0;dY=0;
+ timeout.attach(stop_mouse_movement, 0.001);
+ }
}
int main()
{
Ticker heartbeat;
-
- button1.rise(send_stuff);
- button2.rise(send_more_stuff);
+
+ button1.rise(register_Lclick);
+ button2.rise(register_Rclick);
- HID_DEBUG("initialising ticker\r\n");
+ button1.fall(register_Lclick_release);
+ button2.fall(register_Rclick_release);
+
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");
- MouseService musService(ble);
- musServicePtr = &musService;
- 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::MOUSE);
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();
+
+ wait(10);
+ // movement.attach(®ister_mouse_movement, 0.05);
while (true)
{
+ register_mouse_movement();
ble.waitForEvent();
/// make the following a separate periodic ticker
- accelerometer.ReadXYZ(accelerometer_data);
- x = accelerometer_data[0];
- y = accelerometer_data[1];
- z = accelerometer_data[2];
+ //accelerometer.ReadXYZ(accelerometer_data);
+ // x = accelerometer_data[0];
+ // y = accelerometer_data[1];
+ // z = accelerometer_data[2];
}
}
