Touch events & rotary events to host
This commit is contained in:
@@ -18,21 +18,67 @@ struct ClassToMessageType
|
||||
enum class MessageFwToHost : uint8_t
|
||||
{
|
||||
RFID_TOKEN_READ = 0,
|
||||
BUTTON_NORMAL_PRESS = 1,
|
||||
ROTARY_ENCODER = 2,
|
||||
ROTARY_ENCODER = 1,
|
||||
TOUCH_BUTTON_PRESS = 2,
|
||||
TOUCH_BUTTON_RELEASE = 3,
|
||||
};
|
||||
|
||||
enum class TouchButton : uint8_t
|
||||
{
|
||||
LEFT_FOOT = 0,
|
||||
RIGHT_FOOT = 1,
|
||||
LEFT_EAR = 2,
|
||||
RIGHT_EAR = 3
|
||||
};
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct MsgRfidTokenRead
|
||||
{
|
||||
uint8_t tagId[5];
|
||||
};
|
||||
|
||||
struct MsgRotaryEncoder
|
||||
{
|
||||
int32_t position;
|
||||
uint8_t direction;
|
||||
};
|
||||
|
||||
struct MsgTouchButtonPress
|
||||
{
|
||||
TouchButton button;
|
||||
};
|
||||
|
||||
struct MsgTouchButtonRelease
|
||||
{
|
||||
TouchButton button;
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
template <>
|
||||
struct ClassToMessageType<MsgRfidTokenRead>
|
||||
{
|
||||
static constexpr auto msgType = MessageFwToHost::RFID_TOKEN_READ;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct ClassToMessageType<MsgRotaryEncoder>
|
||||
{
|
||||
static constexpr auto msgType = MessageFwToHost::ROTARY_ENCODER;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct ClassToMessageType<MsgTouchButtonPress>
|
||||
{
|
||||
static constexpr auto msgType = MessageFwToHost::TOUCH_BUTTON_PRESS;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct ClassToMessageType<MsgTouchButtonRelease>
|
||||
{
|
||||
static constexpr auto msgType = MessageFwToHost::TOUCH_BUTTON_RELEASE;
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------------------------------
|
||||
|
||||
enum class MessageHostToFw : uint8_t
|
||||
|
||||
@@ -58,6 +58,13 @@ void setupRotaryEncoder()
|
||||
ESP_ERROR_CHECK(rotary_encoder_set_queue(&info, eventQueueRotaryEncoder));
|
||||
}
|
||||
|
||||
void handleRotaryEncoder()
|
||||
{
|
||||
rotary_encoder_event_t event = {0};
|
||||
if (xQueueReceive(eventQueueRotaryEncoder, &event, 0) == pdTRUE)
|
||||
sendMessageToHost(MsgRotaryEncoder{event.state.position, (uint8_t)(event.state.direction)});
|
||||
}
|
||||
|
||||
// -------------------------------------------------- Buttons ----------------------------------------
|
||||
|
||||
constexpr int BUTTON1_PIN = 25;
|
||||
@@ -79,7 +86,8 @@ void setupButtons()
|
||||
digitalWrite(BUTTON2_LED_PIN, 1);
|
||||
}
|
||||
|
||||
// -------------------------------------------------- Led circle ----------------------------------------
|
||||
// -------------------------------------------------- Led circle ------------------------------------------
|
||||
|
||||
LedStripRGBW<51> ledStripCircle;
|
||||
Esp32DriverRGBW ledDriverCircle;
|
||||
LedTask<decltype(ledStripCircle)> ledTaskCircle;
|
||||
@@ -88,7 +96,20 @@ void setupLedCircle()
|
||||
{
|
||||
ledDriverCircle.begin(23, 0);
|
||||
ledTaskCircle.begin(ledStripCircle, ledDriverCircle);
|
||||
ledTaskCircle.startEffect(EffectStaticConfig{ColorRGBW{0, 0, 0, 0}});
|
||||
ledTaskCircle.startEffect(EffectStaticConfig(ColorRGBW{0, 0, 0, 0}));
|
||||
}
|
||||
|
||||
// -------------------------------------------------- Mouse Leds -- ----------------------------------------
|
||||
|
||||
LedStripRGBW<12 + 16 + 17> ledStripMouse;
|
||||
Esp32DriverRGBW ledDriverMouse;
|
||||
LedTask<decltype(ledStripMouse)> ledTaskMouse;
|
||||
|
||||
void setupMouseLeds()
|
||||
{
|
||||
ledDriverMouse.begin(16, 1);
|
||||
ledTaskMouse.begin(ledStripMouse, ledDriverMouse);
|
||||
ledTaskMouse.startEffect(EffectStaticConfig{ColorRGBW{0, 0, 0, 0}, 0, 0});
|
||||
}
|
||||
|
||||
// -------------------------------------------------- Touch Buttons ----------------------------------------
|
||||
@@ -107,10 +128,37 @@ void setupTouchButtons()
|
||||
touch_pad_filter_start(2);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------
|
||||
void handleTouchInputs()
|
||||
{
|
||||
static bool previousState[4];
|
||||
|
||||
LedStripRGBW<12 + 16 + 17> ledStripMouse;
|
||||
Esp32DriverRGBW ledDriverMouse;
|
||||
uint16_t touchLeftEar = 0;
|
||||
uint16_t touchRightEar = 0;
|
||||
uint16_t touchLeftFoot = 0;
|
||||
uint16_t touchRightFoot = 0;
|
||||
|
||||
touch_pad_read(TOUCH_PAD_LEFT_FOOT, &touchLeftFoot);
|
||||
touch_pad_read(TOUCH_PAD_RIGHT_FOOT, &touchRightFoot);
|
||||
touch_pad_read(TOUCH_PAD_LEFT_EAR, &touchLeftEar);
|
||||
touch_pad_read(TOUCH_PAD_RIGHT_EAR, &touchRightEar);
|
||||
|
||||
bool currentState[4];
|
||||
currentState[int(TouchButton::LEFT_FOOT)] = touchLeftFoot < 380;
|
||||
currentState[int(TouchButton::RIGHT_FOOT)] = touchRightFoot < 380;
|
||||
currentState[int(TouchButton::LEFT_EAR)] = touchLeftEar < 430;
|
||||
currentState[int(TouchButton::RIGHT_EAR)] = touchRightEar < 430;
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
if (previousState[i] == false && currentState[i] == true)
|
||||
sendMessageToHost(MsgTouchButtonPress{TouchButton(i)});
|
||||
else if (previousState[i] == true && currentState[i] == false)
|
||||
sendMessageToHost(MsgTouchButtonRelease{TouchButton(i)});
|
||||
previousState[i] = currentState[i];
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -120,61 +168,11 @@ void setup()
|
||||
setupLedCircle();
|
||||
setupButtons();
|
||||
setupTouchButtons();
|
||||
|
||||
ledDriverMouse.begin(16, 1);
|
||||
}
|
||||
|
||||
void handleTouchInputs()
|
||||
{
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
handleIncomingMessagesFromHost(&ledTaskCircle);
|
||||
|
||||
int btn1 = !digitalRead(BUTTON1_PIN);
|
||||
int btn2 = !digitalRead(BUTTON2_PIN);
|
||||
int rotaryBtn = !digitalRead(ROTARY_PRESS_PIN);
|
||||
|
||||
uint16_t touchLeftEar = 1, touchRightEar = 1, touchLeftFoot = 1, touchRightFoot = 1;
|
||||
|
||||
touch_pad_read(TOUCH_PAD_LEFT_FOOT, &touchLeftFoot);
|
||||
touch_pad_read(TOUCH_PAD_RIGHT_FOOT, &touchRightFoot);
|
||||
touch_pad_read(TOUCH_PAD_LEFT_EAR, &touchLeftEar); // 430
|
||||
touch_pad_read(TOUCH_PAD_RIGHT_EAR, &touchRightEar); // 430
|
||||
|
||||
//Serial.printf("%d, %d, %d | re %d, le %d, rf %d, lf %d\n", btn1, btn2, rotaryBtn, touchRightEar, touchLeftEar, touchRightFoot, touchLeftFoot);
|
||||
|
||||
for (int i = 0; i < ledStripMouse.numLeds(); ++i)
|
||||
ledStripMouse.set(i, 0, 0, 30, 0);
|
||||
|
||||
bool rightFootPressed = touchRightFoot < 380;
|
||||
bool leftFootPressed = touchLeftFoot < 380;
|
||||
bool leftEarPressed = touchLeftEar < 430;
|
||||
bool rightEarPressed = touchRightEar < 430;
|
||||
|
||||
if (btn1 || rightFootPressed)
|
||||
{
|
||||
for (int i = 0; i < 6; ++i)
|
||||
ledStripMouse.set(i, 180, 0, 255, 0);
|
||||
}
|
||||
|
||||
if (btn2 || leftFootPressed)
|
||||
{
|
||||
for (int i = 6; i < 12; ++i)
|
||||
ledStripMouse.set(i, 180, 0, 255, 0);
|
||||
}
|
||||
|
||||
if (leftEarPressed)
|
||||
for (int i = 12; i < 12 + 16; ++i)
|
||||
ledStripMouse.set(i, 28, 241, 234, 0);
|
||||
|
||||
if (rightEarPressed)
|
||||
for (int i = 12 + 16; i < ledStripMouse.numLeds(); ++i)
|
||||
ledStripMouse.set(i, 28, 241, 234, 0);
|
||||
|
||||
ledDriverMouse.writeSync(ledStripMouse.rawData(), ledStripMouse.numLeds());
|
||||
delay(50);
|
||||
|
||||
//delay(150);
|
||||
handleTouchInputs();
|
||||
handleRotaryEncoder();
|
||||
}
|
||||
Reference in New Issue
Block a user