Skip to content

Commit 33694cc

Browse files
committed
fix ValueMutex
1 parent 69391ea commit 33694cc

File tree

3 files changed

+57
-52
lines changed

3 files changed

+57
-52
lines changed

Distr/nrf24batch/Kitchen Vent.txt

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Write start: 0,0,0x8F
1818
R: ID*=,ID
1919
R: FanOn=0x77,RAM,0xC1
2020
S: FanSet=,,0x40
21+
S: Lamp=,,0x20
2122
R: FanSpeed=0x76,RAM,0xC1
2223

2324
R: CO2 level 1*2=4,,0xC2
@@ -92,4 +93,6 @@ RBatch: Hardware: ID;RxAddr;Ch;FanSpeedInitIdx;Flags;OutPeriod;IRRemotes;OSCCAL_
9293
RBatch: All: ID;CO2 level 1;CO2 level 2;CO2 level 3;FanCookSpeed_1;FanCookSpeed_2;FanCookSpeed_3;FanCookStartupTime;FanCookOffTime;FanCookSleep;FanSpeed_1;FanSpeed_2;FanSpeed_3;FanStartupTime;FanOffTime;FanSleep;RxAddr;Ch;FanSpeedInitIdx;Flags;OutPeriod;IRRemotes;IRRemotesHash
9394

9495
WBatch: SetSpeed: FanSet=0
96+
WBatch: Lamp ON: Lamp=1
97+
WBatch: Lamp OFF: Lamp=0
9598
WBatch: Reset: Reset

nrf24batch.c

+53-47
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,10 @@ void display_edit_ttf_font(Canvas* const canvas, uint8_t start_x, uint8_t start_
957957
}
958958

959959
static void render_callback(Canvas* const canvas, void* ctx) {
960-
const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
961-
if(plugin_state == NULL) return;
960+
if(ctx == NULL) return;
961+
const PluginState* plugin_state = ctx;
962+
if(furi_mutex_acquire(plugin_state->mutex, 25) != FuriStatusOk) return;
963+
962964
//canvas_draw_frame(canvas, 0, 0, 128, 64); // border around the edge of the screen
963965
if(what_doing == 0) {
964966
canvas_set_font(canvas, FontSecondary); // 8x10 font, 6 lines
@@ -1142,13 +1144,15 @@ static void render_callback(Canvas* const canvas, void* ctx) {
11421144
}
11431145
}
11441146
}
1145-
release_mutex((ValueMutex*)ctx, plugin_state);
1147+
furi_mutex_release(plugin_state->mutex);
11461148
}
11471149

11481150
void work_timer_callback(void* ctx)
11491151
{
1150-
UNUSED(ctx);
1152+
if(ctx == NULL) return;
11511153
if(what_doing == 2) {
1154+
const PluginState* plugin_state = ctx;
1155+
if(furi_mutex_acquire(plugin_state->mutex, 0) != FuriStatusOk) return;
11521156
if(rw_type == rwt_write_batch) {
11531157
if(send_status == sst_ok) {
11541158
if(ERR == 0 && WriteBatch_cmd_curr < Log_Total && furi_get_tick() - NRF_time >= delay_between_pkt) {
@@ -1197,35 +1201,36 @@ void work_timer_callback(void* ctx)
11971201
}
11981202
}
11991203
}
1204+
furi_mutex_release(plugin_state->mutex);
12001205
}
12011206
}
12021207

