1+ // Copyright (c) Sandeep Mistry. All rights reserved.
2+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
14// Import libraries (BLEPeripheral depends on SPI)
25#include < SPI.h>
36#include < BLEHIDPeripheral.h>
@@ -26,24 +29,24 @@ void setup() {
2629#if defined (__AVR_ATmega32U4__)
2730 while (!Serial);
2831#endif
29-
32+
3033 pinMode (JOYSTICK_BUTTON_PIN, INPUT_PULLUP);
3134 buttonState = digitalRead (JOYSTICK_BUTTON_PIN);
32-
35+
3336 if (buttonState == LOW) {
3437 Serial.println (F (" BLE HID Peripheral - clearing bond data" ));
35-
38+
3639 // clear bond store data
3740 bleHIDPeripheral.clearBondStoreData ();
3841 }
39-
42+
4043 bleHIDPeripheral.setLocalName (" HID Mouse" );
4144 bleHIDPeripheral.addHID (bleMouse);
4245
4346 bleHIDPeripheral.begin ();
4447
4548 Serial.println (F (" BLE HID Mouse" ));
46-
49+
4750 joystickXCenter = readJoystickAxis (JOYSTICK_X_AXIS_PIN);
4851 joystickYCenter = readJoystickAxis (JOYSTICK_Y_AXIS_PIN);
4952}
@@ -60,7 +63,7 @@ void loop() {
6063 int tempButtonState = digitalRead (JOYSTICK_BUTTON_PIN);
6164 if (tempButtonState != buttonState) {
6265 buttonState = tempButtonState;
63-
66+
6467 if (buttonState == LOW) {
6568 Serial.println (F (" Mouse press" ));
6669 bleMouse.press ();
@@ -72,7 +75,7 @@ void loop() {
7275
7376 int x = readJoystickAxis (JOYSTICK_X_AXIS_PIN) - joystickXCenter;
7477 int y = readJoystickAxis (JOYSTICK_Y_AXIS_PIN) - joystickYCenter;
75-
78+
7679 if (x || y) {
7780 bleMouse.move (x, y);
7881 }
@@ -88,6 +91,6 @@ int readJoystickAxis(int pin) {
8891 int rawValue = analogRead (pin);
8992 int mappedValue = map (rawValue, 0 , 1023 , 0 , JOYSTICK_RANGE);
9093 int centeredValue = mappedValue - (JOYSTICK_RANGE / 2 );
91-
94+
9295 return (centeredValue * -1 ); // reverse direction
9396}
0 commit comments