Skip to content

Commit 5fc071b

Browse files
authored
Merge pull request #5 from FreeSK8/0.10.0
0.10.0
2 parents d6fb02b + d37e8ea commit 5fc071b

File tree

2 files changed

+108
-45
lines changed

2 files changed

+108
-45
lines changed

command_interface.c

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extern volatile bool update_rtc;
2828

2929
extern volatile bool log_file_active;
3030

31+
static bool cat_in_progress = false;
3132
static lfs_file_t file_command_interface;
3233
static uint8_t lfs_file_buf[4096]; // Must be cache size
3334
static struct lfs_file_config lfs_file_config;
@@ -77,23 +78,39 @@ void command_interface_process_byte(char incoming)
7778
{
7879
NRF_LOG_INFO("ACK handler >%s<", command_input_buffer);
7980
NRF_LOG_FLUSH();
80-
command_interface_continue_transfer( command_input_buffer );
81+
if (sync_in_progress)
82+
{
83+
command_interface_continue_transfer( command_input_buffer );
84+
}
85+
else
86+
{
87+
NRF_LOG_WARNING("ACK received while !sync_in_progress");
88+
NRF_LOG_FLUSH();
89+
}
8190
}
8291
else if( strncmp(&command_input_buffer[strlen(command_input_buffer)-4], "nack", 4) == 0)
8392
{
8493
NRF_LOG_INFO("NACK handler >%s<", command_input_buffer);
8594
NRF_LOG_FLUSH();
8695
if (sync_in_progress)
8796
{
88-
// For a cat command we want to seek to the byte the client last received
89-
if(strncmp(command_input_buffer, "cat", 3) == 0)
97+
if (cat_in_progress)
9098
{
91-
// Set the file position to what we've received on the client side
92-
lfs_file_seek(m_lfs, &file_command_interface, atoi(command_input_buffer+4), LFS_SEEK_SET);
99+
// For a cat command we want to seek to the byte the client last received
100+
if(strncmp(command_input_buffer, "cat", 3) == 0)
101+
{
102+
// Set the file position to what we've received on the client side
103+
lfs_file_seek(m_lfs, &file_command_interface, atoi(command_input_buffer+4), LFS_SEEK_SET);
104+
}
93105
}
94106
// Continue to send data to the client
95107
command_interface_continue_transfer(command_input_buffer);
96108
}
109+
else
110+
{
111+
NRF_LOG_WARNING("NACK received while !sync_in_progress");
112+
NRF_LOG_FLUSH();
113+
}
97114
}
98115
else if(strncmp(command_input_buffer, "log", 3) == 0)
99116
{
@@ -184,12 +201,17 @@ void command_interface_process_byte(char incoming)
184201

185202
NRF_LOG_INFO("filename %s", filename);
186203
NRF_LOG_FLUSH();
204+
if (cat_in_progress)
205+
{
206+
lfs_file_close(m_lfs, &file_command_interface);
207+
}
187208
if(lfs_file_opencfg(m_lfs, &file_command_interface, filename, LFS_O_RDONLY, &lfs_file_config) >= 0)
188209
{
210+
lfs_file_seek(m_lfs, &file_command_interface, 0, LFS_SEEK_SET); //TODO: Testing rewind on open, necessary?
189211
sprintf((char *)command_response_buffer, "cat,%s", filename);
190212
m_ble_tx_logbuffer(command_response_buffer, (size_t)strlen((const char *)command_response_buffer));
191213
bytes_sent = 0;
192-
sync_in_progress = true;
214+
cat_in_progress = true;
193215
}
194216
}
195217
else if(strncmp(command_input_buffer, "rm ", 3) == 0 && strlen(command_input_buffer) > 3)
@@ -241,7 +263,7 @@ void command_interface_process_byte(char incoming)
241263
}
242264
else if(strncmp(command_input_buffer, "version", 7) == 0)
243265
{
244-
sprintf((char *)command_response_buffer, "version,0.9.1,beta");
266+
sprintf((char *)command_response_buffer, "version,0.10.0,beta");
245267
m_ble_tx_logbuffer(command_response_buffer, strlen((const char *)command_response_buffer));
246268
}
247269
else if(strncmp(command_input_buffer, "getcfg", 6) == 0)
@@ -346,21 +368,40 @@ void command_interface_process_byte(char incoming)
346368

