Skip to content

Commit

Permalink
Decoded signal square wave WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Jan 16, 2023
1 parent 004a457 commit f885491
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
7 changes: 5 additions & 2 deletions app.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ ProtoViewApp* protoview_app_alloc() {
app->signal_decoded = false;
app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
app->signal_offset = 0;
app->msg_info = NULL;

//init Worker & Protocol
app->txrx = malloc(sizeof(ProtoViewTxRx));
Expand Down Expand Up @@ -258,12 +259,14 @@ int32_t protoview_app_entry(void* p) {
/* Exit the app. */
app->running = 0;
} else if (input.type == InputTypeShort &&
input.key == InputKeyRight)
input.key == InputKeyRight &&
get_current_subview(app) == 0)
{
/* Go to the next view. */
app_switch_view(app,AppNextView);
} else if (input.type == InputTypeShort &&
input.key == InputKeyLeft)
input.key == InputKeyLeft &&
get_current_subview(app) == 0)
{
/* Go to the previous view. */
app_switch_view(app,AppPrevView);
Expand Down
5 changes: 4 additions & 1 deletion app.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct ProtoViewApp {
uint32_t signal_last_scan_idx; /* Index of the buffer last time we
performed the scan. */
bool signal_decoded; /* Was the current signal decoded? */
ProtoViewMsgInfo signal_info; /* Decoded message, if signal_decoded true. */
ProtoViewMsgInfo *msg_info; /* Decoded message info if not NULL. */
bool direct_sampling_enabled; /* This special view needs an explicit
acknowledge to work. */

Expand Down Expand Up @@ -180,6 +180,8 @@ bool bitmap_match_bits(uint8_t *b, uint32_t blen, uint32_t bitpos, const char *b
uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, uint32_t maxbits, const char *bits);
uint32_t convert_from_line_code(uint8_t *buf, uint64_t buflen, uint8_t *bits, uint32_t len, uint32_t offset, const char *zero_pattern, const char *one_pattern);
uint32_t convert_from_diff_manchester(uint8_t *buf, uint64_t buflen, uint8_t *bits, uint32_t len, uint32_t off, bool previous);
void init_msg_info(ProtoViewMsgInfo *i, ProtoViewApp *app);
void free_msg_info(ProtoViewMsgInfo *i);

/* view_*.c */
void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app);
Expand All @@ -195,6 +197,7 @@ void view_exit_direct_sampling(ProtoViewApp *app);
void view_exit_settings(ProtoViewApp *app);

/* ui.c */
int get_current_subview(ProtoViewApp *app);
void show_available_subviews(Canvas *canvas, ProtoViewApp *app, int last_subview);
bool process_subview_updown(ProtoViewApp *app, InputEvent input, int last_subview);
void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color);
Expand Down
21 changes: 16 additions & 5 deletions signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "app.h"

bool decode_signal(RawSamplesBuffer *s, uint64_t len, ProtoViewMsgInfo *info);
void initialize_msg_info(ProtoViewMsgInfo *i, ProtoViewApp *app);

/* =============================================================================
* Raw signal detection
Expand All @@ -23,6 +22,8 @@ void reset_current_signal(ProtoViewApp *app) {
app->signal_decoded = false;
raw_samples_reset(DetectedSamples);
raw_samples_reset(RawSamples);
free_msg_info(app->msg_info);
app->msg_info = NULL;
}

/* This function starts scanning samples at offset idx looking for the
Expand Down Expand Up @@ -143,7 +144,7 @@ void scan_for_signal(ProtoViewApp *app) {

/* For messages that are long enough, attempt decoding. */
if (thislen > minlen) {
initialize_msg_info(info,app);
init_msg_info(info,app);
uint32_t saved_idx = copy->idx; /* Save index, see later. */
/* decode_signal() expects the detected signal to start
* from index .*/
Expand All @@ -158,7 +159,8 @@ void scan_for_signal(ProtoViewApp *app) {
if ((thislen > app->signal_bestlen && app->signal_decoded == false)
|| (app->signal_decoded == false && decoded))
{
app->signal_info = *info;
free_msg_info(app->msg_info);
app->msg_info = info;
app->signal_bestlen = thislen;
app->signal_decoded = decoded;
raw_samples_copy(DetectedSamples,copy);
Expand All @@ -172,12 +174,13 @@ void scan_for_signal(ProtoViewApp *app) {
app->us_scale = 10;
else if (DetectedSamples->short_pulse_dur < 145)
app->us_scale = 30;
} else {
free_msg_info(info);
}
}
i += thislen ? thislen : 1;
}
raw_samples_free(copy);
free(info);
}

/* =============================================================================
Expand Down Expand Up @@ -502,12 +505,20 @@ ProtoViewDecoder *Decoders[] = {
NULL
};

/* Free the message info and allocated data. */
void free_msg_info(ProtoViewMsgInfo *i) {
if (i == NULL) return;
free(i->bits);
free(i);
}

