UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]
Dependencies: mbed SDFileSystem
event.cpp@5:a37e3a15444b, 2019-04-24 (annotated)
- Committer:
- Inscape_ao
- Date:
- Wed Apr 24 02:34:51 2019 +0000
- Revision:
- 5:a37e3a15444b
- Parent:
- 2:a694440145e9
- Child:
- 7:9ab8809f9693
add SDDataStore ; and SD Control commands (SDS, SDE, SDW[for Test])
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Inscape_ao | 2:a694440145e9 | 1 | /** --- Includes --- */ |
Inscape_ao | 2:a694440145e9 | 2 | #include "mbed.h" |
Inscape_ao | 2:a694440145e9 | 3 | #include "TimeManager.h" |
Inscape_ao | 2:a694440145e9 | 4 | #include "UartReceiver.h" |
Inscape_ao | 2:a694440145e9 | 5 | #include "CommandParser.h" |
Inscape_ao | 2:a694440145e9 | 6 | #include "global.h" |
Inscape_ao | 2:a694440145e9 | 7 | #include "string.h" |
Inscape_ao | 2:a694440145e9 | 8 | |
Inscape_ao | 2:a694440145e9 | 9 | static int sampleHanlder(CommandParser *pC, char *arg, int exarg); |
Inscape_ao | 2:a694440145e9 | 10 | static int setTimeHanlder(CommandParser *pC, char *arg, int exarg); |
Inscape_ao | 2:a694440145e9 | 11 | static int getTimeHanlder(CommandParser *pC, char *arg, int exarg); |
Inscape_ao | 5:a37e3a15444b | 12 | static int startSDStore(CommandParser *pC, char *arg, int exarg); |
Inscape_ao | 5:a37e3a15444b | 13 | static int endSDStore(CommandParser *pC, char *arg, int exarg); |
Inscape_ao | 5:a37e3a15444b | 14 | static int writeSDStore(CommandParser *pC, char *arg, int exarg); |
Inscape_ao | 2:a694440145e9 | 15 | |
Inscape_ao | 2:a694440145e9 | 16 | /* Event Hankder */ |
Inscape_ao | 2:a694440145e9 | 17 | CmdParseRule rules[] = { |
Inscape_ao | 2:a694440145e9 | 18 | /* サンプルコマンド */ |
Inscape_ao | 2:a694440145e9 | 19 | {"CMD", sampleHanlder, 0}, |
Inscape_ao | 5:a37e3a15444b | 20 | /* SDカード記録 */ |
Inscape_ao | 5:a37e3a15444b | 21 | {"SDS", startSDStore, 0}, |
Inscape_ao | 5:a37e3a15444b | 22 | {"SDE", endSDStore, 0}, |
Inscape_ao | 5:a37e3a15444b | 23 | {"SDW", writeSDStore, 0}, /* for TEST */ |
Inscape_ao | 2:a694440145e9 | 24 | /* 時刻取得 */ |
Inscape_ao | 2:a694440145e9 | 25 | {"GTS", getTimeHanlder, 0}, |
Inscape_ao | 2:a694440145e9 | 26 | /* 時刻設定 */ |
Inscape_ao | 2:a694440145e9 | 27 | {"TYR", setTimeHanlder, TimeManager::SetTimeMethod::Year}, |
Inscape_ao | 2:a694440145e9 | 28 | {"TMO", setTimeHanlder, TimeManager::SetTimeMethod::Month}, |
Inscape_ao | 2:a694440145e9 | 29 | {"TDA", setTimeHanlder, TimeManager::SetTimeMethod::Day}, |
Inscape_ao | 2:a694440145e9 | 30 | {"THR", setTimeHanlder, TimeManager::SetTimeMethod::Hour}, |
Inscape_ao | 2:a694440145e9 | 31 | {"TMI", setTimeHanlder, TimeManager::SetTimeMethod::Min}, |
Inscape_ao | 2:a694440145e9 | 32 | {"TSE", setTimeHanlder, TimeManager::SetTimeMethod::Sec}, |
Inscape_ao | 2:a694440145e9 | 33 | }; |
Inscape_ao | 2:a694440145e9 | 34 | |
Inscape_ao | 2:a694440145e9 | 35 | int getNumOfRules = sizeof(rules)/sizeof(CmdParseRule); |
Inscape_ao | 2:a694440145e9 | 36 | |
Inscape_ao | 2:a694440145e9 | 37 | |
Inscape_ao | 2:a694440145e9 | 38 | /****************************************************/ |
Inscape_ao | 2:a694440145e9 | 39 | /* Event Handlers */ |
Inscape_ao | 2:a694440145e9 | 40 | /****************************************************/ |
Inscape_ao | 2:a694440145e9 | 41 | /* Sample Command */ |
Inscape_ao | 2:a694440145e9 | 42 | static int sampleHanlder(CommandParser *pC, char *arg, int exarg) |
Inscape_ao | 2:a694440145e9 | 43 | { |
Inscape_ao | 2:a694440145e9 | 44 | pC->reply(); |
Inscape_ao | 2:a694440145e9 | 45 | return 0; |
Inscape_ao | 2:a694440145e9 | 46 | } |
Inscape_ao | 2:a694440145e9 | 47 | |
Inscape_ao | 5:a37e3a15444b | 48 | /****************************************************/ |
Inscape_ao | 5:a37e3a15444b | 49 | /* Event Handlers (SD Control) */ |
Inscape_ao | 5:a37e3a15444b | 50 | /****************************************************/ |
Inscape_ao | 5:a37e3a15444b | 51 | static int startSDStore(CommandParser *pC, char *arg, int exarg) |
Inscape_ao | 5:a37e3a15444b | 52 | { |
Inscape_ao | 5:a37e3a15444b | 53 | if (pSds->startFileWithTimeStamp("","txt") != true) { |
Inscape_ao | 5:a37e3a15444b | 54 | pC->reply(false, 1); |
Inscape_ao | 5:a37e3a15444b | 55 | return 1; |
Inscape_ao | 5:a37e3a15444b | 56 | } |
Inscape_ao | 5:a37e3a15444b | 57 | pC->reply(); |
Inscape_ao | 5:a37e3a15444b | 58 | return 0; |
Inscape_ao | 5:a37e3a15444b | 59 | } |
Inscape_ao | 5:a37e3a15444b | 60 | |
Inscape_ao | 5:a37e3a15444b | 61 | static int endSDStore(CommandParser *pC, char *arg, int exarg) |
Inscape_ao | 5:a37e3a15444b | 62 | { |
Inscape_ao | 5:a37e3a15444b | 63 | FILE *fp; |
Inscape_ao | 5:a37e3a15444b | 64 | if ((fp = pSds->getFilePointer()) == NULL) { |
Inscape_ao | 5:a37e3a15444b | 65 | /* NOP */ |
Inscape_ao | 5:a37e3a15444b | 66 | pC->reply(false, 2); |
Inscape_ao | 5:a37e3a15444b | 67 | return 2; |
Inscape_ao | 5:a37e3a15444b | 68 | } |
Inscape_ao | 5:a37e3a15444b | 69 | fprintf(fp, "call endSDStore(%s)\n", pSds->getFileName()); |
Inscape_ao | 5:a37e3a15444b | 70 | pSds->syncFile(); |
Inscape_ao | 5:a37e3a15444b | 71 | pSds->closeFile(); |
Inscape_ao | 5:a37e3a15444b | 72 | pC->reply(); |
Inscape_ao | 5:a37e3a15444b | 73 | return 0; |
Inscape_ao | 5:a37e3a15444b | 74 | } |
Inscape_ao | 5:a37e3a15444b | 75 | |
Inscape_ao | 5:a37e3a15444b | 76 | static int writeSDStore(CommandParser *pC, char *arg, int exarg) |
Inscape_ao | 5:a37e3a15444b | 77 | { |
Inscape_ao | 5:a37e3a15444b | 78 | FILE *fp; |
Inscape_ao | 5:a37e3a15444b | 79 | char curr[TimeManager::TimeStampLength + 1] = {0}; |
Inscape_ao | 5:a37e3a15444b | 80 | |
Inscape_ao | 5:a37e3a15444b | 81 | if ((fp = pSds->getFilePointer()) == NULL) { |
Inscape_ao | 5:a37e3a15444b | 82 | /* NOP */ |
Inscape_ao | 5:a37e3a15444b | 83 | pC->reply(false, 2); |
Inscape_ao | 5:a37e3a15444b | 84 | return 2; |
Inscape_ao | 5:a37e3a15444b | 85 | } |
Inscape_ao | 5:a37e3a15444b | 86 | pTM->getTimeStamp(curr); |
Inscape_ao | 5:a37e3a15444b | 87 | fprintf(fp, "call writeSDStore at %s\n", curr); |
Inscape_ao | 5:a37e3a15444b | 88 | pSds->syncFile(); |
Inscape_ao | 5:a37e3a15444b | 89 | pC->reply(); |
Inscape_ao | 5:a37e3a15444b | 90 | return 0; |
Inscape_ao | 5:a37e3a15444b | 91 | } |
Inscape_ao | 5:a37e3a15444b | 92 | |
Inscape_ao | 5:a37e3a15444b | 93 | /****************************************************/ |
Inscape_ao | 5:a37e3a15444b | 94 | /* Event Handlers (Time Control) */ |
Inscape_ao | 5:a37e3a15444b | 95 | /****************************************************/ |
Inscape_ao | 2:a694440145e9 | 96 | /* Time Set Command */ |
Inscape_ao | 2:a694440145e9 | 97 | static int setTimeHanlder(CommandParser *pC, char *arg, int exarg) |
Inscape_ao | 2:a694440145e9 | 98 | { |
Inscape_ao | 2:a694440145e9 | 99 | bool success = false; |
Inscape_ao | 2:a694440145e9 | 100 | int setvalue = atoi(arg); |
Inscape_ao | 2:a694440145e9 | 101 | |
Inscape_ao | 2:a694440145e9 | 102 | switch (exarg) { |
Inscape_ao | 2:a694440145e9 | 103 | case TimeManager::SetTimeMethod::Year: |
Inscape_ao | 2:a694440145e9 | 104 | case TimeManager::SetTimeMethod::Month: |
Inscape_ao | 2:a694440145e9 | 105 | case TimeManager::SetTimeMethod::Day: |
Inscape_ao | 2:a694440145e9 | 106 | case TimeManager::SetTimeMethod::Hour: |
Inscape_ao | 2:a694440145e9 | 107 | case TimeManager::SetTimeMethod::Min: |
Inscape_ao | 2:a694440145e9 | 108 | case TimeManager::SetTimeMethod::Sec: |
Inscape_ao | 2:a694440145e9 | 109 | success = pTM->setCurrentTime(exarg,setvalue); |
Inscape_ao | 2:a694440145e9 | 110 | break; |
Inscape_ao | 2:a694440145e9 | 111 | } |
Inscape_ao | 2:a694440145e9 | 112 | pC->reply(success, (success)? 0 : setvalue ); |
Inscape_ao | 2:a694440145e9 | 113 | return 0; |
Inscape_ao | 2:a694440145e9 | 114 | } |
Inscape_ao | 2:a694440145e9 | 115 | |
Inscape_ao | 2:a694440145e9 | 116 | /* Time Get Command */ |
Inscape_ao | 2:a694440145e9 | 117 | static int getTimeHanlder(CommandParser *pC, char *arg, int exarg) |
Inscape_ao | 2:a694440145e9 | 118 | { |
Inscape_ao | 2:a694440145e9 | 119 | int len; |
Inscape_ao | 2:a694440145e9 | 120 | char timestamp[TimeManager::TimeStampLength + 1] = {0}; |
Inscape_ao | 2:a694440145e9 | 121 | Serial *pUart = pC->getCurrentUart(); |
Inscape_ao | 2:a694440145e9 | 122 | len = pTM->getTimeStamp(timestamp); |
Inscape_ao | 2:a694440145e9 | 123 | pUart->printf(":%d RTS %04d %s\n", pC->getDeviceID(), len, timestamp); |
Inscape_ao | 2:a694440145e9 | 124 | return 0; |
Inscape_ao | 2:a694440145e9 | 125 | } |