Skip to content

Commit 4cc3c60

Browse files
committed
Making tmTime and currentTime static
1 parent 9889947 commit 4cc3c60

File tree

6 files changed

+49
-39
lines changed

6 files changed

+49
-39
lines changed

buzzer/melody_notes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#define REST 0
9494

9595
enum {
96+
MELODY_NONE,
9697
MELODY_GOTCHI_FAULT,
9798
MELODY_ESC_FAULT,
9899
MELODY_BLE_FAIL,

command_interface.c

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,41 @@ extern uint16_t m_ble_fus_max_data_len;
2121
extern void user_cfg_set(bool restart_telemetry_timer);
2222
extern struct gotchi_configuration gotchi_cfg_user;
2323

24-
static lfs_file_t file;
24+
extern bool sync_in_progress;
25+
extern struct lfs_config cfg;
26+
extern volatile bool update_rtc;
27+
28+
extern volatile bool log_file_active;
29+
30+
static lfs_file_t file_command_interface;
2531
static uint8_t lfs_file_buf[256]; // Must be cache size
2632
static struct lfs_file_config lfs_file_config;
2733
static lfs_dir_t directory;
2834
static int32_t bytes_sent = -1; //file.ctz.size;
2935

30-
void (*m_ble_tx_logbuffer)(unsigned char *data, unsigned int len);
36+
static void (*m_ble_tx_logbuffer)(unsigned char *data, unsigned int len);
3137
static lfs_t *m_lfs;
3238

3339
static int command_input_index = 0;
3440
static char command_input_buffer[ 128 ] = { 0 };
35-
static unsigned char command_response_buffer[512];
41+
static unsigned char command_response_buffer[256];
3642

37-
extern bool sync_in_progress;
43+
static void (*m_update_time)(int syear, int smonth, int sday, int shour, int sminute, int ssecond);
3844

39-
void command_interface_init(void (*ble_send_logbuffer)(unsigned char *, unsigned int), lfs_t *lfs)
45+
void command_interface_init(void (*ble_send_logbuffer)(unsigned char *, unsigned int), lfs_t *lfs, void (*update_time)(int, int, int, int, int, int))
4046
{
4147
ASSERT(lfs != NULL);
4248
ASSERT(ble_send_logbuffer != NULL);
4349
m_lfs = lfs;
4450
m_ble_tx_logbuffer = ble_send_logbuffer;
51+
m_update_time = update_time;
4552

4653
// Prepare the static file buffer
4754
memset(&lfs_file_config, 0, sizeof(struct lfs_file_config));
4855
lfs_file_config.buffer = lfs_file_buf;
4956
lfs_file_config.attr_count = 0;
5057
}
5158

52-
extern time_t currentTime;
53-
extern struct tm * tmTime;
54-
extern struct lfs_config cfg;
55-
extern volatile bool update_rtc;
56-
57-
extern volatile bool log_file_active;
58-
5959
void command_interface_process_byte(char incoming)
6060
{
6161
command_input_buffer[ command_input_index++ ] = incoming;
@@ -88,7 +88,7 @@ void command_interface_process_byte(char incoming)
8888
if(strncmp(command_input_buffer, "cat", 3) == 0)
8989
{
9090
// Set the file position to what we've received on the client side
91-
lfs_file_seek(m_lfs, &file, atoi(command_input_buffer+4), LFS_SEEK_SET);
91+
lfs_file_seek(m_lfs, &file_command_interface, atoi(command_input_buffer+4), LFS_SEEK_SET);
9292
}
9393
// Continue to send data to the client
9494
command_interface_continue_transfer(command_input_buffer);
@@ -126,13 +126,7 @@ void command_interface_process_byte(char incoming)
126126
NRF_LOG_FLUSH();
127127

128128
// Update time in memory
129-
tmTime->tm_year = syear - 1900;
130-
tmTime->tm_mon = smonth - 1;
131-
tmTime->tm_mday = sday;
132-
tmTime->tm_hour = shour;
133-
tmTime->tm_min = sminute;
134-
tmTime->tm_sec = ssecond;
135-
currentTime = mktime(tmTime);
129+
m_update_time(syear, smonth, sday, shour, sminute, ssecond);
136130

137131
// Update time on RTC
138132
update_rtc = true;
@@ -189,7 +183,7 @@ void command_interface_process_byte(char incoming)
189183

190184
NRF_LOG_INFO("filename %s", filename);
191185
NRF_LOG_FLUSH();
192-
if(lfs_file_opencfg(m_lfs, &file, filename, LFS_O_RDONLY, &lfs_file_config) >= 0)
186+
if(lfs_file_opencfg(m_lfs, &file_command_interface, filename, LFS_O_RDONLY, &lfs_file_config) >= 0)
193187
{
194188
sprintf((char *)command_response_buffer, "cat,%s", filename);
195189
m_ble_tx_logbuffer(command_response_buffer, (size_t)strlen((const char *)command_response_buffer));
@@ -358,7 +352,7 @@ void command_interface_process_byte(char incoming)
358352
NRF_LOG_FLUSH();
359353
if (sync_in_progress)
360354
{
361-
int close_result = lfs_file_close(m_lfs, &file);
355+
int close_result = lfs_file_close(m_lfs, &file_command_interface);
362356
NRF_LOG_INFO("lfs close result: %d", close_result);
363357
NRF_LOG_FLUSH();
364358
sync_in_progress = false;
@@ -417,23 +411,23 @@ void command_interface_continue_transfer(char* command)
417411
NRF_LOG_INFO("command_interface_continue_transfer(TRANSFER_MODE_CAT)");
418412
NRF_LOG_FLUSH();
419413

420-
if(bytes_sent >= file.ctz.size)
414+
if(bytes_sent >= file_command_interface.ctz.size)
421415
{
422416
m_ble_tx_logbuffer((unsigned char *)"cat,complete", strlen("cat,complete"));
423417

424418
NRF_LOG_INFO("finished cat");
425419
NRF_LOG_FLUSH();
426420

427-
int close_result = lfs_file_close(m_lfs, &file);
421+
int close_result = lfs_file_close(m_lfs, &file_command_interface);
428422

429423
NRF_LOG_INFO("cat close result: %d", close_result);
430424
NRF_LOG_FLUSH();
431425

432426
sync_in_progress = false;
433427
}
434-
else if(bytes_sent < file.ctz.size)
428+
else if(bytes_sent < file_command_interface.ctz.size)
435429
{
436-
int32_t read_response = lfs_file_read(m_lfs, &file, &command_response_buffer, m_ble_fus_max_data_len);
430+
int32_t read_response = lfs_file_read(m_lfs, &file_command_interface, &command_response_buffer, m_ble_fus_max_data_len);
437431
if(read_response > 0)
438432
{
439433
m_ble_tx_logbuffer(command_response_buffer, read_response);

command_interface.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ enum{
1212

1313

1414
void command_interface_process_byte( char incoming );
15-
void command_interface_init(void (*ble_send_logbuffer)(unsigned char *, unsigned int), lfs_t *lfs );
15+
void command_interface_init(
16+
void (*ble_send_logbuffer)(unsigned char *, unsigned int),
17+
lfs_t *lfs,
18+
void (*update_time)(int, int, int, int, int, int)
19+
);
1620
void command_interface_continue_transfer();
1721

1822

main.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,26 @@ void i2c_oled_comm_handle(uint8_t hdl_address, uint8_t *hdl_buffer, size_t hdl_b
128128
#include "rtc.h"
129129
volatile bool update_rtc = false; // Set to true to trigger I2C communication with RTC module
130130
volatile bool rtc_time_has_sync = false; // Set to true when the RTC has been set by GPS or Mobile app
131-
struct tm * tmTime;
132-
time_t currentTime;
131+
static struct tm * tmTime;
132+
static time_t currentTime;
133133
static char datetimestring[ 64 ] = { 0 };
134134
volatile bool log_file_active = false;
135135
static volatile bool write_logdata_now = false;
136136
static volatile bool gps_signal_locked = false;
137137
static time_t time_esc_last_responded; // For triggering TX and RX pin swapping
138138
static time_t time_gps_last_responded; // For detecting stale data
139139

140+
void update_time(int syear, int smonth, int sday, int shour, int sminute, int ssecond)
141+
{
142+
tmTime->tm_year = syear - 1900;
143+
tmTime->tm_mon = smonth - 1;
144+
tmTime->tm_mday = sday;
145+
tmTime->tm_hour = shour;
146+
tmTime->tm_min = sminute;
147+
tmTime->tm_sec = ssecond;
148+
currentTime = mktime(tmTime);
149+
}
150+
140151
////////////////////////////////////////
141152
// Fault tracking
142153
////////////////////////////////////////
@@ -168,7 +179,7 @@ bool is_melody_playing = false;
168179
bool is_melody_playing_pause = false;
169180
uint32_t melody_next_note = 0;
170181
int *melody;
171-
182+
int melody_last_alert_index = MELODY_NONE;
172183

173184

174185
uint32_t app_timer_ms(uint32_t ticks)
@@ -2122,7 +2133,7 @@ void log_file_start()
21222133
void update_status_packet(char * buffer)
21232134
{
21242135
// Update the buffer with the a status response packet
2125-
sprintf(buffer, "status,OK,%d,%d,%d,%d,%d,%d,%d", log_file_active, fault_count, recent_fault_index, lfs_percent_free, lfs_file_count, hgps.fix, hgps.sats_in_view);
2136+
sprintf(buffer, "status,OK,%d,%d,%d,%d,%d,%d,%d,%d", log_file_active, fault_count, recent_fault_index, lfs_percent_free, lfs_file_count, hgps.fix, hgps.sats_in_view, melody_last_alert_index);
21262137
}
21272138

21282139
uint16_t create_fault_packet(char * buffer)
@@ -2404,7 +2415,7 @@ int main(void) {
24042415
// Set RTC to trickle charge super capacitor
24052416
rtc_battery_charge();
24062417
// Get the current time from the RTC
2407-
rtc_get_time();
2418+
rtc_get_time(tmTime, &currentTime);
24082419
// Set the last ESC response time to now
24092420
time_esc_last_responded = currentTime;
24102421
// Set the last GPS response time to now
@@ -2465,7 +2476,7 @@ int main(void) {
24652476
advertising_init();
24662477
conn_params_init();
24672478
peer_manager_init();
2468-
command_interface_init(&ble_send_logbuffer, &lfs);
2479+
command_interface_init(&ble_send_logbuffer, &lfs, &update_time);
24692480

24702481
packet_init(uart_send_buffer, process_packet_vesc, PACKET_VESC);
24712482
packet_init(ble_send_buffer, process_packet_ble, PACKET_BLE);

rtc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include <time.h>
77

88
extern const nrf_drv_twi_t m_twi_master;
9-
extern struct tm * tmTime;
10-
extern time_t currentTime;
119

1210
void rtc_battery_charge()
1311
{
@@ -82,7 +80,7 @@ static inline uint8_t bcd_decimal(uint8_t hex)
8280
return dec;
8381
}
8482

85-
void rtc_get_time()
83+
void rtc_get_time(struct tm * tmTime, time_t * currentTime)
8684
{
8785
NRF_LOG_INFO("Requesting time from RTC");
8886
NRF_LOG_FLUSH();
@@ -113,7 +111,7 @@ void rtc_get_time()
113111
tmTime->tm_wday = bcd_decimal( pdata[ 4 ] ) - 1;
114112
tmTime->tm_mon = bcd_decimal( pdata[ 5 ] ) - 1;
115113
tmTime->tm_year = bcd_decimal( pdata[ 6 ] ) + 100; //tm_year starts at 1900, rtc starts at 2000
116-
currentTime = mktime( tmTime );
114+
*currentTime = mktime( tmTime );
117115

118116
char dt_string[64] = {0};
119117
strftime(dt_string, 64, "%Y-%m-%dT%H:%M:%S", tmTime);

rtc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#ifndef RTC_H_
22
#define RTC_H_
33

4+
#include <time.h>
5+
46
#define RTC_ADDRESS 0x56
57
#define BIT(x) 1<<x
68

79
void rtc_battery_charge();
8-
void rtc_set_time( int year, int month, int day, int hour, int minute, int second );
9-
void rtc_get_time();
10+
void rtc_set_time(int year, int month, int day, int hour, int minute, int second);
11+
void rtc_get_time(struct tm * tmTime, time_t * current_time);
1012

1113
#endif //RTC_H_

0 commit comments

Comments
 (0)