Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
V47 Release Candidate Changes (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored May 29, 2023
2 parents 9eb2e38 + 4c74803 commit 540fe29
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 50 deletions.
100 changes: 73 additions & 27 deletions applications/main/bad_kb/helpers/ducky_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ static inline void update_bt_timeout(Bt* bt) {
}

typedef enum {
WorkerEvtToggle = (1 << 0),
WorkerEvtEnd = (1 << 1),
WorkerEvtConnect = (1 << 2),
WorkerEvtDisconnect = (1 << 3),
WorkerEvtStartStop = (1 << 0),
WorkerEvtPauseResume = (1 << 1),
WorkerEvtEnd = (1 << 2),
WorkerEvtConnect = (1 << 3),
WorkerEvtDisconnect = (1 << 4),
} WorkerEvtFlags;

static const char ducky_cmd_id[] = {"ID"};
Expand Down Expand Up @@ -569,6 +570,7 @@ static int32_t bad_kb_worker(void* context) {
BadKbScript* bad_kb = context;

BadKbWorkerState worker_state = BadKbStateInit;
BadKbWorkerState pause_state = BadKbStateRunning;
int32_t delay_val = 0;

FURI_LOG_I(WORKER_TAG, "Init");
Expand Down Expand Up @@ -615,24 +617,24 @@ static int32_t bad_kb_worker(void* context) {

} else if(worker_state == BadKbStateNotConnected) { // State: Not connected
uint32_t flags = bad_kb_flags_get(
WorkerEvtEnd | WorkerEvtConnect | WorkerEvtToggle, FuriWaitForever);
WorkerEvtEnd | WorkerEvtConnect | WorkerEvtStartStop, FuriWaitForever);

if(flags & WorkerEvtEnd) {
break;
} else if(flags & WorkerEvtConnect) {
worker_state = BadKbStateIdle; // Ready to run
} else if(flags & WorkerEvtToggle) {
} else if(flags & WorkerEvtStartStop) {
worker_state = BadKbStateWillRun; // Will run when connected
}
bad_kb->st.state = worker_state;

} else if(worker_state == BadKbStateIdle) { // State: ready to start
uint32_t flags = bad_kb_flags_get(
WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect, FuriWaitForever);
WorkerEvtEnd | WorkerEvtStartStop | WorkerEvtDisconnect, FuriWaitForever);

if(flags & WorkerEvtEnd) {
break;
} else if(flags & WorkerEvtToggle) { // Start executing script
} else if(flags & WorkerEvtStartStop) { // Start executing script
DOLPHIN_DEED(DolphinDeedBadKbPlayScript);
delay_val = 0;
bad_kb->buf_len = 0;
Expand All @@ -652,7 +654,7 @@ static int32_t bad_kb_worker(void* context) {

} else if(worker_state == BadKbStateWillRun) { // State: start on connection
uint32_t flags = bad_kb_flags_get(
WorkerEvtEnd | WorkerEvtConnect | WorkerEvtToggle, FuriWaitForever);
WorkerEvtEnd | WorkerEvtConnect | WorkerEvtStartStop, FuriWaitForever);

if(flags & WorkerEvtEnd) {
break;
Expand All @@ -668,35 +670,37 @@ static int32_t bad_kb_worker(void* context) {
storage_file_seek(script_file, 0, true);
// extra time for PC to recognize Flipper as keyboard
flags = furi_thread_flags_wait(
WorkerEvtEnd | WorkerEvtDisconnect | WorkerEvtToggle,
WorkerEvtEnd | WorkerEvtDisconnect | WorkerEvtStartStop,
FuriFlagWaitAny | FuriFlagNoClear,
1500);
if(flags == (unsigned)FuriFlagErrorTimeout) {
// If nothing happened - start script execution
worker_state = BadKbStateRunning;
} else if(flags & WorkerEvtToggle) {
} else if(flags & WorkerEvtStartStop) {
worker_state = BadKbStateIdle;
furi_thread_flags_clear(WorkerEvtToggle);
furi_thread_flags_clear(WorkerEvtStartStop);
}
if(bad_kb->bt) {
update_bt_timeout(bad_kb->bt);
}
bad_kb_script_set_keyboard_layout(bad_kb, bad_kb->keyboard_layout);
} else if(flags & WorkerEvtToggle) { // Cancel scheduled execution
} else if(flags & WorkerEvtStartStop) { // Cancel scheduled execution
worker_state = BadKbStateNotConnected;
}
bad_kb->st.state = worker_state;

} else if(worker_state == BadKbStateRunning) { // State: running
uint16_t delay_cur = (delay_val > 1000) ? (1000) : (delay_val);
uint32_t flags = furi_thread_flags_wait(
WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect, FuriFlagWaitAny, delay_cur);
WorkerEvtEnd | WorkerEvtStartStop | WorkerEvtPauseResume | WorkerEvtDisconnect,
FuriFlagWaitAny,
delay_cur);

delay_val -= delay_cur;
if(!(flags & FuriFlagError)) {
if(flags & WorkerEvtEnd) {
break;
} else if(flags & WorkerEvtToggle) {
} else if(flags & WorkerEvtStartStop) {
worker_state = BadKbStateIdle; // Stop executing script
if(bad_kb->bt) {
furi_hal_bt_hid_kb_release_all();
Expand All @@ -710,6 +714,9 @@ static int32_t bad_kb_worker(void* context) {
} else {
furi_hal_hid_kb_release_all();
}
} else if(flags & WorkerEvtPauseResume) {
pause_state = BadKbStateRunning;
worker_state = BadKbStatePaused; // Pause
}
bad_kb->st.state = worker_state;
continue;
Expand Down Expand Up @@ -756,45 +763,79 @@ static int32_t bad_kb_worker(void* context) {
furi_check((flags & FuriFlagError) == 0);
}
} else if(worker_state == BadKbStateWaitForBtn) { // State: Wait for button Press
uint16_t delay_cur = (delay_val > 1000) ? (1000) : (delay_val);
uint32_t flags = furi_thread_flags_wait(
WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect, FuriFlagWaitAny, delay_cur);
uint32_t flags = bad_kb_flags_get(
WorkerEvtEnd | WorkerEvtStartStop | WorkerEvtPauseResume | WorkerEvtDisconnect,
FuriWaitForever);
if(!(flags & FuriFlagError)) {
if(flags & WorkerEvtEnd) {
break;
} else if(flags & WorkerEvtToggle) {
} else if(flags & WorkerEvtStartStop) {
delay_val = 0;
worker_state = BadKbStateRunning;
} else if(flags & WorkerEvtDisconnect) {
worker_state = BadKbStateNotConnected; // USB disconnected
worker_state = BadKbStateNotConnected; // Disconnected
furi_hal_hid_kb_release_all();
}
bad_kb->st.state = worker_state;
continue;
}
} else if(worker_state == BadKbStatePaused) { // State: Paused
uint32_t flags = bad_kb_flags_get(
WorkerEvtEnd | WorkerEvtStartStop | WorkerEvtPauseResume | WorkerEvtDisconnect,
FuriWaitForever);
if(!(flags & FuriFlagError)) {
if(flags & WorkerEvtEnd) {
break;
} else if(flags & WorkerEvtStartStop) {
worker_state = BadKbStateIdle; // Stop executing script
bad_kb->st.state = worker_state;
furi_hal_hid_kb_release_all();
} else if(flags & WorkerEvtDisconnect) {
worker_state = BadKbStateNotConnected; // Disconnected
bad_kb->st.state = worker_state;
furi_hal_hid_kb_release_all();
} else if(flags & WorkerEvtPauseResume) {
if(pause_state == BadKbStateRunning) {
if(delay_val > 0) {
bad_kb->st.state = BadKbStateDelay;
bad_kb->st.delay_remain = delay_val / 1000;
} else {
bad_kb->st.state = BadKbStateRunning;
delay_val = 0;
}
worker_state = BadKbStateRunning; // Resume
} else if(pause_state == BadKbStateStringDelay) {
bad_kb->st.state = BadKbStateRunning;
worker_state = BadKbStateStringDelay; // Resume
}
}
continue;
}
} else if(worker_state == BadKbStateStringDelay) { // State: print string with delays
uint32_t flags = furi_thread_flags_wait(
WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect,
FuriFlagWaitAny,
uint32_t flags = bad_kb_flags_get(
WorkerEvtEnd | WorkerEvtStartStop | WorkerEvtPauseResume | WorkerEvtDisconnect,
bad_kb->stringdelay);

if(!(flags & FuriFlagError)) {
if(flags & WorkerEvtEnd) {
break;
} else if(flags & WorkerEvtToggle) {
} else if(flags & WorkerEvtStartStop) {
worker_state = BadKbStateIdle; // Stop executing script
if(bad_kb->bt) {
furi_hal_bt_hid_kb_release_all();
} else {
furi_hal_hid_kb_release_all();
}
} else if(flags & WorkerEvtDisconnect) {
worker_state = BadKbStateNotConnected; // USB disconnected
worker_state = BadKbStateNotConnected; // Disconnected
if(bad_kb->bt) {
furi_hal_bt_hid_kb_release_all();
} else {
furi_hal_hid_kb_release_all();
}
} else if(flags & WorkerEvtPauseResume) {
pause_state = BadKbStateStringDelay;
worker_state = BadKbStatePaused; // Pause
}
bad_kb->st.state = worker_state;
continue;
Expand Down Expand Up @@ -905,9 +946,14 @@ void bad_kb_script_set_keyboard_layout(BadKbScript* bad_kb, FuriString* layout_p
storage_file_free(layout_file);
}

void bad_kb_script_toggle(BadKbScript* bad_kb) {
void bad_kb_script_start_stop(BadKbScript* bad_kb) {
furi_assert(bad_kb);
furi_thread_flags_set(furi_thread_get_id(bad_kb->thread), WorkerEvtStartStop);
}

void bad_kb_script_pause_resume(BadKbScript* bad_kb) {
furi_assert(bad_kb);
furi_thread_flags_set(furi_thread_get_id(bad_kb->thread), WorkerEvtToggle);
furi_thread_flags_set(furi_thread_get_id(bad_kb->thread), WorkerEvtPauseResume);
}

BadKbState* bad_kb_script_get_state(BadKbScript* bad_kb) {
Expand Down
5 changes: 4 additions & 1 deletion applications/main/bad_kb/helpers/ducky_script.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef enum {
BadKbStateDelay,
BadKbStateStringDelay,
BadKbStateWaitForBtn,
BadKbStatePaused,
BadKbStateDone,
BadKbStateScriptError,
BadKbStateFileError,
Expand Down Expand Up @@ -101,7 +102,9 @@ void bad_kb_script_start(BadKbScript* bad_kb);

void bad_kb_script_stop(BadKbScript* bad_kb);

void bad_kb_script_toggle(BadKbScript* bad_kb);
void bad_kb_script_start_stop(BadKbScript* bad_kb);

void bad_kb_script_pause_resume(BadKbScript* bad_kb);

BadKbState* bad_kb_script_get_state(BadKbScript* bad_kb);

Expand Down
5 changes: 4 additions & 1 deletion applications/main/bad_kb/scenes/bad_kb_scene_work.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ bool bad_kb_scene_work_on_event(void* context, SceneManagerEvent event) {
}
consumed = true;
} else if(event.event == InputKeyOk) {
bad_kb_script_toggle(app->bad_kb_script);
bad_kb_script_start_stop(app->bad_kb_script);
consumed = true;
} else if(event.event == InputKeyRight) {
bad_kb_script_pause_resume(app->bad_kb_script);
consumed = true;
}
} else if(event.type == SceneManagerEventTypeTick) {
Expand Down
Loading

0 comments on commit 540fe29

Please sign in to comment.