Sfoglia il codice sorgente

Connect inputs in plaintext serial protocol to actually do things

Scott Bezek 4 anni fa
parent
commit
c56a318bbb

+ 15 - 2
firmware/src/interface_task.cpp

@@ -124,7 +124,7 @@ InterfaceTask::InterfaceTask(const uint8_t task_core, MotorTask& motor_task, Dis
         stream_(),
         motor_task_(motor_task),
         display_task_(display_task),
-        plaintext_protocol_(stream_),
+        plaintext_protocol_(stream_, motor_task_),
         proto_protocol_(stream_) {
     #if SK_DISPLAY
         assert(display_task != nullptr);
@@ -132,6 +132,9 @@ InterfaceTask::InterfaceTask(const uint8_t task_core, MotorTask& motor_task, Dis
 
     log_queue_ = xQueueCreate(10, sizeof(std::string *));
     assert(log_queue_ != NULL);
+
+    knob_state_queue_ = xQueueCreate(1, sizeof(PB_SmartKnobState));
+    assert(knob_state_queue_ != NULL);
 }
 
 InterfaceTask::~InterfaceTask() {}
@@ -161,10 +164,13 @@ void InterfaceTask::run() {
     #endif
 
     motor_task_.setConfig(configs[0]);
+    motor_task_.addListener(knob_state_queue_);
 
 
     // Start in legacy protocol mode
-    plaintext_protocol_.init();
+    plaintext_protocol_.init([this] () {
+        changeConfig(true);
+    });
     SerialProtocol* current_protocol = &plaintext_protocol_;
 
     ProtocolChangeCallback protocol_change_callback = [this, &current_protocol] (uint8_t protocol) {
@@ -186,6 +192,13 @@ void InterfaceTask::run() {
 
     // Interface loop:
     while (1) {
+        PB_SmartKnobState state;
+        if (xQueueReceive(knob_state_queue_, &state, 0) == pdTRUE) {
+            current_protocol->handleState(state);
+        }
+
+        current_protocol->loop();
+
         std::string* log_string;
         while (xQueueReceive(log_queue_, &log_string, 0) == pdTRUE) {
             stream_.println(log_string->c_str());

+ 1 - 0
firmware/src/interface_task.h

@@ -32,6 +32,7 @@ class InterfaceTask : public Task<InterfaceTask>, public Logger {
         int current_config_ = 0;
 
         QueueHandle_t log_queue_;
+        QueueHandle_t knob_state_queue_;
         SerialProtocolPlaintext plaintext_protocol_;
         SerialProtocolProtobuf proto_protocol_;
 

+ 9 - 7
firmware/src/serial/serial_protocol_plaintext.cpp

@@ -20,15 +20,17 @@ void SerialProtocolPlaintext::loop() {
             }
             break;
         }
-        // TODO: implement calibration and mode change here
-        // if (v == ' ') {
-        //     changeConfig(true);
-        // } else if (v == 'C') {
-        //     motor_task_.runCalibration();
-        // }
+        if (b == ' ') {
+            if (demo_config_change_callback_) {
+                demo_config_change_callback_();
+            }
+        } else if (b == 'C') {
+            motor_task_.runCalibration();
+        }
     }
 }
 
-void SerialProtocolPlaintext::init() {
+void SerialProtocolPlaintext::init(DemoConfigChangeCallback cb) {
+    demo_config_change_callback_ = cb;
     stream_.println("SmartKnob starting!\n\nSerial mode: plaintext\nPress 'C' at any time to calibrate.\nPress <Space> to change haptic modes.");
 }

+ 7 - 2
firmware/src/serial/serial_protocol_plaintext.h

@@ -2,20 +2,25 @@
 
 #include "../proto_gen/smartknob.pb.h"
 
+#include "motor_task.h"
 #include "serial_protocol.h"
 #include "uart_stream.h"
 
+typedef std::function<void(void)> DemoConfigChangeCallback;
+
 class SerialProtocolPlaintext : public SerialProtocol {
     public:
-        SerialProtocolPlaintext(Stream& stream) : SerialProtocol(), stream_(stream) {}
+        SerialProtocolPlaintext(Stream& stream, MotorTask& motor_task) : SerialProtocol(), stream_(stream), motor_task_(motor_task) {}
         ~SerialProtocolPlaintext(){}
         void log(const char* msg) override;
         void loop() override;
         void handleState(const PB_SmartKnobState& state) override;
 
-        void init();
+        void init(DemoConfigChangeCallback cb);
     
     private:
         Stream& stream_;
+        MotorTask& motor_task_;
         PB_SmartKnobState latest_state_ = {};
+        DemoConfigChangeCallback demo_config_change_callback_;
 };