-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalanceBoardMouse.ino
129 lines (100 loc) · 3.02 KB
/
BalanceBoardMouse.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Name: BalanceBoardMouse.ino
Created: 8/18/2017 1:25:55 PM
Author: Josef
*/
#include <Arduino.h>
#include "MyButton.h"
// Bluetooth hid
#include <bluefruit.h>
BLEDis bledis;
BLEHidAdafruit blehid;
//Gyro sensor
#include "GyroSensor.h"
GyroSensor MyGyro;
int startposX, startposY;
//Add buttons.
MyButton bMouseMove(2); // pin A0
MyButton button2(3); // pin A1
MyButton button3(4); // pin A2
MyButton button4(5); // pin A3
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(115200);
MyGyro.initSensor();
bMouseMove.addInput();
button2.addInput();
button3.addInput();
button4.addInput();
//------------------------------------------------------------- Bluetooth Settings
Bluefruit.begin();
// HID Device can have a min connection interval of 9*1.25 = 11.25 ms
Bluefruit.setConnInterval(9, 16); // min = 9*1.25=11.25 ms, max = 16*1.25=20ms
Bluefruit.setName("Bluefruit52");
// Configure and Start Device Information Service
bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather 52");
bledis.begin();
// BLE HID
blehid.begin();
// Set up and start advertising
startAdv();
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
//Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_MOUSE);
// Include BLE HID service
Bluefruit.Advertising.addService(blehid);
// There is enough room for 'Name' in the advertising packet
Bluefruit.Advertising.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
}
//------------------------------------------------------------- Bluetooth Settings
// the loop function runs over and over again until power down or reset
void loop() {
//_state = _mouseMove.isPressed();
if ( bMouseMove.isPressed() == 1 ) {
MyGyro.GetSensorMotion(startposX, startposY);
Serial.println("MouseMove is pressed");
}
if (bMouseMove.isPressed() > 0) {
Serial.println("MouseMove is hold");
int x, y;
MyGyro.GetSensorMotion(x, y);
x = MyGyro.calcMotion(x);
y = MyGyro.calcMotion(y);
blehid.mouseMove(x, -y);
}
/*
_button2.isPressed();
_button3.isPressed();
_button4.isPressed();
*/
/*
int x, y, z;
MyGyro.GetSensorMotion(x, y, z);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.println();
*/
delay(10);
}