347369
m_ble_tx_logbuffer(command_response_buffer, strlen((const char *)command_response_buffer));
348370
}
371+
else if(strncmp(command_input_buffer, "syncstart", 9) == 0)
372+
{
373+
// Sync process is starting
374+
NRF_LOG_INFO("syncstart command received");
375+
NRF_LOG_FLUSH();
376+
if (!sync_in_progress)
377+
{
378+
sync_in_progress = true;
379+
NRF_LOG_INFO("sync_in_progress is now true");
380+
NRF_LOG_FLUSH();
381+
} else {
382+
NRF_LOG_INFO("sync_in_progress was already true");
383+
NRF_LOG_FLUSH();
384+
}
385+
}
349386
else if(strncmp(command_input_buffer, "syncstop", 8) == 0)
350387
{
351388
// Sync process was aborted by the user
352389
NRF_LOG_INFO("syncstop command received");
353390
NRF_LOG_FLUSH();
354391
if (sync_in_progress)
355392
{
356-
int close_result = lfs_file_close(m_lfs, &file_command_interface);
357-
NRF_LOG_INFO("lfs close result: %d", close_result);
358-
NRF_LOG_FLUSH();
359393
sync_in_progress = false;
360394
} else {
361395
NRF_LOG_INFO("sync_in_progress was false");
362396
NRF_LOG_FLUSH();
363397
}
398+
if (cat_in_progress)
399+
{
400+
int close_result = lfs_file_close(m_lfs, &file_command_interface);
401+
NRF_LOG_INFO("lfs close result: %d", close_result);
402+
NRF_LOG_FLUSH();
403+
cat_in_progress = false;
404+
}
364405
}
365406
else if(strncmp(command_input_buffer, "snooze,", 7) == 0)
366407
{
@@ -374,7 +415,7 @@ void command_interface_process_byte(char incoming)
374415
// Return robogotchi unique ID
375416
NRF_LOG_INFO("robogotchi ID command received");
376417
NRF_LOG_FLUSH();
377-
sprintf((char *)command_response_buffer, "roboid,%02x%02x", NRF_FICR->DEVICEID[0], NRF_FICR->DEVICEID[1]);
418+
sprintf((char *)command_response_buffer, "roboid,%02lx%02lx", NRF_FICR->DEVICEID[0], NRF_FICR->DEVICEID[1]);
378419
m_ble_tx_logbuffer(command_response_buffer, strlen((const char *)command_response_buffer));
379420
}
380421

