Skip to content

Commit

Permalink
rename soft timer funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
devAdhiraj committed Nov 19, 2023
1 parent 4a52bf4 commit cbe8b4b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 37 deletions.
11 changes: 6 additions & 5 deletions libraries/ms-common/inc/soft_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ typedef void (*SoftTimerCallback)(SoftTimerId id);
// Adds a software timer. The provided duration is the number of
// miliseconds before running and the callback is the process to run once
// the time has expired.
StatusCode soft_timer_start(uint32_t duration_ms, SoftTimerCallback callback, SoftTimer *timer);
StatusCode soft_timer_init_and_start(uint32_t duration_ms, SoftTimerCallback callback,
SoftTimer *timer);

// Creates a new software timer with given params (same as soft_timer_start) but doesn't start the
// timer
// Creates a new software timer with given params (same as soft_timer_init_and_start) but doesn't
// start the timer
StatusCode soft_timer_init(uint32_t duration_ms, SoftTimerCallback callback, SoftTimer *timer);

// Starts the software timer
StatusCode soft_timer_start_timer(SoftTimer *timer);
// Starts the software timer. The timer must already be initialized
StatusCode soft_timer_start(SoftTimer *timer);

// Cancels the soft timer specified by name. Returns true if successful.
// the timer is not cancelled immediately,
Expand Down
2 changes: 1 addition & 1 deletion libraries/ms-common/inc/watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ typedef SoftTimer WatchDog;

// start watchdog
#define watchdog_start(watchdog, timeout_ms, callback) \
soft_timer_start(timeout_ms, callback, watchdog)
soft_timer_init_and_start(timeout_ms, callback, watchdog)

// :)
#define watchdog_kick(watchdog) soft_timer_reset(watchdog)
Expand Down
7 changes: 4 additions & 3 deletions libraries/ms-common/src/persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static void prv_periodic_commit(SoftTimerId timer_id) {
}

s_context = persist;
soft_timer_start(PERSIST_COMMIT_TIMEOUT_MS, prv_periodic_commit, &persist->timer);
soft_timer_init_and_start(PERSIST_COMMIT_TIMEOUT_MS, prv_periodic_commit, &persist->timer);
}

StatusCode persist_init(PersistStorage *persist, FlashPage page, void *blob, size_t blob_size,
Expand Down Expand Up @@ -120,14 +120,15 @@ StatusCode persist_init(PersistStorage *persist, FlashPage page, void *blob, siz
persist->flash_addr += sizeof(header) + header.size_bytes;
}
s_context = persist;
return soft_timer_start(PERSIST_COMMIT_TIMEOUT_MS, prv_periodic_commit, &persist->timer);
return soft_timer_init_and_start(PERSIST_COMMIT_TIMEOUT_MS, prv_periodic_commit, &persist->timer);
}