12031208
int32_t nrf24batch_app(void* p) {
12041209
UNUSED(p);
12051210
APP = malloc(sizeof(nRF24Batch));
1206-
APP->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
1207-
APP->plugin_state = malloc(sizeof(PluginState));
1208-
ValueMutex state_mutex;
1209-
if(!init_mutex(&state_mutex, APP->plugin_state, sizeof(PluginState))) {
1210-
furi_message_queue_free(APP->event_queue);
1211+
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
1212+
PluginState* plugin_state = malloc(sizeof(PluginState));
1213+
plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
1214+
if(!plugin_state->mutex) {
1215+
furi_message_queue_free(event_queue);
12111216
FURI_LOG_E(TAG, "cannot create mutex");
1212-
free(APP->plugin_state);
1217+
free(plugin_state);
12131218
return 255;
12141219
}
12151220

12161221
// Set system callbacks
1217-
APP->view_port = view_port_alloc();
1218-
view_port_draw_callback_set(APP->view_port, render_callback, &state_mutex);
1219-
view_port_input_callback_set(APP->view_port, input_callback, APP->event_queue);
1222+
ViewPort* view_port = view_port_alloc();
1223+
view_port_draw_callback_set(view_port, render_callback, plugin_state);
1224+
view_port_input_callback_set(view_port, input_callback, event_queue);
12201225

12211226
// Open GUI and register view_port
12221227
APP->gui = furi_record_open(RECORD_GUI);
1223-
gui_add_view_port(APP->gui, APP->view_port, GuiLayerFullscreen);
1228+
gui_add_view_port(APP->gui, view_port, GuiLayerFullscreen);
12241229
APP->notification = furi_record_open(RECORD_NOTIFICATION);
12251230
APP->storage = furi_record_open(RECORD_STORAGE);
12261231
storage_common_mkdir(APP->storage, SCAN_APP_PATH_FOLDER);
12271232
file_stream = file_stream_alloc(APP->storage);
1228-
FuriTimer *work_timer = furi_timer_alloc(work_timer_callback, FuriTimerTypePeriodic, NULL);
1233+
FuriTimer *work_timer = furi_timer_alloc(work_timer_callback, FuriTimerTypePeriodic, plugin_state);
12291234
furi_timer_start(work_timer, WORK_PERIOD);
12301235
if(!furi_hal_power_is_otg_enabled()) {
12311236
furi_hal_power_enable_otg();
@@ -1236,8 +1241,8 @@ int32_t nrf24batch_app(void* p) {
12361241

12371242
PluginEvent event;
12381243
for(bool processing = true; processing;) {
1239-
FuriStatus event_status = furi_message_queue_get(APP->event_queue, &event, 200);
1240-
PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex);
1244+
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
1245+
furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
12411246

12421247
static FuriLogLevel FuriLogLevel = FuriLogLevelDefault;
12431248
if(furi_log_get_level() != FuriLogLevel) {
@@ -1471,27 +1476,8 @@ int32_t nrf24batch_app(void* p) {
14711476
ask_question = ask_save_batch;
14721477
ask_question_answer = 0;
14731478
} else if(rw_type == rwt_write_batch) {
1474-
if(!Edit) {
1475-
Edit = 0;
1476-
Edit_hex = 0;
1477-
char *s = (char*)furi_string_get_cstr(Log[view_Batch]);
1478-
char *p = strchr(s, '=');
1479-
if(p) {
1480-
p++;
1481-
if(*p == '{') p++; // array
1482-
if(*(p + 1) == 'x') {
1483-
p += 2;
1484-
Edit_hex = 1; // hex
1485-
}
1486-
if(is_digit(p, Edit_hex)) {
1487-
Edit_start = p;
1488-
while(is_digit(p, Edit_hex)) p++;
1489-
Edit_pos = p - 1;
1490-
Edited = true;
1491-
Edit = 1;
1492-
}
1493-
}
1494-
}
1479+
ask_question = ask_write_batch;
1480+
ask_question_answer = 0;
14951481
}
14961482
}
14971483
}
@@ -1525,8 +1511,27 @@ int32_t nrf24batch_app(void* p) {
15251511
ReadRepeat = !ReadRepeat;
15261512
} else if(Log_Total) {
15271513
if(rw_type == rwt_write_batch) {
1528-
ask_question = ask_write_batch;
1529-
ask_question_answer = 0;
1514+
if(!Edit) {
1515+
Edit = 0;
1516+
Edit_hex = 0;
1517+
char *s = (char*)furi_string_get_cstr(Log[view_Batch]);
1518+
char *p = strchr(s, '=');
1519+
if(p) {
1520+
p++;
1521+
if(*p == '{') p++; // array
1522+
if(*(p + 1) == 'x') {
1523+
p += 2;
1524+
Edit_hex = 1; // hex
1525+
}
1526+
if(is_digit(p, Edit_hex)) {
1527+
Edit_start = p;
1528+
while(is_digit(p, Edit_hex)) p++;
1529+
Edit_pos = p - 1;
1530+
Edited = true;
1531+
Edit = 1;
1532+
}
1533+
}
1534+
}
15301535
} else if(rw_type == rwt_read_batch) {
15311536
ask_question = ask_save_batch;
15321537
ask_question_answer = 0;
@@ -1575,28 +1580,29 @@ int32_t nrf24batch_app(void* p) {
15751580
}
15761581
}
15771582

1578-
view_port_update(APP->view_port);
1579-
release_mutex(&state_mutex, plugin_state);
1583+
view_port_update(view_port);
1584+
furi_mutex_release(plugin_state->mutex);
15801585
}
15811586
nrf24_set_idle(nrf24_HANDLE);
15821587
nrf24_deinit();
15831588
if(NRF_BOARD_POWER_5V) furi_hal_power_disable_otg();
15841589

1585-
view_port_enabled_set(APP->view_port, false);
1586-
gui_remove_view_port(APP->gui, APP->view_port);
1590+
view_port_enabled_set(view_port, false);
1591+
gui_remove_view_port(APP->gui, view_port);
15871592
furi_record_close(RECORD_GUI);
15881593
furi_record_close(RECORD_NOTIFICATION);
15891594
furi_record_close(RECORD_STORAGE);
15901595
if(file_stream) {
15911596
file_stream_close(file_stream);
15921597
stream_free(file_stream);
15931598
}
1594-
view_port_free(APP->view_port);
1595-
furi_message_queue_free(APP->event_queue);
1599+
view_port_free(view_port);
1600+
furi_message_queue_free(event_queue);
15961601
free_store();
15971602
furi_timer_stop(work_timer);
15981603
furi_timer_free(work_timer);
1599-
free(APP->plugin_state);
1604+
furi_mutex_free(plugin_state->mutex);
1605+
free(plugin_state);
16001606
free(APP);
16011607
return 0;
16021608
}

nrf24batch.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@ typedef struct {
1919
} PluginEvent;
2020

2121
typedef struct {
22-
int x;
23-
int y;
22+
FuriMutex* mutex;
2423
} PluginState;
2524

2625
typedef struct {
2726
Gui* gui;
28-
FuriMessageQueue* event_queue;
29-
PluginState* plugin_state;
30-
ViewPort* view_port;
3127
Storage* storage;
3228
NotificationApp* notification;
3329
} nRF24Batch;

0 commit comments

Comments
 (0)