Преглед изворни кода

More in-progress proto work (untested)

Scott Bezek пре 4 година
родитељ
комит
4e7d699711

+ 0 - 2
firmware/src/interface_task.cpp

@@ -137,8 +137,6 @@ InterfaceTask::InterfaceTask(const uint8_t task_core, MotorTask& motor_task, Dis
     assert(knob_state_queue_ != NULL);
 }
 
-InterfaceTask::~InterfaceTask() {}
-
 void InterfaceTask::run() {
     stream_.begin();
     

+ 1 - 1
firmware/src/interface_task.h

@@ -16,7 +16,7 @@ class InterfaceTask : public Task<InterfaceTask>, public Logger {
 
     public:
         InterfaceTask(const uint8_t task_core, MotorTask& motor_task, DisplayTask* display_task);
-        ~InterfaceTask();
+        virtual ~InterfaceTask() {};
 
         void log(const char* msg) override;
 

+ 3 - 3
firmware/src/main.cpp

@@ -5,15 +5,15 @@
 #include "motor_task.h"
 
 #if SK_DISPLAY
-static DisplayTask display_task = DisplayTask(0);
+static DisplayTask display_task(0);
 static DisplayTask* display_task_p = &display_task;
 #else
 static DisplayTask* display_task_p = nullptr;
 #endif
-static MotorTask motor_task = MotorTask(1);
+static MotorTask motor_task(1);
 
 
-InterfaceTask interface_task = InterfaceTask(0, motor_task, display_task_p);
+InterfaceTask interface_task(0, motor_task, display_task_p);
 
 void setup() {
   #if SK_DISPLAY

+ 20 - 0
firmware/src/proto_helpers.h

@@ -0,0 +1,20 @@
+#pragma once
+
+#include "proto_gen/smartknob.pb.h"
+
+bool config_eq(PB_SmartKnobConfig& first, PB_SmartKnobConfig& second) {
+    return first.detent_strength_unit == second.detent_strength_unit
+        && first.endstop_strength_unit == second.endstop_strength_unit
+        && first.num_positions == second.num_positions
+        && first.position == second.position
+        && first.position_width_radians == second.position_width_radians
+        && first.snap_point == second.snap_point
+        && strcmp(first.text, second.text) == 0;
+}
+
+bool state_eq(PB_SmartKnobState& first, PB_SmartKnobState& second) {
+    return first.has_config == second.has_config
+        && (!first.has_config || config_eq(first.config, second.config))
+        && first.current_position == second.current_position
+        && first.sub_position_unit == second.sub_position_unit;
+}

+ 21 - 0
firmware/src/serial/crc32.cpp

@@ -0,0 +1,21 @@
+/* Simple public domain implementation of the standard CRC32 checksum.
+ * Outputs the checksum for each file given as a command line argument.
+ * Invalid file names and files that cause errors are silently skipped.
+ * The program reads from stdin if it is called with no arguments. */
+
+#include "crc32.h"
+
+static uint32_t crc32_for_byte(uint32_t r) {
+  for(int j = 0; j < 8; ++j)
+    r = (r & 1? 0: (uint32_t)0xEDB88320L) ^ r >> 1;
+  return r ^ (uint32_t)0xFF000000L;
+}
+
+void crc32(const void *data, size_t n_bytes, uint32_t* crc) {
+  static uint32_t table[0x100];
+  if(!*table)
+    for(size_t i = 0; i < 0x100; ++i)
+      table[i] = crc32_for_byte(i);
+  for(size_t i = 0; i < n_bytes; ++i)
+    *crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
+}

+ 11 - 0
firmware/src/serial/crc32.h

@@ -0,0 +1,11 @@
+/* Simple public domain implementation of the standard CRC32 checksum.
+ * Outputs the checksum for each file given as a command line argument.
+ * Invalid file names and files that cause errors are silently skipped.
+ * The program reads from stdin if it is called with no arguments. */
+#pragma once
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+void crc32(const void *data, size_t n_bytes, uint32_t* crc);

+ 7 - 3
firmware/src/serial/serial_protocol_protobuf.cpp

@@ -1,7 +1,10 @@
+#include <PacketSerial.h>
+
 #include "../proto_gen/smartknob.pb.h"
 
+#include "../proto_helpers.h"
+
 #include "crc32.h"
-#include "PacketSerial.h"
 #include "pb_encode.h"
 #include "pb_decode.h"
 #include "serial_protocol_protobuf.h"
@@ -13,7 +16,8 @@ static const uint16_t PERIODIC_STATE_INTERVAL_MILLIS = 5000;
 
 SerialProtocolProtobuf::SerialProtocolProtobuf(Stream& stream) :
         SerialProtocol(),
-        stream_(stream) {
+        stream_(stream),
+        packet_serial_() {
     packet_serial_.setStream(&stream);
 
     // Note: not threadsafe or instance safe!! but PacketSerial requires a legacy function pointer, so we can't
@@ -52,7 +56,7 @@ void SerialProtocolProtobuf::loop() {
     } while (stream_.available());
 
     // Rate limit state change transmissions
-    bool state_changed = latest_state_ != last_sent_state_ && millis() - last_sent_state_millis_ >= MIN_STATE_INTERVAL_MILLIS;
+    bool state_changed = !state_eq(latest_state_, last_sent_state_) && millis() - last_sent_state_millis_ >= MIN_STATE_INTERVAL_MILLIS;
 
     // Send state periodically or when forced, regardless of rate limit for state changes
     bool force_send_state = state_requested_ || millis() - last_sent_state_millis_ > PERIODIC_STATE_INTERVAL_MILLIS;

+ 3 - 3
firmware/src/serial/serial_protocol_protobuf.h

@@ -1,5 +1,7 @@
 #pragma once
 
+#include <PacketSerial.h>
+
 #include "../proto_gen/smartknob.pb.h"
 
 #include "serial_protocol.h"
@@ -7,13 +9,11 @@
 
 class SerialProtocolProtobuf : public SerialProtocol {
     public:
-        SerialProtocolProtobuf(Stream& stream) : SerialProtocol(), stream_(stream) {}
+        SerialProtocolProtobuf(Stream& stream);
         ~SerialProtocolProtobuf(){}
         void log(const char* msg) override;
         void loop() override;
         void handleState(const PB_SmartKnobState& state) override;
-
-        void init();
     
     private:
         Stream& stream_;