Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/OutputTask/OutputTask.cpp@70:7427f4959201, 2016-09-16 (annotated)
- Committer:
- jmarkel44
- Date:
- Fri Sep 16 17:54:51 2016 +0000
- Revision:
- 70:7427f4959201
- Parent:
- 67:49f266601d83
- Child:
- 71:34856d21f2bf
increased outputtask size;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 70:7427f4959201 | 1 | /****************************************************************************** |
jmarkel44 | 70:7427f4959201 | 2 | * |
jmarkel44 | 70:7427f4959201 | 3 | * File: OutputTask.cpp |
jmarkel44 | 70:7427f4959201 | 4 | * Desciption: source for the ICE Output task |
jmarkel44 | 70:7427f4959201 | 5 | * |
jmarkel44 | 70:7427f4959201 | 6 | *****************************************************************************/ |
jmarkel44 | 67:49f266601d83 | 7 | #include "OutputTask.h" |
jmarkel44 | 48:1c7861d80d16 | 8 | #include "global.h" |
jmarkel44 | 66:db1425574b58 | 9 | #include "MbedJSONValue.h" |
jmarkel44 | 70:7427f4959201 | 10 | #include <vector> |
jmarkel44 | 48:1c7861d80d16 | 11 | |
jmarkel44 | 70:7427f4959201 | 12 | // locals |
jmarkel44 | 67:49f266601d83 | 13 | static int createOutput(const char *controlFile); |
jmarkel44 | 66:db1425574b58 | 14 | static void loadPersistentOutputs(void); |
jmarkel44 | 63:0ded43237b22 | 15 | |
jmarkel44 | 70:7427f4959201 | 16 | class Control |
jmarkel44 | 70:7427f4959201 | 17 | { |
jmarkel44 | 70:7427f4959201 | 18 | private: |
jmarkel44 | 70:7427f4959201 | 19 | string id; |
jmarkel44 | 70:7427f4959201 | 20 | unsigned int priority; |
jmarkel44 | 70:7427f4959201 | 21 | typedef enum { CONTROL_OFF = 0, CONTROL_ON = 1 } State; |
jmarkel44 | 70:7427f4959201 | 22 | State state; |
jmarkel44 | 70:7427f4959201 | 23 | public: |
jmarkel44 | 70:7427f4959201 | 24 | Control(string id, unsigned int priority, State state) : |
jmarkel44 | 70:7427f4959201 | 25 | id(id), priority(priority), state(state) {} |
jmarkel44 | 70:7427f4959201 | 26 | }; |
jmarkel44 | 70:7427f4959201 | 27 | |
jmarkel44 | 70:7427f4959201 | 28 | vector<Control> controlStack; |
jmarkel44 | 70:7427f4959201 | 29 | |
jmarkel44 | 66:db1425574b58 | 30 | typedef std::map<string, string> StringOutputVector_t; |
jmarkel44 | 66:db1425574b58 | 31 | |
jmarkel44 | 66:db1425574b58 | 32 | StringOutputVector_t outputMap; |
jmarkel44 | 63:0ded43237b22 | 33 | |
jmarkel44 | 70:7427f4959201 | 34 | /***************************************************************************** |
jmarkel44 | 70:7427f4959201 | 35 | * Function: OutputTask |
jmarkel44 | 70:7427f4959201 | 36 | * Description: Main entry point for the Output Task |
jmarkel44 | 70:7427f4959201 | 37 | * |
jmarkel44 | 70:7427f4959201 | 38 | * @param args -> not used |
jmarkel44 | 70:7427f4959201 | 39 | * @return none |
jmarkel44 | 70:7427f4959201 | 40 | *****************************************************************************/ |
jmarkel44 | 48:1c7861d80d16 | 41 | void OutputTask(void const *args) |
jmarkel44 | 48:1c7861d80d16 | 42 | { |
jmarkel44 | 51:66b820f203a5 | 43 | UNUSED(args); |
jmarkel44 | 70:7427f4959201 | 44 | |
jmarkel44 | 67:49f266601d83 | 45 | printf("\r%s has started...\n", __func__); |
jmarkel44 | 63:0ded43237b22 | 46 | |
jmarkel44 | 66:db1425574b58 | 47 | loadPersistentOutputs(); |
jmarkel44 | 66:db1425574b58 | 48 | |
jmarkel44 | 48:1c7861d80d16 | 49 | while (true) { |
jmarkel44 | 51:66b820f203a5 | 50 | // wait for an event |
jmarkel44 | 70:7427f4959201 | 51 | printf("\r%s is waiting for an event\n", __func__); |
jmarkel44 | 56:225786c56315 | 52 | osEvent evt = OutputMasterMailBox.get(); |
jmarkel44 | 51:66b820f203a5 | 53 | if (evt.status == osEventMail) { |
jmarkel44 | 63:0ded43237b22 | 54 | OutputControlMsg_t *msg = (OutputControlMsg_t*) evt.value.p; |
jmarkel44 | 56:225786c56315 | 55 | printf("\r%s received message from someone...\n", __func__); |
jmarkel44 | 63:0ded43237b22 | 56 | printf("\rmsg->relay = %s\n", msg->output.c_str()); |
jmarkel44 | 56:225786c56315 | 57 | printf("\rmsg->state = %s\n", msg->state == ON ? "ON" : "OFF"); |
jmarkel44 | 56:225786c56315 | 58 | printf("\rmsg->priority = %u\n", msg->priority); |
jmarkel44 | 70:7427f4959201 | 59 | |
jmarkel44 | 70:7427f4959201 | 60 | Thread::wait(2000); |
jmarkel44 | 63:0ded43237b22 | 61 | |
jmarkel44 | 63:0ded43237b22 | 62 | switch ( msg->action ) { |
jmarkel44 | 63:0ded43237b22 | 63 | case ACTION_NEW: |
jmarkel44 | 63:0ded43237b22 | 64 | // read the file and and create an output entry |
jmarkel44 | 66:db1425574b58 | 65 | (void) createOutput(msg->controlFile); |
jmarkel44 | 63:0ded43237b22 | 66 | break; |
jmarkel44 | 66:db1425574b58 | 67 | case ACTION_CONTROL_REQ: |
jmarkel44 | 63:0ded43237b22 | 68 | default: |
jmarkel44 | 63:0ded43237b22 | 69 | break; |
jmarkel44 | 63:0ded43237b22 | 70 | } |
jmarkel44 | 63:0ded43237b22 | 71 | |
jmarkel44 | 56:225786c56315 | 72 | // free the message |
jmarkel44 | 56:225786c56315 | 73 | OutputMasterMailBox.free(msg); |
jmarkel44 | 51:66b820f203a5 | 74 | } |
jmarkel44 | 48:1c7861d80d16 | 75 | } |
jmarkel44 | 66:db1425574b58 | 76 | } |
jmarkel44 | 66:db1425574b58 | 77 | |
jmarkel44 | 70:7427f4959201 | 78 | /***************************************************************************** |
jmarkel44 | 70:7427f4959201 | 79 | * Function: DisplayOutputs |
jmarkel44 | 70:7427f4959201 | 80 | * Description: Display a list of outputs and its controls |
jmarkel44 | 70:7427f4959201 | 81 | * |
jmarkel44 | 70:7427f4959201 | 82 | * @param args -> not used |
jmarkel44 | 70:7427f4959201 | 83 | * @return none |
jmarkel44 | 70:7427f4959201 | 84 | *****************************************************************************/ |
jmarkel44 | 66:db1425574b58 | 85 | void DisplayOutputs(void) |
jmarkel44 | 66:db1425574b58 | 86 | { |
jmarkel44 | 66:db1425574b58 | 87 | StringOutputVector_t::iterator pos; |
jmarkel44 | 66:db1425574b58 | 88 | |
jmarkel44 | 66:db1425574b58 | 89 | for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) { |
jmarkel44 | 66:db1425574b58 | 90 | printf("\r [%s] [ %s]\n", pos->first.c_str(), pos->second.c_str()); |
jmarkel44 | 66:db1425574b58 | 91 | } |
jmarkel44 | 66:db1425574b58 | 92 | printf("\r\n"); |
jmarkel44 | 66:db1425574b58 | 93 | } |
jmarkel44 | 66:db1425574b58 | 94 | |
jmarkel44 | 66:db1425574b58 | 95 | static int createOutput(const char *controlFile) |
jmarkel44 | 66:db1425574b58 | 96 | { |
jmarkel44 | 66:db1425574b58 | 97 | char dataBuf[1024]; |
jmarkel44 | 67:49f266601d83 | 98 | int status = GLOBAL_mdot->readUserFile(controlFile, (void *)dataBuf, sizeof(dataBuf)); |
jmarkel44 | 67:49f266601d83 | 99 | if ( status != true ) { |
jmarkel44 | 66:db1425574b58 | 100 | logError("%s failed to read %s", __func__, controlFile); |
jmarkel44 | 66:db1425574b58 | 101 | return -1; |
jmarkel44 | 66:db1425574b58 | 102 | } |
jmarkel44 | 66:db1425574b58 | 103 | |
jmarkel44 | 66:db1425574b58 | 104 | MbedJSONValue json_value; |
jmarkel44 | 66:db1425574b58 | 105 | parse(json_value, dataBuf); |
jmarkel44 | 66:db1425574b58 | 106 | |
jmarkel44 | 66:db1425574b58 | 107 | // extract the relay information |
jmarkel44 | 66:db1425574b58 | 108 | string id = json_value["id"].get<string>(); |
jmarkel44 | 66:db1425574b58 | 109 | |
jmarkel44 | 66:db1425574b58 | 110 | // maps shouldnt' allow duplicates |
jmarkel44 | 66:db1425574b58 | 111 | outputMap[id] = "Null for now"; |
jmarkel44 | 66:db1425574b58 | 112 | |
jmarkel44 | 66:db1425574b58 | 113 | return 0; |
jmarkel44 | 66:db1425574b58 | 114 | } |
jmarkel44 | 66:db1425574b58 | 115 | |
jmarkel44 | 66:db1425574b58 | 116 | static void loadPersistentOutputs(void) |
jmarkel44 | 66:db1425574b58 | 117 | { |
jmarkel44 | 66:db1425574b58 | 118 | bool status; |
jmarkel44 | 66:db1425574b58 | 119 | MbedJSONValue json_value; |
jmarkel44 | 70:7427f4959201 | 120 | |
jmarkel44 | 70:7427f4959201 | 121 | printf("\rLoading persistent controls\n"); |
jmarkel44 | 66:db1425574b58 | 122 | |
jmarkel44 | 66:db1425574b58 | 123 | std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles(); |
jmarkel44 | 70:7427f4959201 | 124 | |
jmarkel44 | 66:db1425574b58 | 125 | for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) { |
jmarkel44 | 67:49f266601d83 | 126 | if( strncmp( i->name, OUTPUT_STR, strlen(OUTPUT_STR)) == 0 ) { |
jmarkel44 | 66:db1425574b58 | 127 | |
jmarkel44 | 66:db1425574b58 | 128 | logInfo("%s: FOUND OUTPUT FILE: %s", __func__, i->name); |
jmarkel44 | 66:db1425574b58 | 129 | |
jmarkel44 | 66:db1425574b58 | 130 | char scratchBuf[1024]; |
jmarkel44 | 66:db1425574b58 | 131 | |
jmarkel44 | 66:db1425574b58 | 132 | status = GLOBAL_mdot->readUserFile(i->name, scratchBuf, 1024); |
jmarkel44 | 66:db1425574b58 | 133 | if( status != true ) { |
jmarkel44 | 66:db1425574b58 | 134 | logInfo("(%d)read file failed, status=%d", __LINE__, status); |
jmarkel44 | 66:db1425574b58 | 135 | } else { |
jmarkel44 | 66:db1425574b58 | 136 | logInfo("(%d)Read File SUCCESS: %s", __LINE__, scratchBuf ); |
jmarkel44 | 66:db1425574b58 | 137 | } |
jmarkel44 | 66:db1425574b58 | 138 | |
jmarkel44 | 66:db1425574b58 | 139 | parse( json_value, scratchBuf ); |
jmarkel44 | 66:db1425574b58 | 140 | |
jmarkel44 | 66:db1425574b58 | 141 | string id = json_value["id"].get<string>(); |
jmarkel44 | 70:7427f4959201 | 142 | printf("\r %s id = %s loaded\n", __func__, id.c_str()); |
jmarkel44 | 66:db1425574b58 | 143 | outputMap[id] = "Null for now"; |
jmarkel44 | 66:db1425574b58 | 144 | } |
jmarkel44 | 66:db1425574b58 | 145 | } |
jmarkel44 | 70:7427f4959201 | 146 | } |