add got_chars((char *)Buf, Len); to int8_t CDC_Receive_FS (uint8_t Buf, uint32_t *Len)
and add usb_out(); to main loop
* 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); | |
} | |
/* | |
* usb.h | |
* | |
* Created on: 8 июн. 2017 г. | |
* Author: stavinsky | |
*/ | |
#ifndef USB_H_ | |
#define USB_H_ | |
#include <string.h> | |
#include <stddef.h> | |
#include "usbd_cdc_if.h" | |
#include "rtc.h" | |
void got_chars(char * buf, uint32_t len); | |
#endif /* USB_H_ */ |
/* | |
* utils.c | |
* | |
* Created on: 14 июн. 2017 г. | |
* Author: stavinsky | |
*/ | |
#include "utils.h" | |
char USB_OUT_BUFFER[1024]=""; | |
void serial_print(char * data){ | |
if(strlen(USB_OUT_BUFFER)<= 1024-strlen(data)) | |
{ | |
strcat(USB_OUT_BUFFER, data); | |
} | |
} | |
void usb_out(void ){ | |
char data_to_out[64]=""; | |
if ( strlen(USB_OUT_BUFFER)>0){ | |
strncpy(data_to_out, USB_OUT_BUFFER, 64); | |
str_cut(USB_OUT_BUFFER, 0, 64); | |
CDC_Transmit_FS((uint8_t*)data_to_out, strlen(data_to_out)); | |
} | |
} | |
int str_cut(char *str, int begin, int len) | |
{ | |
int l = strlen(str); | |
if (len < 0) len = l - begin; | |
if (begin + len > l) len = l - begin; | |
memmove(str + begin, str + begin + len, l - len + 1); | |
return len; | |
} |
#ifndef UTILS_H_ | |
#define UTILS_H_ | |
#include "usbd_cdc_if.h" | |
extern char USB_OUT_BUFFER[1024]; | |
int str_cut(char *str, int begin, int len); | |
void serial_print(char * data); | |
#endif /* UTILS_H_ */ |