Skip to content

Commit

Permalink
Add example steinberg scale driver
Browse files Browse the repository at this point in the history
  • Loading branch information
eamars committed Sep 5, 2023
1 parent 84f5cd2 commit 1501e65
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 36 deletions.
35 changes: 3 additions & 32 deletions src/and_scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "hardware/uart.h"
#include "configuration.h"
#include "scale.h"
#include "eeprom.h"
#include "app.h"


Expand All @@ -27,43 +26,15 @@ typedef union {


// Forward declaration
void scale_listener_task(void *p);
void scale_write(char * command, size_t len);
void _and_scale_listener_task(void *p);
extern scale_config_t scale_config;

// Instance of the scale handle for A&D FXi series
scale_handle_t and_fxi_scale_handle = {
.read_loop_task = scale_listener_task,
.write = scale_write
.read_loop_task = _and_scale_listener_task,
};



static inline void _take_mutex(BaseType_t scheduler_state) {
if (scheduler_state != taskSCHEDULER_NOT_STARTED){
xSemaphoreTake(scale_config.scale_serial_write_access_mutex, portMAX_DELAY);
}
}


static inline void _give_mutex(BaseType_t scheduler_state) {
if (scheduler_state != taskSCHEDULER_NOT_STARTED){
xSemaphoreGive(scale_config.scale_serial_write_access_mutex);
}
}


void scale_write(char * command, size_t len) {
BaseType_t scheduler_state = xTaskGetSchedulerState();

_take_mutex(scheduler_state);

uart_write_blocking(SCALE_UART, (uint8_t *) command, len);

_give_mutex(scheduler_state);
}


float _decode_measurement_msg(scale_standard_data_format_t * msg) {
// Decode header
// Doesn't really matter though..
Expand All @@ -75,7 +46,7 @@ float _decode_measurement_msg(scale_standard_data_format_t * msg) {
}


void scale_listener_task(void *p) {
void _and_scale_listener_task(void *p) {
char string_buf[20];
uint8_t string_buf_idx = 0;

Expand Down
48 changes: 48 additions & 0 deletions src/scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "scale.h"

extern scale_handle_t and_fxi_scale_handle;
extern scale_handle_t steinberg_scale_handle;
scale_config_t scale_config;


Expand All @@ -25,6 +26,10 @@ void set_scale_driver(scale_driver_t scale_driver) {
scale_config.scale_handle = &and_fxi_scale_handle;
break;
}
case SCALE_DRIVER_STEINBERG_SBS:
{
scale_config.scale_handle = &steinberg_scale_handle;
}
default:
assert(false);
break;
Expand Down Expand Up @@ -119,6 +124,49 @@ const char * get_scale_unit_string(bool is_short_string) {
}


const char * get_scale_driver_string() {
const char * scale_driver_string = NULL;

switch (scale_config.persistent_config.scale_driver) {
case SCALE_DRIVER_AND_FXI:
scale_driver_string = "A&D FX-i Std";
break;
case SCALE_DRIVER_STEINBERG_SBS:
scale_driver_string = "Steinberg SBS";
break;
default:
break;
}

return scale_driver_string;
}


static inline void _take_mutex(BaseType_t scheduler_state) {
if (scheduler_state != taskSCHEDULER_NOT_STARTED){
xSemaphoreTake(scale_config.scale_serial_write_access_mutex, portMAX_DELAY);
}
}


static inline void _give_mutex(BaseType_t scheduler_state) {
if (scheduler_state != taskSCHEDULER_NOT_STARTED){
xSemaphoreGive(scale_config.scale_serial_write_access_mutex);
}
}


void scale_write(char * command, size_t len) {
BaseType_t scheduler_state = xTaskGetSchedulerState();

_take_mutex(scheduler_state);

uart_write_blocking(SCALE_UART, (uint8_t *) command, len);

_give_mutex(scheduler_state);
}


bool http_rest_scale_config(struct fs_file *file, int num_params, char *params[], char *values[]) {
static char scale_config_to_json_buffer[32];

Expand Down
12 changes: 8 additions & 4 deletions src/scale.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ typedef struct {
typedef struct {
// Basic functions
void (*read_loop_task)(void *self);
void (*write)(char * cmd, size_t len);
} scale_handle_t;



typedef enum {
SCALE_UNIT_GRAIN = 0,
SCALE_UNIT_GRAM = 1,
SCALE_UNIT_GRAM,
} scale_unit_t;

typedef enum {
SCALE_DRIVER_AND_FXI = 0,
SCALE_DRIVER_STEINBERG_SBS,
} scale_driver_t;


Expand Down Expand Up @@ -61,15 +61,19 @@ extern "C" {
bool scale_init();
float scale_get_current_measurement();
float scale_block_wait_for_next_measurement();
const char * get_scale_unit_string(bool);
void set_scale_unit(scale_unit_t scale_unit);
void set_scale_driver(scale_driver_t scale_driver);
const char * get_scale_unit_string(bool);
const char * get_scale_driver_string();
bool scale_config_save(void);

// Low lever handler for writing data to the scale
void scale_write(char * command, size_t len);

// REST
bool http_rest_scale_weight(struct fs_file *file, int num_params, char *params[], char *values[]);
bool http_rest_scale_config(struct fs_file *file, int num_params, char *params[], char *values[]);

////////////////////////// A&D Scale Implementations /////////////////////////////
// Key bindings
void scale_press_re_zero_key();
void scale_press_print_key();
Expand Down
63 changes: 63 additions & 0 deletions src/steinberg_scale.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <FreeRTOS.h>
#include <queue.h>
#include <stdlib.h>
#include <semphr.h>
#include <time.h>
#include <math.h>
#include <task.h>
#include <stdlib.h>

#include "hardware/uart.h"
#include "configuration.h"
#include "scale.h"
#include "app.h"


// Forward declaration
void _steinberg_scale_listener_task(void *p);
extern scale_config_t scale_config;

// Instance of the scale handle for A&D FXi series
scale_handle_t steinberg_scale_handle = {
.read_loop_task = _steinberg_scale_listener_task,
};


void _steinberg_scale_listener_task(void *p) {
char string_buf[20];
uint8_t string_buf_idx = 0;

while (true) {
// Read all data
while (uart_is_readable(SCALE_UART)) {
char ch = uart_getc(SCALE_UART);
/////////////////////////////////////////////////////////////////////
// TODO: I'm not sure how to decode, please provide example here...//
/////////////////////////////////////////////////////////////////////


// string_buf[string_buf_idx++] = ch;

// // If we have received 17 bytes then we can decode the message
// if (string_buf_idx == 17) {
// // Data is ready, send to decode
// scale_config.current_scale_measurement = _decode_measurement_msg((scale_standard_data_format_t *) string_buf);

// // Signal the data is ready
// if (scale_config.scale_measurement_ready) {
// xSemaphoreGive(scale_config.scale_measurement_ready);
// }

// // Reset
// string_buf_idx = 0;
// }

// // \n is the terminator. We shall reset the receive of message on receiving any of those character.
// if (ch =='\n') {
// string_buf_idx = 0;
// }
}

vTaskDelay(pdMS_TO_TICKS(20));
}
}

0 comments on commit 1501e65

Please sign in to comment.