Skip to content

Commit

Permalink
feat(ProTubeVR): update POC sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Aug 11, 2024
1 parent 4ea355f commit ae3f22c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions examples/poc-protubevr-compat/poc-protubevr-compat.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
#define PIN_KICK 33
#define CHA_KICK 2

const int freq = 60;
const int resolution = 8;
const int maxOutput = (1 << resolution) - 1;

const int rumbleFreq = 8000;
const int kickFreq = 60;
const int minKick = (maxOutput / 4) * 3;

constexpr inline const bool checkSN(const uint32_t sn) {
return (sn % 48991) % 521 == 0;
Expand All @@ -32,7 +36,7 @@ constexpr inline _Tp simpleMap(_Tp x, _Tp in_max, _Tp out_max) {
}

void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
Serial.printf("BT Callback: %d\n", event);
// Serial.printf("BT Callback: %d\n", event);
}

void setup() {
Expand All @@ -55,10 +59,10 @@ void setup() {
pinMode(PIN_RUMBLE, OUTPUT);
pinMode(PIN_KICK, OUTPUT);

ledcSetup(CHA_RUMBLE, freq, resolution);
ledcSetup(CHA_RUMBLE, rumbleFreq, resolution);
ledcAttachPin(PIN_RUMBLE, CHA_RUMBLE);

ledcSetup(CHA_KICK, freq, resolution);
ledcSetup(CHA_KICK, kickFreq, resolution);
ledcAttachPin(PIN_KICK, CHA_KICK);

SerialBT.register_callback(btCallback);
Expand Down Expand Up @@ -88,21 +92,22 @@ void loop() {
switch (payload[1]) {
case MAGIC:
if (payload[2] == CHANNEL_KICK) {
uint16_t val = simpleMap<uint16_t>(payload[3], 255, 1 << resolution);
Serial.printf(" - kick %03u", val);
if (val == 0) {
delay(100);
}
const auto receivedVal = payload[3];
uint16_t val = receivedVal == 0 ? 0 : 255;
Serial.printf(" - kick %03u\n", val);
// if (val == 0) {
// delay(100);
// }
ledcWrite(CHA_KICK, val);
} else if (payload[2] == CHANNEL_RUMBLE) {
uint16_t val = simpleMap<uint16_t>(payload[3], 255, 1 << resolution);
uint16_t val = simpleMap<uint16_t>(payload[3], 255, maxOutput);
ledcWrite(CHA_RUMBLE, val);
Serial.printf(" - rumble %03u", val);
} else Serial.print(" - unknown effect");
Serial.printf(" - rumble %03u\n", val);
} else Serial.print(" - unknown effect\n");
break;
default:
Serial.println(" !!! Unknown 2nd value, not MAGIC");
break;
}
Serial.println();
delay(5);
}

0 comments on commit ae3f22c

Please sign in to comment.