Home GSPRO Control box
Post
Cancel

GSPRO Control box

esp32 code to create GSPRO Control box

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
// Benedikt Gabriel Egilsson - bensiegils.com
// 1= 5 - Camera to ball
// 2= KEY_M_CTRL - Muligan 
// 3= T - Scorecard display
// 4= P - See the pin
// 5= O - Flyover of the hole
// 6= . - shadow quality
// 7= B - Hide - Make objects in your line of sight invisible 
// 8= Z - Hide or show the 3D gras
// 9= KEY_UP_ARROW
// 10= KEY_DOWN_ARROW
// 11= KEY_LEFT_ARROW
// 12= KEY_RIGHT_ARROW

#define USE_NIMBLE
#include <BleKeyboard.h>

BleKeyboard bleKeyboard("GSPRO BOX");

#define NUM_KEYS 12

struct Key {
  int pin;
  uint8_t code;
  bool state;
  bool isCtrl;  // New field to indicate if Ctrl needs to be held
};

Key keys[NUM_KEYS] = {
  {16, '5', false, false},
  {17, 'm', false, true},  // 'm' with Ctrl modifier
  {18, 't', false, false},
  {19, 'p', false, false},
  {21, 'o', false, false},
  {22, '.', false, false},
  {32, 'b', false, false},
  {33, 'z', false, false},
  {27, KEY_UP_ARROW, false, false},
  {14, KEY_DOWN_ARROW, false, false},
  {12, KEY_LEFT_ARROW, false, false},
  {13, KEY_RIGHT_ARROW, false, false}
};

bool connectNotificationSent = false;

void setup() {
  Serial.begin(115200);
  Serial.println("Code running...");
  
  // Set pin modes
  for (int i = 0; i < NUM_KEYS; i++) {
    pinMode(keys[i].pin, INPUT_PULLUP);
  }
  
  // Initialize BLE keyboard
  bleKeyboard.begin();
}

void loop() {
  if (bleKeyboard.isConnected()) {
    if (!connectNotificationSent) {
      Serial.println("BLE connected...");
      connectNotificationSent = true;
    }
    
    for (int i = 0; i < NUM_KEYS; i++) {
      handleButton(i);
    }
  } else {
    if (connectNotificationSent) {
      Serial.println("BLE disconnected...");
      connectNotificationSent = false;
    }
  }
  delay(10); // Small delay to stabilize readings
}

void handleButton(int keyIndex) {
  bool currentState = !digitalRead(keys[keyIndex].pin); // Read the button state (active low)
  
  if (currentState != keys[keyIndex].state) {
    keys[keyIndex].state = currentState;
    
    if (currentState) {
      if (keys[keyIndex].isCtrl) { // Check if Ctrl modifier is needed
        bleKeyboard.press(KEY_LEFT_CTRL);
      }
      bleKeyboard.press(keys[keyIndex].code);
      Serial.print("Key pressed: ");
    } else {
      bleKeyboard.release(keys[keyIndex].code);
      if (keys[keyIndex].isCtrl) { // Check if Ctrl modifier is needed
        bleKeyboard.release(KEY_LEFT_CTRL);
      }
      Serial.print("Key released: ");
    }
    Serial.println(keys[keyIndex].code);
  }
}
This post is licensed under CC BY 4.0 by the author.