66#define PIN_KICK 33
77#define CHA_KICK 2
88
9- const int freq = 60 ;
109const int resolution = 8 ;
10+ const int maxOutput = (1 << resolution) - 1 ;
11+
12+ const int rumbleFreq = 8000 ;
13+ const int kickFreq = 60 ;
14+ const int minKick = (maxOutput / 4 ) * 3 ;
1115
1216constexpr inline const bool checkSN (const uint32_t sn) {
1317 return (sn % 48991 ) % 521 == 0 ;
@@ -32,7 +36,7 @@ constexpr inline _Tp simpleMap(_Tp x, _Tp in_max, _Tp out_max) {
3236}
3337
3438void btCallback (esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
35- Serial.printf (" BT Callback: %d\n " , event);
39+ // Serial.printf("BT Callback: %d\n", event);
3640}
3741
3842void setup () {
@@ -55,10 +59,10 @@ void setup() {
5559 pinMode (PIN_RUMBLE , OUTPUT );
5660 pinMode (PIN_KICK , OUTPUT );
5761
58- ledcSetup (CHA_RUMBLE , freq , resolution);
62+ ledcSetup (CHA_RUMBLE , rumbleFreq , resolution);
5963 ledcAttachPin (PIN_RUMBLE , CHA_RUMBLE );
6064
61- ledcSetup (CHA_KICK , freq , resolution);
65+ ledcSetup (CHA_KICK , kickFreq , resolution);
6266 ledcAttachPin (PIN_KICK , CHA_KICK );
6367
6468 SerialBT.register_callback (btCallback);
@@ -88,21 +92,22 @@ void loop() {
8892 switch (payload[1 ]) {
8993 case MAGIC :
9094 if (payload[2 ] == CHANNEL_KICK ) {
91- uint16_t val = simpleMap<uint16_t >(payload[3 ], 255 , 1 << resolution);
92- Serial.printf (" - kick %03u" , val);
93- if (val == 0 ) {
94- delay (100 );
95- }
95+ const auto receivedVal = payload[3 ];
96+ uint16_t val = receivedVal == 0 ? 0 : 255 ;
97+ Serial.printf (" - kick %03u\n " , val);
98+ // if (val == 0) {
99+ // delay(100);
100+ // }
96101 ledcWrite (CHA_KICK , val);
97102 } else if (payload[2 ] == CHANNEL_RUMBLE ) {
98- uint16_t val = simpleMap<uint16_t >(payload[3 ], 255 , 1 << resolution );
103+ uint16_t val = simpleMap<uint16_t >(payload[3 ], 255 , maxOutput );
99104 ledcWrite (CHA_RUMBLE , val);
100- Serial.printf (" - rumble %03u" , val);
101- } else Serial.print (" - unknown effect" );
105+ Serial.printf (" - rumble %03u\n " , val);
106+ } else Serial.print (" - unknown effect\n " );
102107 break ;
103108 default :
104109 Serial.println (" !!! Unknown 2nd value, not MAGIC" );
105110 break ;
106111 }
107- Serial. println ( );
112+ delay ( 5 );
108113}
0 commit comments