@@ -424,22 +465,30 @@ void command_interface_continue_transfer(char* command)
424465
}
425466
else if(strncmp(command_input_buffer, "cat", 3) == 0)
426467
{
468+
if (!cat_in_progress)
469+
{
470+
NRF_LOG_WARNING("CAT ACK/NACK received while !cat_in_progress");
471+
NRF_LOG_FLUSH();
472+
return;
473+
}
427474
NRF_LOG_INFO("command_interface_continue_transfer(TRANSFER_MODE_CAT)");
428475
NRF_LOG_FLUSH();
429476

430477
if(bytes_sent >= file_command_interface.ctz.size)
431478
{
432479
m_ble_tx_logbuffer((unsigned char *)"cat,complete", strlen("cat,complete"));
433480

434-
NRF_LOG_INFO("finished cat");
481+
NRF_LOG_INFO("sending cat,complete");
435482
NRF_LOG_FLUSH();
436483

484+
//NOTE: Leaving file open in case the client needs to ACK/NACK
485+
//NOTE: File will be closed upon request of the next file or syncstop
486+
/*
437487
int close_result = lfs_file_close(m_lfs, &file_command_interface);
438-
439488
NRF_LOG_INFO("cat close result: %d", close_result);
440489
NRF_LOG_FLUSH();
441-
442-
sync_in_progress = false;
490+
cat_in_progress = false;
491+
*/
443492
}
444493
else if(bytes_sent < file_command_interface.ctz.size)
445494
{

main.c

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ void i2c_oled_comm_handle(uint8_t hdl_address, uint8_t *hdl_buffer, size_t hdl_b
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
131131
static struct tm * tmTime;
132-
static time_t currentTime;
133-
static time_t lastSyncTime;
132+
struct tm gpsTime; // Time received from GPS
133+
static time_t currentTime; // Current time of the Robogotchi
134+
static time_t timeSyncTime; // New time from GPS to be set during timeSync event
135+
static time_t fileSystemSyncTime; // The last time the filesystem performed a sync to QSPI
134136
static char datetimestring[ 64 ] = { 0 };
135137
volatile bool log_file_active = false;
136138
static volatile bool write_logdata_now = false;
@@ -150,6 +152,9 @@ void update_time(int syear, int smonth, int sday, int shour, int sminute, int ss
150152
tmTime->tm_min = sminute;
151153
tmTime->tm_sec = ssecond;
152154
currentTime = mktime(tmTime);
155+
156+
time_esc_last_responded = currentTime;
157+
time_gps_last_responded = currentTime;
153158
}
154159

155160
////////////////////////////////////////
@@ -1622,9 +1627,9 @@ void process_packet_vesc(unsigned char *data, unsigned int len) {
16221627
}
16231628

16241629
// Sync filesystem contents every 60 seconds
1625-
if (log_file_active && currentTime % 60 == 0 && currentTime != lastSyncTime)
1630+
if (log_file_active && currentTime % 60 == 0 && currentTime != fileSystemSyncTime)
16261631
{
1627-
lastSyncTime = currentTime;
1632+
fileSystemSyncTime = currentTime;
16281633
int sync_result = lfs_file_sync(&lfs, &file);
16291634
NRF_LOG_INFO("Sync result: %d", sync_result);
16301635
NRF_LOG_FLUSH();
@@ -1696,6 +1701,27 @@ static void packet_timer_handler(void *p_context) {
16961701
//TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_EXIT();
16971702
}
16981703

1704+
static void finalize_time_sync()
1705+
{
1706+
// Update time in memory
1707+
memcpy(tmTime, &gpsTime, sizeof(struct tm));
1708+
currentTime = timeSyncTime;
1709+
1710+
// Update time on RTC
1711+
update_rtc = true;
1712+
1713+
// Update internal state
1714+
log_message_gps.dt = 0; // Ensure non-delta is written next
1715+
log_message_esc.dt = 0; // Ensure non-delta is written next
1716+
time_esc_last_responded = currentTime;
1717+
time_gps_last_responded = currentTime;
1718+
lastTimeBoardMoved = currentTime;
1719+
strftime(datetimestring, 64, "%Y-%m-%dT%H:%M:%S", tmTime);
1720+
1721+
NRF_LOG_INFO("Time set from GPS %ld", currentTime);
1722+
NRF_LOG_FLUSH();
1723+
}
1724+
16991725
static void logging_timer_handler(void *p_context) {
17001726
(void)p_context;
17011727
static char gps_status[11] = {0};
@@ -1788,47 +1814,27 @@ static void logging_timer_handler(void *p_context) {
17881814
//NRF_LOG_FLUSH();
17891815

17901816
// Convert the time from the GPS
1791-
time_t newTimeSeconds;
1792-
struct tm gpsTime;
17931817
gpsTime.tm_year = 2000 + hgps.year - 1900;
17941818
gpsTime.tm_mon = hgps.month - 1;
17951819
gpsTime.tm_mday = hgps.date;
17961820
gpsTime.tm_hour = hgps.hours;
17971821
gpsTime.tm_min = hgps.minutes;
17981822
gpsTime.tm_sec = hgps.seconds;
17991823
// Give it to me in time_t
1800-
newTimeSeconds = mktime(&gpsTime);
1801-
1802-
//strftime(datetimestring, 64, "%Y-%m-%dT%H:%M:%S", tmTime);
1803-
//NRF_LOG_INFO("Setting time from GPS; time now %s or %ld", datetimestring, newTimeSeconds);
1804-
//NRF_LOG_FLUSH();
1824+
timeSyncTime = mktime(&gpsTime);
18051825

18061826
// Store time sync difference in seconds if we are currently logging
18071827
if (log_file_active)
18081828
{
18091829
log_message_freesk8.event_type = TIME_SYNC;
1810-
log_message_freesk8.event_data = newTimeSeconds - currentTime;
1830+
log_message_freesk8.event_data = timeSyncTime - currentTime;
18111831

18121832
write_time_sync_now = true;
18131833
}
1814-
1815-
// Update time in memory
1816-
memcpy(tmTime, &gpsTime, sizeof(struct tm));
1817-
currentTime = newTimeSeconds;
1818-
1819-
// Update time on RTC
1820-
update_rtc = true;
1821-
1822-
// Update internal state
1823-
log_message_gps.dt = 0; // Ensure non-delta is written next
1824-
log_message_esc.dt = 0; // Ensure non-delta is written next
1825-
time_esc_last_responded = currentTime;
1826-
time_gps_last_responded = currentTime;
1827-
lastTimeBoardMoved = currentTime;
1828-
strftime(datetimestring, 64, "%Y-%m-%dT%H:%M:%S", tmTime);
1829-
1830-
NRF_LOG_INFO("Time set from GPS %ld", currentTime);
1831-
NRF_LOG_FLUSH();
1834+
else
1835+
{
1836+
finalize_time_sync();
1837+
}
18321838
}
18331839

18341840
// Check if the ESC has not responded in 1 second and try swapping the UART pin configuration
@@ -2548,19 +2554,23 @@ int main(void) {
25482554
// Monitor button press; Do not block for more than 25ms
25492555
process_user_input();
25502556

2557+
// Reset UART and packet on error
2558+
//TODO: m_uart_error is never set to true
25512559
if (m_uart_error) {
25522560
app_uart_close();
25532561
uart_init();
25542562
packet_reset(PACKET_VESC);
25552563
m_uart_error = false;
25562564
}
25572565

2566+
// Read ESC data; Potentially writing log data
25582567
uint8_t byte;
25592568
while (app_uart_get(&byte) == NRF_SUCCESS) {
25602569
time_esc_last_responded = currentTime;
25612570
packet_process_byte(byte, PACKET_VESC);
25622571
}
25632572

2573+
// Check log file writing flags from 1Hz timer
25642574
if (write_gps_now)
25652575
{
25662576
write_gps_now = false;
@@ -2599,13 +2609,17 @@ int main(void) {
25992609
bytes_written += lfs_file_write(&lfs, &file, &end, sizeof(end));
26002610
NRF_LOG_INFO("TIME_SYNC Bytes Written: %ld", bytes_written);
26012611
NRF_LOG_FLUSH();
2612+
2613+
finalize_time_sync();
26022614
}
26032615

2616+
// Update GPS data
26042617
while (gps_uart_get(&byte) == NRF_SUCCESS) {
26052618
time_gps_last_responded = currentTime;
26062619
lwgps_process(&hgps, &byte, sizeof(byte));
26072620
}
26082621

2622+
// Update RTC
26092623
if (update_rtc) {
26102624
update_rtc = false;
26112625
rtc_set_time( tmTime->tm_year + 1900, tmTime->tm_mon + 1, tmTime->tm_mday, tmTime->tm_hour, tmTime->tm_min, tmTime->tm_sec );

0 commit comments

Comments
 (0)