Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

create input for tembed devices

Geo3cc3c2774e17
diff --git a/src/Inputs/TembedInput.cpp b/src/Inputs/TembedInput.cpp
new file mode 100644
index 00000000..96e6af11
--- /dev/null
+++ b/src/Inputs/TembedInput.cpp
@@ -0,0 +1,88 @@
+#if defined(DEVICE_TEMBEDS3) || defined(DEVICE_TEMBEDS3CC1101)
+
+#include "TembedInput.h"
+#include "Inputs/InputKeys.h"
+#include <esp_sleep.h>
+#include <Arduino.h>
+
+TembedInput::TembedInput()
+ : encoder(TEMBED_PIN_ENCODE_A, TEMBED_PIN_ENCODE_B, RotaryEncoder::LatchMode::TWO03),
+ lastInput(KEY_NONE),
+ lastPos(0),
+ lastButton(false),
+ pressStart(0)
+{
+ encoder.setPosition(0);
+ pinMode(TEMBED_PIN_ENCODE_BTN, INPUT_PULLUP);
+ pinMode(TEMBED_PIN_SIDE_BTN, INPUT_PULLUP);
+}
+
+void TembedInput::tick() {
+ encoder.tick();
+
+ int pos = encoder.getPosition();
+ if (pos < lastPos) {
+ lastInput = KEY_ARROW_LEFT;
+ lastPos = pos;
+ } else if (pos > lastPos) {
+ lastInput = KEY_ARROW_RIGHT;
+ lastPos = pos;
+ } else if (!digitalRead(TEMBED_PIN_ENCODE_BTN) && !lastButton) {
+ lastInput = KEY_OK;
+ lastButton = true;
+ } else if (digitalRead(TEMBED_PIN_ENCODE_BTN)) {
+ lastButton = false;
+ }
+
+ checkShutdownRequest(); // Check if OK/side button is long pressed
+}
+
+char TembedInput::readChar() {
+ tick();
+ char c = lastInput;
+ lastInput = KEY_NONE;
+ return c;
+}
+
+char TembedInput::handler() {
+ while (true) {
+ char c = readChar();
+ if (c != KEY_NONE) return c;
+ delay(5);
+ }
+}
+
+void TembedInput::waitPress() {
+ while (true) {
+ if (readChar() != KEY_NONE) return;
+ delay(5);
+ }
+}
+
+void TembedInput::checkShutdownRequest() {
+ if (!digitalRead(TEMBED_PIN_ENCODE_BTN) || !digitalRead(TEMBED_PIN_SIDE_BTN)) {
+ unsigned long start = millis();
+
+ // Wait 3sec press
+ for (int i = 3; i > 0; --i) {
+ // Verify if it's still pressed
+ for (int j = 0; j < 10; ++j) {
+ // Released
+ if (digitalRead(TEMBED_PIN_ENCODE_BTN) && digitalRead(TEMBED_PIN_SIDE_BTN)) return;
+ delay(100);
+ }
+ }
+
+ // If we are here, then the button was pressed 3sec
+ shutdownToDeepSleep();
+ }
+}
+
+void TembedInput::shutdownToDeepSleep() {
+ view.shutDown();
+ delay(3000);
+ esp_sleep_enable_ext0_wakeup((gpio_num_t)TEMBED_PIN_SIDE_BTN, 0);
+ esp_deep_sleep_start();
+}
+
+#endif
diff --git a/src/Inputs/TembedInput.h b/src/Inputs/TembedInput.h
new file mode 100644
index 00000000..b7af08bd
--- /dev/null
+++ b/src/Inputs/TembedInput.h
@@ -0,0 +1,43 @@
+#pragma once
+
+#if defined(DEVICE_TEMBEDS3) || defined(DEVICE_TEMBEDS3CC1101)
+
+#include "Interfaces/IInput.h"
+#include <RotaryEncoder.h>
+#include <Arduino.h>
+#include <Views/TembedDeviceView.h>
+
+#ifdef DEVICE_TEMBEDS3CC1101
+ #define TEMBED_PIN_ENCODE_A 4
+ #define TEMBED_PIN_ENCODE_B 5
+ #define TEMBED_PIN_SIDE_BTN 6
+#else
+ #define TEMBED_PIN_ENCODE_A 2
+ #define TEMBED_PIN_ENCODE_B 1
+ #define TEMBED_PIN_SIDE_BTN 0 // side button is reset
+#endif
+
+#define TEMBED_PIN_ENCODE_BTN 0
+
+class TembedInput : public IInput {
+public:
+ TembedInput();
+
+ char handler() override;
+ char readChar() override;
+ void waitPress() override;
+
+ void tick();
+ void checkShutdownRequest();
+ void shutdownToDeepSleep();
+
+private:
+ RotaryEncoder encoder;
+ TembedDeviceView view;
+ char lastInput;
+ char lastButton;
+ int lastPos;
+ unsigned long pressStart;
+};
+
+#endif
\ No newline at end of file