Explorar o código

Use PB structs

Scott Bezek %!s(int64=4) %!d(string=hai) anos
pai
achega
d911fb5f11

+ 5 - 5
firmware/src/display_task.cpp

@@ -7,7 +7,7 @@
 static const uint8_t LEDC_CHANNEL_LCD_BACKLIGHT = 0;
 
 DisplayTask::DisplayTask(const uint8_t task_core) : Task{"Display", 2048, 1, task_core} {
-  knob_state_queue_ = xQueueCreate(1, sizeof(KnobState));
+  knob_state_queue_ = xQueueCreate(1, sizeof(PB_SmartKnobState));
   assert(knob_state_queue_ != NULL);
 
   mutex_ = xSemaphoreCreateMutex();
@@ -40,7 +40,7 @@ void DisplayTask::run() {
     }
     spr_.setTextColor(0xFFFF, TFT_BLACK);
     
-    KnobState state;
+    PB_SmartKnobState state;
 
     const int RADIUS = TFT_WIDTH / 2;
     const uint16_t FILL_COLOR = spr_.color565(90, 18, 151);
@@ -63,15 +63,15 @@ void DisplayTask::run() {
         spr_.drawString(String() + state.current_position, TFT_WIDTH / 2, TFT_HEIGHT / 2 - VALUE_OFFSET, 1);
         spr_.setFreeFont(&DESCRIPTION_FONT);
         int32_t line_y = TFT_HEIGHT / 2 + DESCRIPTION_Y_OFFSET;
-        char* start = state.config.descriptor;
-        char* end = start + strlen(state.config.descriptor);
+        char* start = state.config.text;
+        char* end = start + strlen(state.config.text);
         while (start < end) {
           char* newline = strchr(start, '\n');
           if (newline == nullptr) {
             newline = end;
           }
           
-          char buf[sizeof(state.config.descriptor)] = {};
+          char buf[sizeof(state.config.text)] = {};
           strncat(buf, start, min(sizeof(buf) - 1, (size_t)(newline - start)));
           spr_.drawString(String(buf), TFT_WIDTH / 2, line_y, 1);
           start = newline + 1;

+ 2 - 2
firmware/src/display_task.h

@@ -5,8 +5,8 @@
 #include <Arduino.h>
 #include <TFT_eSPI.h>
 
-#include "knob_data.h"
 #include "logger.h"
+#include "proto_gen/smartknob.pb.h"
 #include "task.h"
 
 class DisplayTask : public Task<DisplayTask> {
@@ -32,7 +32,7 @@ class DisplayTask : public Task<DisplayTask> {
 
         QueueHandle_t knob_state_queue_;
 
-        KnobState state_;
+        PB_SmartKnobState state_;
         SemaphoreHandle_t mutex_;
         uint16_t brightness_;
         Logger* logger_;

+ 3 - 3
firmware/src/interface_task.cpp

@@ -31,14 +31,14 @@ HX711 scale;
 Adafruit_VEML7700 veml = Adafruit_VEML7700();
 #endif
 
-static KnobConfig configs[] = {
+static PB_SmartKnobConfig configs[] = {
     // int32_t num_positions;
     // int32_t position;
     // float position_width_radians;
     // float detent_strength_unit;
     // float endstop_strength_unit;
     // float snap_point;
-    // char descriptor[50];
+    // char text[51];
 
     {
         0,
@@ -313,7 +313,7 @@ void InterfaceTask::changeConfig(bool next) {
     }
     
     char buf_[256];
-    snprintf(buf_, sizeof(buf_), "Changing config to %d -- %s", current_config_, configs[current_config_].descriptor);
+    snprintf(buf_, sizeof(buf_), "Changing config to %d -- %s", current_config_, configs[current_config_].text);
     log(buf_);
     motor_task_.setConfig(configs[current_config_]);
 }

+ 0 - 19
firmware/src/knob_data.h

@@ -1,19 +0,0 @@
-#pragma once
-
-#include <Arduino.h>
-
-struct KnobConfig {
-    int32_t num_positions;
-    int32_t position;
-    float position_width_radians;
-    float detent_strength_unit;
-    float endstop_strength_unit;
-    float snap_point;
-    char descriptor[50];
-};
-
-struct KnobState {
-    int32_t current_position;
-    float sub_position_unit;
-    KnobConfig config;
-};

+ 1 - 1
firmware/src/main.cpp

@@ -29,7 +29,7 @@ void setup() {
   interface_task.begin();
 
   // // Create a queue and register it with motor_task to print knob state to serial (see loop() below)
-  // knob_state_debug_queue = xQueueCreate(1, sizeof(KnobState));
+  // knob_state_debug_queue = xQueueCreate(1, sizeof(PB_SmartKnobState));
   // assert(knob_state_debug_queue != NULL);
   // motor_task.addListener(knob_state_debug_queue);
 

+ 5 - 4
firmware/src/motor_task.cpp

@@ -84,7 +84,7 @@ void MotorTask::run() {
     // disableCore0WDT();
 
     float current_detent_center = motor.shaft_angle;
-    KnobConfig config = {
+    PB_SmartKnobConfig config = {
         .num_positions = 2,
         .position = 0,
         .position_width_radians = 60 * _PI / 180,
@@ -95,7 +95,7 @@ void MotorTask::run() {
     uint32_t last_idle_start = 0;
     uint32_t last_publish = 0;
 
-    KnobConfig latest_config = config;
+    PB_SmartKnobConfig latest_config = config;
 
     while (1) {
         motor.loopFOC();
@@ -212,6 +212,7 @@ void MotorTask::run() {
             publish({
                 .current_position = config.position,
                 .sub_position_unit = -angle_to_detent_center / config.position_width_radians,
+                .has_config = true,
                 .config = config,
             });
             last_publish = millis();
@@ -223,7 +224,7 @@ void MotorTask::run() {
     }
 }
 
-void MotorTask::setConfig(const KnobConfig& config) {
+void MotorTask::setConfig(const PB_SmartKnobConfig& config) {
     Command command = {
         .command_type = CommandType::CONFIG,
         .data = {
@@ -261,7 +262,7 @@ void MotorTask::addListener(QueueHandle_t queue) {
     listeners_.push_back(queue);
 }
 
-void MotorTask::publish(const KnobState& state) {
+void MotorTask::publish(const PB_SmartKnobState& state) {
     for (auto listener : listeners_) {
         xQueueOverwrite(listener, &state);
     }

+ 4 - 4
firmware/src/motor_task.h

@@ -4,8 +4,8 @@
 #include <SimpleFOC.h>
 #include <vector>
 
-#include "knob_data.h"
 #include "logger.h"
+#include "proto_gen/smartknob.pb.h"
 #include "task.h"
 
 
@@ -23,7 +23,7 @@ struct Command {
     CommandType command_type;
     union CommandData {
         uint8_t unused;
-        KnobConfig config;
+        PB_SmartKnobConfig config;
         HapticData haptic;
     };
     CommandData data;
@@ -36,7 +36,7 @@ class MotorTask : public Task<MotorTask> {
         MotorTask(const uint8_t task_core);
         ~MotorTask();
 
-        void setConfig(const KnobConfig& config);
+        void setConfig(const PB_SmartKnobConfig& config);
         void playHaptic(bool press);
         void runCalibration();
 
@@ -56,7 +56,7 @@ class MotorTask : public Task<MotorTask> {
         BLDCMotor motor = BLDCMotor(1);
         BLDCDriver6PWM driver = BLDCDriver6PWM(PIN_UH, PIN_UL, PIN_VH, PIN_VL, PIN_WH, PIN_WL);
 
-        void publish(const KnobState& state);
+        void publish(const PB_SmartKnobState& state);
         void calibrate();
         void checkSensorError();
         void log(const char* msg);

+ 45 - 17
firmware/src/proto_gen/smartknob.pb.h

@@ -14,14 +14,6 @@ typedef struct _PB_RequestState {
     char dummy_field;
 } PB_RequestState;
 
-typedef struct _PB_SmartKnobConfig {
-    char dummy_field;
-} PB_SmartKnobConfig;
-
-typedef struct _PB_SmartKnobState {
-    char dummy_field;
-} PB_SmartKnobState;
-
 typedef struct _PB_Ack {
     uint32_t nonce;
 } PB_Ack;
@@ -30,6 +22,23 @@ typedef struct _PB_Log {
     char msg[256];
 } PB_Log;
 
+typedef struct _PB_SmartKnobConfig {
+    int32_t num_positions;
+    int32_t position;
+    float position_width_radians;
+    float detent_strength_unit;
+    float endstop_strength_unit;
+    float snap_point;
+    char text[51];
+} PB_SmartKnobConfig;
+
+typedef struct _PB_SmartKnobState {
+    int32_t current_position;
+    float sub_position_unit;
+    bool has_config;
+    PB_SmartKnobConfig config;
+} PB_SmartKnobState;
+
 /* Message TO the Smartknob from the USB host */
 typedef struct _PB_ToSmartknob {
     uint32_t nonce;
@@ -59,21 +68,31 @@ extern "C" {
 #define PB_FromSmartKnob_init_default            {0, {PB_Ack_init_default}}
 #define PB_Ack_init_default                      {0}
 #define PB_Log_init_default                      {""}
-#define PB_SmartKnobState_init_default           {0}
+#define PB_SmartKnobState_init_default           {0, 0, false, PB_SmartKnobConfig_init_default}
 #define PB_ToSmartknob_init_default              {0, 0, {PB_RequestState_init_default}}
-#define PB_SmartKnobConfig_init_default          {0}
+#define PB_SmartKnobConfig_init_default          {0, 0, 0, 0, 0, 0, ""}
 #define PB_RequestState_init_default             {0}
 #define PB_FromSmartKnob_init_zero               {0, {PB_Ack_init_zero}}
 #define PB_Ack_init_zero                         {0}
 #define PB_Log_init_zero                         {""}
-#define PB_SmartKnobState_init_zero              {0}
+#define PB_SmartKnobState_init_zero              {0, 0, false, PB_SmartKnobConfig_init_zero}
 #define PB_ToSmartknob_init_zero                 {0, 0, {PB_RequestState_init_zero}}
-#define PB_SmartKnobConfig_init_zero             {0}
+#define PB_SmartKnobConfig_init_zero             {0, 0, 0, 0, 0, 0, ""}
 #define PB_RequestState_init_zero                {0}
 
 /* Field tags (for use in manual encoding/decoding) */
 #define PB_Ack_nonce_tag                         1
 #define PB_Log_msg_tag                           1
+#define PB_SmartKnobConfig_num_positions_tag     1
+#define PB_SmartKnobConfig_position_tag          2
+#define PB_SmartKnobConfig_position_width_radians_tag 3
+#define PB_SmartKnobConfig_detent_strength_unit_tag 4
+#define PB_SmartKnobConfig_endstop_strength_unit_tag 5
+#define PB_SmartKnobConfig_snap_point_tag        6
+#define PB_SmartKnobConfig_text_tag              7
+#define PB_SmartKnobState_current_position_tag   1
+#define PB_SmartKnobState_sub_position_unit_tag  2
+#define PB_SmartKnobState_config_tag             3
 #define PB_ToSmartknob_nonce_tag                 1
 #define PB_ToSmartknob_request_state_tag         2
 #define PB_ToSmartknob_smartknob_config_tag      3
@@ -103,9 +122,12 @@ X(a, STATIC,   SINGULAR, STRING,   msg,               1)
 #define PB_Log_DEFAULT NULL
 
 #define PB_SmartKnobState_FIELDLIST(X, a) \
-
+X(a, STATIC,   SINGULAR, INT32,    current_position,   1) \
+X(a, STATIC,   SINGULAR, FLOAT,    sub_position_unit,   2) \
+X(a, STATIC,   OPTIONAL, MESSAGE,  config,            3)
 #define PB_SmartKnobState_CALLBACK NULL
 #define PB_SmartKnobState_DEFAULT NULL
+#define PB_SmartKnobState_config_MSGTYPE PB_SmartKnobConfig
 
 #define PB_ToSmartknob_FIELDLIST(X, a) \
 X(a, STATIC,   SINGULAR, UINT32,   nonce,             1) \
@@ -117,7 +139,13 @@ X(a, STATIC,   ONEOF,    MESSAGE,  (payload,smartknob_config,payload.smartknob_c
 #define PB_ToSmartknob_payload_smartknob_config_MSGTYPE PB_SmartKnobConfig
 
 #define PB_SmartKnobConfig_FIELDLIST(X, a) \
-
+X(a, STATIC,   SINGULAR, INT32,    num_positions,     1) \
+X(a, STATIC,   SINGULAR, INT32,    position,          2) \
+X(a, STATIC,   SINGULAR, FLOAT,    position_width_radians,   3) \
+X(a, STATIC,   SINGULAR, FLOAT,    detent_strength_unit,   4) \
+X(a, STATIC,   SINGULAR, FLOAT,    endstop_strength_unit,   5) \
+X(a, STATIC,   SINGULAR, FLOAT,    snap_point,        6) \
+X(a, STATIC,   SINGULAR, STRING,   text,              7)
 #define PB_SmartKnobConfig_CALLBACK NULL
 #define PB_SmartKnobConfig_DEFAULT NULL
 
@@ -148,9 +176,9 @@ extern const pb_msgdesc_t PB_RequestState_msg;
 #define PB_FromSmartKnob_size                    261
 #define PB_Log_size                              258
 #define PB_RequestState_size                     0
-#define PB_SmartKnobConfig_size                  0
-#define PB_SmartKnobState_size                   0
-#define PB_ToSmartknob_size                      8
+#define PB_SmartKnobConfig_size                  94
+#define PB_SmartKnobState_size                   112
+#define PB_ToSmartknob_size                      102
 
 #ifdef __cplusplus
 } /* extern "C" */

+ 10 - 0
proto/smartknob.proto

@@ -24,6 +24,9 @@ message Log {
 }
 
 message SmartKnobState {
+    int32 current_position = 1;
+    float sub_position_unit = 2;
+    SmartKnobConfig config = 3;
 }
 
 /*
@@ -39,6 +42,13 @@ message ToSmartknob {
 }
 
 message SmartKnobConfig {
+    int32 num_positions = 1;
+    int32 position = 2;
+    float position_width_radians = 3;
+    float detent_strength_unit = 4;
+    float endstop_strength_unit = 5;
+    float snap_point = 6;
+    string text = 7 [(nanopb).max_length = 50];
 }
 
 message RequestState {}