|
* usb.c |
|
* |
|
* Created on: 14 июн. 2017 г. |
|
* Author: stavinsky |
|
*/ |
|
|
|
|
|
#include "usb.h" |
|
#include "utils.h" |
|
|
|
|
|
char cmd_buf[256]=""; |
|
|
|
void print_current_time(void){ |
|
char data_buf[250]=""; |
|
uint8_t year = bcd_to_dec(rtc_read_reg(kYearReg)); |
|
uint8_t month = bcd_to_dec(rtc_read_reg(kMonthReg)); |
|
uint8_t date = bcd_to_dec(rtc_read_reg(kDateReg)); |
|
uint8_t hour = bcd_to_dec(rtc_read_reg(kHourReg)); |
|
uint8_t minute = bcd_to_dec(rtc_read_reg(kMinuteReg)); |
|
uint8_t second = bcd_to_dec(rtc_read_reg(kSecondReg)); |
|
sprintf(data_buf, "%u-%02u-%u %u:%u:%u\r\n", year, month, date, hour, minute, second); |
|
serial_print(data_buf); |
|
|
|
} |
|
|
|
void get_n_word(char* str, char* token, int n){ |
|
unsigned int end = 0; |
|
unsigned int start = 0; |
|
for (int i=0; i<=n; i++){ |
|
start = end; |
|
while(end <= strlen(str) && str[end] != ' ' && (int)str[end] != 13){ |
|
end++; |
|
} |
|
end++; |
|
} |
|
strncpy(token, str+start, end); |
|
token[end-1]=0; |
|
} |
|
void got_command(char * str){ |
|
char cmd[256]=""; |
|
get_n_word(str, cmd, 0); |
|
if (strcmp("print time\r", str)==0){ |
|
serial_print("time is:\r\n"); |
|
print_current_time(); |
|
serial_print("\r\n"); |
|
} |
|
int year = 0; |
|
int month = 0; |
|
int date = 0; |
|
int hour = 0; |
|
int min = 0; |
|
int sec = 0; |
|
if (sscanf(str, "set time %u %u %u %u %u %u", &year, &month, &date, &hour, &min, &sec)==6){ |
|
rtc_set_time(year, month, date, hour, min, sec); |
|
serial_print("time was set\r\n"); |
|
serial_print("\r\n"); |
|
} |
|
|
|
|
|
} |
|
void got_chars(char * buf, uint32_t len){ |
|
char tmp[256]=""; |
|
char output[265]=""; |
|
strncpy(tmp, buf, len); |
|
tmp[len]=0; |
|
serial_print(tmp); |
|
if (strlen(cmd_buf) <256-strlen(tmp)) { |
|
strcat(cmd_buf, tmp); |
|
} |
|
if (strlen(cmd_buf)==256) { |
|
serial_print("string is too big\r\n"); |
|
} |
|
if ((int)cmd_buf[strlen(cmd_buf)-1] == 13){ |
|
serial_print("\r\n"); |
|
strcpy(output, cmd_buf); |
|
got_command(output); |
|
cmd_buf[0]=0; |
|
} |
|
|
|
|
|
// serial_print(cmd_buf); |
|
|
|
} |
|
|
|
|
|
|