/* Reset the message info structure before passing it to the decoding
* functions. */
void initialize_msg_info(ProtoViewMsgInfo *i, ProtoViewApp *app) {
void init_msg_info(ProtoViewMsgInfo *i, ProtoViewApp *app) {
UNUSED(app);
memset(i,0,sizeof(ProtoViewMsgInfo));
i->short_pulse_dur = DetectedSamples->short_pulse_dur;
i->bits = NULL;
}

/* This function is called when a new signal is detected. It converts it
Expand Down
10 changes: 8 additions & 2 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@

#include "app.h"

/* Return the ID of the currently selected subview, of the current
* view. */
int get_current_subview(ProtoViewApp *app) {
return app->current_subview[app->current_view];
}

/* Called by view rendering callback that has subviews, to show small triangles
* facing down/up if there are other subviews the user can access with up
* and down. */
void show_available_subviews(Canvas *canvas, ProtoViewApp *app,
int last_subview)
{
int subview = app->current_subview[app->current_view];
int subview = get_current_subview(app);
if (subview != 0)
canvas_draw_triangle(canvas,120,5,8,5,CanvasDirectionBottomToTop);
if (subview != last_subview-1)
Expand All @@ -20,7 +26,7 @@ void show_available_subviews(Canvas *canvas, ProtoViewApp *app,
* such keypress, it returns true, so that the actual view input callback
* knows it can just return ASAP without doing anything. */
bool process_subview_updown(ProtoViewApp *app, InputEvent input, int last_subview) {
int subview = app->current_subview[app->current_view];
int subview = get_current_subview(app);
if (input.type == InputTypePress) {
if (input.key == InputKeyUp) {
if (subview != 0)
Expand Down
32 changes: 23 additions & 9 deletions view_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,41 @@ static void render_subview_main(Canvas *const canvas, ProtoViewApp *app) {
/* Protocol name as title. */
canvas_set_font(canvas, FontPrimary);
uint8_t y = 8, lineheight = 10;
canvas_draw_str(canvas, 0, y, app->signal_info.name);
canvas_draw_str(canvas, 0, y, app->msg_info->name);
y += lineheight;

/* Info fields. */
char buf[128];
canvas_set_font(canvas, FontSecondary);
if (app->signal_info.raw[0]) {
snprintf(buf,sizeof(buf),"Raw: %s", app->signal_info.raw);
if (app->msg_info->raw[0]) {
snprintf(buf,sizeof(buf),"Raw: %s", app->msg_info->raw);
canvas_draw_str(canvas, 0, y, buf);
y += lineheight;
}
canvas_draw_str(canvas, 0, y, app->signal_info.info1); y += lineheight;
canvas_draw_str(canvas, 0, y, app->signal_info.info2); y += lineheight;
canvas_draw_str(canvas, 0, y, app->signal_info.info3); y += lineheight;
canvas_draw_str(canvas, 0, y, app->signal_info.info4); y += lineheight;
canvas_draw_str(canvas, 0, y, app->msg_info->info1); y += lineheight;
canvas_draw_str(canvas, 0, y, app->msg_info->info2); y += lineheight;
canvas_draw_str(canvas, 0, y, app->msg_info->info3); y += lineheight;
canvas_draw_str(canvas, 0, y, app->msg_info->info4); y += lineheight;
}

/* Render view with save option. */
static void render_subview_save(Canvas *const canvas, ProtoViewApp *app) {
UNUSED(canvas);
UNUSED(app);
uint8_t rows = 6;
uint8_t rowheight = 8;
uint8_t bitwidth = 4;
uint8_t bitheight = 5;
uint32_t idx = 0;
bool prevbit = false;
for (uint8_t y = bitheight; y < rows*rowheight; y += rowheight) {
for (uint8_t x = 5; x < 128; x += 4) {
bool bit = bitmap_get(app->msg_info->bits,
app->msg_info->bits_bytes,idx++);
uint8_t prevy = y + prevbit*bitheight - 1;
uint8_t thisy = y + bit*bitheight - 1;
canvas_draw_line(canvas,x,prevy,x,thisy);
canvas_draw_line(canvas,x,thisy,x+bitwidth-1,thisy);
}
}
}

/* Render the selected subview of this view. */
Expand Down
2 changes: 1 addition & 1 deletion view_raw_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app) {
canvas_draw_str_with_border(canvas, 97, 63, buf, ColorWhite, ColorBlack);
if (app->signal_decoded) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_with_border(canvas, 1, 61, app->signal_info.name, ColorWhite, ColorBlack);
canvas_draw_str_with_border(canvas, 1, 61, app->msg_info->name, ColorWhite, ColorBlack);
}
}

Expand Down

0 comments on commit f885491

Please sign in to comment.