StatusCode persist_ctrl_periodic(PersistStorage *persist, bool enabled) {
if (id == soft_timer_invalid_timer && enabled) {
// Enable periodic commit - previously disabled
s_context = persist;
return soft_timer_start(PERSIST_COMMIT_TIMEOUT_MS, prv_periodic_commit, &persist->timer);
return soft_timer_init_and_start(PERSIST_COMMIT_TIMEOUT_MS, prv_periodic_commit,
&persist->timer);
} else if (id != soft_timer_invalid_timer && !enabled) {
// Disable periodic commit - previously enabled
soft_timer_cancel(&persist->timer);
Expand Down
17 changes: 5 additions & 12 deletions libraries/ms-common/src/soft_timer.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
#include "soft_timer.h"

StatusCode soft_timer_start(uint32_t duration_ms, SoftTimerCallback callback, SoftTimer *timer) {
if (timer->id != NULL) {
// timer already exist/inuse, delete the old timer
xTimerDelete(timer->id, 0);
}
timer->id = xTimerCreateStatic(NULL, pdMS_TO_TICKS(duration_ms), pdFALSE, //
NULL, callback, &timer->buffer);
if (xTimerStart(timer->id, 0) != pdPASS) {
return status_msg(STATUS_CODE_INTERNAL_ERROR, "timer command queue is full");
}
return STATUS_CODE_OK;
StatusCode soft_timer_init_and_start(uint32_t duration_ms, SoftTimerCallback callback,
SoftTimer *timer) {
status_ok_or_return(soft_timer_init(duration_ms, callback, timer));
return soft_timer_start(timer);
}

StatusCode soft_timer_init(uint32_t duration_ms, SoftTimerCallback callback, SoftTimer *timer) {
Expand All @@ -23,7 +16,7 @@ StatusCode soft_timer_init(uint32_t duration_ms, SoftTimerCallback callback, Sof
return STATUS_CODE_OK;
}

StatusCode soft_timer_start_timer(SoftTimer *timer) {
StatusCode soft_timer_start(SoftTimer *timer) {
if (timer->id == NULL) {
return STATUS_CODE_UNINITIALIZED;
}
Expand Down
8 changes: 4 additions & 4 deletions libraries/ms-common/test/test_soft_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void test_soft_timer() {
for (int i = 0; i < 3; ++i) {
triggered = false;
// test soft timer start and cancel
soft_timer_start(50, prv_set, &s_timer);
soft_timer_init_and_start(50, prv_set, &s_timer);

xTaskDelayUntil(&last_wake, 49);

Expand All @@ -47,7 +47,7 @@ void test_soft_timer() {
TEST_ASSERT_FALSE(soft_timer_inuse(&s_timer));

// test start to finish
soft_timer_start(200, prv_set, &s_timer);
soft_timer_init_and_start(200, prv_set, &s_timer);

xTaskDelayUntil(&last_wake, 99);

Expand All @@ -70,8 +70,8 @@ void test_multiple_timer() {
for (int i = 0; i < 3; ++i) {
triggered = false;
// test soft timer start and cancel
soft_timer_start(100, prv_set, &s_timer);
soft_timer_start(50, prv_set, &s_timer_2);
soft_timer_init_and_start(100, prv_set, &s_timer);
soft_timer_init_and_start(50, prv_set, &s_timer_2);

delay_ms(51);

Expand Down
4 changes: 2 additions & 2 deletions libraries/ms-drivers/src/ads1015.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void prv_timer_callback(SoftTimerId id) {
current_channel = (Ads1015Channel)(__builtin_ffs(storage->pending_channel_bitset) - 1);
// Update so that the ADS1015 reads from the next channel.
prv_set_channel(storage, current_channel);
soft_timer_start(ADS1015_CHANNEL_UPDATE_PERIOD_US, prv_timer_callback, &s_timer);
soft_timer_init_and_start(ADS1015_CHANNEL_UPDATE_PERIOD_US, prv_timer_callback, &s_timer);
}

// Inits the storage for ADS1015 and starts the soft timer.
Expand All @@ -80,7 +80,7 @@ StatusCode ads1015_init(Ads1015Storage *storage, I2CPort i2c_port, Ads1015Addres
storage->channel_readings[channel] = ADS1015_DISABLED_CHANNEL_READING;
}
s_context = storage;
return soft_timer_start(ADS1015_CHANNEL_UPDATE_PERIOD_US, prv_timer_callback, &s_timer);
return soft_timer_init_and_start(ADS1015_CHANNEL_UPDATE_PERIOD_US, prv_timer_callback, &s_timer);
}

// Enable/disables a channel, and sets a callback for the channel.
Expand Down
4 changes: 2 additions & 2 deletions projects/bms_carrier/src/current_sense.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static void prv_periodic_ads_read(SoftTimerId id) {
s_current_storage->ring_idx = (s_current_storage->ring_idx + 1) % NUM_STORED_CURRENT_READINGS;
ads1259_get_conversion_data(&s_ads1259_storage);
// Kick new soft timer
soft_timer_start(s_current_storage->conv_period_ms, prv_periodic_ads_read, &s_timer);
soft_timer_init_and_start(s_current_storage->conv_period_ms, prv_periodic_ads_read, &s_timer);

// update average
int32_t sum = 0;
Expand Down Expand Up @@ -92,6 +92,6 @@ StatusCode current_sense_init(CurrentStorage *storage, SpiSettings *settings,
s_current_storage->conv_period_ms = conv_period_ms;
status_ok_or_return(ads1259_init(&s_ads1259_storage, &ads_settings));
ads1259_get_conversion_data(&s_ads1259_storage);
soft_timer_start(s_current_storage->conv_period_ms, prv_periodic_ads_read, &s_timer);
soft_timer_init_and_start(s_current_storage->conv_period_ms, prv_periodic_ads_read, &s_timer);
return STATUS_CODE_OK;
}
16 changes: 8 additions & 8 deletions projects/power_distribution/src/lights_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void prv_lights_signal_blinker(SoftTimerId id) {
#endif
pd_set_output_group(OUTPUT_GROUP_RIGHT_TURN, right_signal_state);
}
soft_timer_start_timer(&s_timer_single);
soft_timer_start(&s_timer_single);
break;
case EE_LIGHT_TYPE_SIGNAL_RIGHT:
right_signal_state ^= 1;
Expand All @@ -50,7 +50,7 @@ static void prv_lights_signal_blinker(SoftTimerId id) {
#endif
pd_set_output_group(OUTPUT_GROUP_LEFT_TURN, left_signal_state);
}
soft_timer_start_timer(&s_timer_single);
soft_timer_start(&s_timer_single);
break;
case EE_LIGHT_TYPE_SIGNAL_HAZARD:
if (left_signal_state != right_signal_state) {
Expand All @@ -64,7 +64,7 @@ static void prv_lights_signal_blinker(SoftTimerId id) {
gpio_set_state(&RIGHT_LIGHT_ADDR, left_signal_state);
#endif
pd_set_output_group(OUTPUT_GROUP_HAZARD, left_signal_state);
soft_timer_start_timer(&s_timer_single);
soft_timer_start(&s_timer_single);
break;
default:
left_signal_state = OUTPUT_STATE_OFF;
Expand All @@ -79,8 +79,8 @@ static void prv_lights_signal_blinker(SoftTimerId id) {

static void prv_init_state_input(Fsm *fsm, void *context) {
// can transition to LEFT, RIGHT, HAZARD
EELightType light_event = get_steering_info_input_lights(); // can msg id = 682 = 0x2AA
HazardStatus hazard_status = get_power_info_hazard_state(); // can msg id = 1026 = 0x5E2
EELightType light_event = get_steering_info_input_lights(); // can msg id = 682 = 0x2AA
HazardStatus hazard_status = get_power_info_hazard_state(); // can msg id = 1026 = 0x5E2

if (hazard_status == HAZARD_ON) {
fsm_transition(fsm, HAZARD);
Expand Down Expand Up @@ -116,7 +116,7 @@ static void prv_left_signal_output(void *context) {
// Toggle Left Signal blinkers at 100 BPM -> 0.6s
light_id_callback = EE_LIGHT_TYPE_SIGNAL_LEFT;
if (fsm_prev_state == INIT_STATE) {
soft_timer_start_timer(&s_timer_single);
soft_timer_start(&s_timer_single);
}
fsm_prev_state = LEFT_SIGNAL;
}
Expand All @@ -140,7 +140,7 @@ static void prv_right_signal_output(void *context) {
// Toggle Right Signal blinkers at 100 BPM -> 0.6 s
light_id_callback = EE_LIGHT_TYPE_SIGNAL_RIGHT;
if (fsm_prev_state == INIT_STATE) {
soft_timer_start_timer(&s_timer_single);
soft_timer_start(&s_timer_single);
}
fsm_prev_state = RIGHT_SIGNAL;
}
Expand All @@ -167,7 +167,7 @@ static void prv_hazard_output(void *context) {
// Toggle Left and Right Signal blinkers at 100 BPM -> 0.6s
light_id_callback = EE_LIGHT_TYPE_SIGNAL_HAZARD;
if (fsm_prev_state == INIT_STATE) {
soft_timer_start_timer(&s_timer_single);
soft_timer_start(&s_timer_single);
}
fsm_prev_state = HAZARD;
}
Expand Down

0 comments on commit cbe8b4b

Please sign in to comment.