![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
work1
Dependencies: mbed MAX44009 mbed-os Si7021
app/AppCommand.cpp@1:3656b45f17a8, 2020-05-27 (annotated)
- Committer:
- danaeb
- Date:
- Wed May 27 12:34:37 2020 +0000
- Revision:
- 1:3656b45f17a8
- Parent:
- AppCommand.cpp@0:d3e390d62607
app_create
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
danaeb | 0:d3e390d62607 | 1 | #include "AppCommand.h" |
danaeb | 0:d3e390d62607 | 2 | #include "mbed_trace.h" |
danaeb | 0:d3e390d62607 | 3 | |
danaeb | 0:d3e390d62607 | 4 | #include "AppMain.h" |
danaeb | 0:d3e390d62607 | 5 | #include "CommandParser.h" |
danaeb | 0:d3e390d62607 | 6 | #include "SensorManager.h" |
danaeb | 0:d3e390d62607 | 7 | #include "Calendar.h" |
danaeb | 0:d3e390d62607 | 8 | #include "Sensor.h" |
danaeb | 0:d3e390d62607 | 9 | #include "rtc.h" |
danaeb | 0:d3e390d62607 | 10 | #include "tools.h" |
danaeb | 0:d3e390d62607 | 11 | |
danaeb | 0:d3e390d62607 | 12 | #define TRACE_GROUP "acmd" |
danaeb | 0:d3e390d62607 | 13 | |
danaeb | 0:d3e390d62607 | 14 | using namespace liboo; |
danaeb | 0:d3e390d62607 | 15 | |
danaeb | 0:d3e390d62607 | 16 | CommandParser *AppCommand::_parser = NULL; |
danaeb | 0:d3e390d62607 | 17 | SensorManager *AppCommand::_sensor_manager = NULL; |
danaeb | 0:d3e390d62607 | 18 | Calendar *AppCommand::_calendar = NULL; |
danaeb | 0:d3e390d62607 | 19 | bool AppCommand::_is_intialized = false; |
danaeb | 0:d3e390d62607 | 20 | bool AppCommand::_is_pump_on = false; |
danaeb | 0:d3e390d62607 | 21 | |
danaeb | 0:d3e390d62607 | 22 | void AppCommand::configure(SensorManager* sensor_manager, |
danaeb | 0:d3e390d62607 | 23 | Calendar *calendar){ |
danaeb | 0:d3e390d62607 | 24 | _parser = new CommandParser; |
danaeb | 0:d3e390d62607 | 25 | _sensor_manager = sensor_manager; |
danaeb | 0:d3e390d62607 | 26 | _is_pump_on = false; |
danaeb | 0:d3e390d62607 | 27 | //_calendar = calendar; |
danaeb | 0:d3e390d62607 | 28 | |
danaeb | 0:d3e390d62607 | 29 | if(!_is_intialized){ |
danaeb | 0:d3e390d62607 | 30 | CommandCallback *cc; |
danaeb | 0:d3e390d62607 | 31 | _is_intialized = true; |
danaeb | 0:d3e390d62607 | 32 | |
danaeb | 0:d3e390d62607 | 33 | // set rtc time |
danaeb | 0:d3e390d62607 | 34 | cc = new CommandCallback("set time", 0b10000000, 0b00000000, _rtc_set_time); |
danaeb | 0:d3e390d62607 | 35 | _parser->register_command_callback(cc); |
danaeb | 0:d3e390d62607 | 36 | |
danaeb | 0:d3e390d62607 | 37 | cc = new CommandCallback("set pump", 0b00000001, 0b00000000, _set_pump); |
danaeb | 0:d3e390d62607 | 38 | _parser->register_command_callback(cc); |
danaeb | 0:d3e390d62607 | 39 | |
danaeb | 0:d3e390d62607 | 40 | #if 0 |
danaeb | 0:d3e390d62607 | 41 | // reset calendar |
danaeb | 0:d3e390d62607 | 42 | cc = new CommandCallback("reset calendar", 0b00110000, 0b00000000, |
danaeb | 0:d3e390d62607 | 43 | _calendar_reset); |
danaeb | 0:d3e390d62607 | 44 | _parser->register_command_callback(cc); |
danaeb | 0:d3e390d62607 | 45 | |
danaeb | 0:d3e390d62607 | 46 | // add time slot |
danaeb | 0:d3e390d62607 | 47 | cc = new CommandCallback("add time slot", 0b00100000, 0b01000000, |
danaeb | 0:d3e390d62607 | 48 | _calendar_add_time_slot); |
danaeb | 0:d3e390d62607 | 49 | _parser->register_command_callback(cc); |
danaeb | 0:d3e390d62607 | 50 | |
danaeb | 0:d3e390d62607 | 51 | // configure active sensors |
danaeb | 0:d3e390d62607 | 52 | cc = new CommandCallback("sensor", 0b00001000, 0b01110111, |
danaeb | 0:d3e390d62607 | 53 | _sensor_configure_active); |
danaeb | 0:d3e390d62607 | 54 | _parser->register_command_callback(cc); |
danaeb | 0:d3e390d62607 | 55 | |
danaeb | 0:d3e390d62607 | 56 | // disable enable calendar |
danaeb | 0:d3e390d62607 | 57 | cc = new CommandCallback("calendar", 0b01000000, 0b00111110, _calendar_state); |
danaeb | 0:d3e390d62607 | 58 | _parser->register_command_callback(cc); |
danaeb | 0:d3e390d62607 | 59 | #endif |
danaeb | 0:d3e390d62607 | 60 | } |
danaeb | 0:d3e390d62607 | 61 | } |
danaeb | 0:d3e390d62607 | 62 | |
danaeb | 0:d3e390d62607 | 63 | void AppCommand::process_command(u8 command_id, u8 *command_data, u16 command_size){ |
danaeb | 0:d3e390d62607 | 64 | _parser->execute(command_id, command_data, command_size); |
danaeb | 0:d3e390d62607 | 65 | } |
danaeb | 0:d3e390d62607 | 66 | |
danaeb | 0:d3e390d62607 | 67 | bool AppCommand::get_pump_state(){ |
danaeb | 0:d3e390d62607 | 68 | return _is_pump_on; |
danaeb | 0:d3e390d62607 | 69 | } |
danaeb | 0:d3e390d62607 | 70 | |
danaeb | 0:d3e390d62607 | 71 | u16 AppCommand::_rtc_set_time(u8 command_id, u8* command_data, u16 command_data_size){ |
danaeb | 0:d3e390d62607 | 72 | u16 ret = 0; |
danaeb | 0:d3e390d62607 | 73 | tr_info("command set time"); |
danaeb | 0:d3e390d62607 | 74 | if(command_data_size < 4){ |
danaeb | 0:d3e390d62607 | 75 | return ret; |
danaeb | 0:d3e390d62607 | 76 | } |
danaeb | 0:d3e390d62607 | 77 | |
danaeb | 0:d3e390d62607 | 78 | s32 offset; |
danaeb | 0:d3e390d62607 | 79 | u32 current_time; |
danaeb | 0:d3e390d62607 | 80 | offset = *((u32*)command_data); |
danaeb | 0:d3e390d62607 | 81 | |
danaeb | 0:d3e390d62607 | 82 | Rtc *rtc = Rtc::get_instance(); |
danaeb | 0:d3e390d62607 | 83 | tr_info("\t offset : %ld", offset); |
danaeb | 0:d3e390d62607 | 84 | rtc->apply_offset(offset); |
danaeb | 0:d3e390d62607 | 85 | |
danaeb | 0:d3e390d62607 | 86 | // check calendar in case we need to change latch state. |
danaeb | 0:d3e390d62607 | 87 | current_time = rtc->get_time(); |
danaeb | 0:d3e390d62607 | 88 | tr_info("\t time: %lu", current_time); |
danaeb | 0:d3e390d62607 | 89 | //_calendar->check(current_time); |
danaeb | 0:d3e390d62607 | 90 | |
danaeb | 0:d3e390d62607 | 91 | ret = 4; |
danaeb | 0:d3e390d62607 | 92 | return ret; |
danaeb | 0:d3e390d62607 | 93 | } |
danaeb | 0:d3e390d62607 | 94 | |
danaeb | 0:d3e390d62607 | 95 | u16 AppCommand::_set_pump(u8 command_id, u8* command_data, u16 command_data_size){ |
danaeb | 0:d3e390d62607 | 96 | u16 ret = 0; |
danaeb | 0:d3e390d62607 | 97 | tr_info("command set pump"); |
danaeb | 0:d3e390d62607 | 98 | if(command_data_size < 1){ |
danaeb | 0:d3e390d62607 | 99 | return ret; |
danaeb | 0:d3e390d62607 | 100 | } |
danaeb | 0:d3e390d62607 | 101 | |
danaeb | 0:d3e390d62607 | 102 | uint8_t state = *command_data; |
danaeb | 0:d3e390d62607 | 103 | |
danaeb | 0:d3e390d62607 | 104 | PowerManager *pm = PowerManager::get_instance(); |
danaeb | 0:d3e390d62607 | 105 | if(state == 1){ |
danaeb | 0:d3e390d62607 | 106 | tr_debug("turn on !!!"); |
danaeb | 0:d3e390d62607 | 107 | pm->lock_and_turn_on_motor(); |
danaeb | 0:d3e390d62607 | 108 | _is_pump_on = true; |
danaeb | 0:d3e390d62607 | 109 | } |
danaeb | 0:d3e390d62607 | 110 | else { |
danaeb | 0:d3e390d62607 | 111 | tr_debug("turn off !!!"); |
danaeb | 0:d3e390d62607 | 112 | pm->unlock_and_turn_off_motor(); |
danaeb | 0:d3e390d62607 | 113 | _is_pump_on = false; |
danaeb | 0:d3e390d62607 | 114 | } |
danaeb | 0:d3e390d62607 | 115 | |
danaeb | 0:d3e390d62607 | 116 | ret = 1; |
danaeb | 0:d3e390d62607 | 117 | return ret; |
danaeb | 0:d3e390d62607 | 118 | } |
danaeb | 0:d3e390d62607 | 119 | |
danaeb | 0:d3e390d62607 | 120 | u16 AppCommand::_calendar_reset(u8 command_id, u8* command_data, |
danaeb | 0:d3e390d62607 | 121 | u16 command_data_size){ |
danaeb | 0:d3e390d62607 | 122 | tr_info("command reset calendar"); |
danaeb | 0:d3e390d62607 | 123 | //_calendar->reset(false); |
danaeb | 0:d3e390d62607 | 124 | return 0; |
danaeb | 0:d3e390d62607 | 125 | } |
danaeb | 0:d3e390d62607 | 126 | |
danaeb | 0:d3e390d62607 | 127 | u16 AppCommand::_calendar_state(u8 command_id, u8* command_data, |
danaeb | 0:d3e390d62607 | 128 | u16 command_data_size){ |
danaeb | 0:d3e390d62607 | 129 | PRINT(" disable calendar\r\n"); |
danaeb | 0:d3e390d62607 | 130 | if(command_id & 0x01){ |
danaeb | 0:d3e390d62607 | 131 | //_calendar->enable(); |
danaeb | 0:d3e390d62607 | 132 | } |
danaeb | 0:d3e390d62607 | 133 | else{ |
danaeb | 0:d3e390d62607 | 134 | //_calendar->disable(); |
danaeb | 0:d3e390d62607 | 135 | } |
danaeb | 0:d3e390d62607 | 136 | return 0; |
danaeb | 0:d3e390d62607 | 137 | } |
danaeb | 0:d3e390d62607 | 138 | |
danaeb | 0:d3e390d62607 | 139 | u16 AppCommand::_calendar_add_time_slot(u8 command_id, u8* command_data, u16 command_data_size){ |
danaeb | 0:d3e390d62607 | 140 | u16 processed_size = 0; |
danaeb | 0:d3e390d62607 | 141 | |
danaeb | 0:d3e390d62607 | 142 | |
danaeb | 0:d3e390d62607 | 143 | tr_info("command add time slot"); |
danaeb | 0:d3e390d62607 | 144 | // compute start time offset |
danaeb | 0:d3e390d62607 | 145 | // the start offset can be changed even if no time slot are present |
danaeb | 0:d3e390d62607 | 146 | s8 start_time_offset; |
danaeb | 0:d3e390d62607 | 147 | start_time_offset = (command_id & 0x0F); |
danaeb | 0:d3e390d62607 | 148 | start_time_offset -= 8; |
danaeb | 0:d3e390d62607 | 149 | //_calendar->set_start_time_offset(start_time_offset); |
danaeb | 0:d3e390d62607 | 150 | |
danaeb | 0:d3e390d62607 | 151 | |
danaeb | 0:d3e390d62607 | 152 | // several time slot can be embedded in one command |
danaeb | 0:d3e390d62607 | 153 | // each time slot is coded on 4 bytes so the command size must have |
danaeb | 0:d3e390d62607 | 154 | // a multiplicity of 4 |
danaeb | 0:d3e390d62607 | 155 | if(command_data_size < 4 || (command_data_size % 4 != 0)){ |
danaeb | 0:d3e390d62607 | 156 | return processed_size; |
danaeb | 0:d3e390d62607 | 157 | } |
danaeb | 0:d3e390d62607 | 158 | u8 i = 0; |
danaeb | 0:d3e390d62607 | 159 | while(command_data_size - processed_size != 0){ |
danaeb | 0:d3e390d62607 | 160 | bool use_stop_day = false; |
danaeb | 0:d3e390d62607 | 161 | u16 duration; |
danaeb | 0:d3e390d62607 | 162 | u16 start_time; |
danaeb | 0:d3e390d62607 | 163 | u8 start_day; |
danaeb | 0:d3e390d62607 | 164 | u8 stop_day; |
danaeb | 0:d3e390d62607 | 165 | u8 day_cycle; |
danaeb | 0:d3e390d62607 | 166 | |
danaeb | 0:d3e390d62607 | 167 | u32 data; |
danaeb | 0:d3e390d62607 | 168 | |
danaeb | 0:d3e390d62607 | 169 | // /4 because we are incrementing u32 pointer |
danaeb | 0:d3e390d62607 | 170 | // so 1 increment means +4 address |
danaeb | 0:d3e390d62607 | 171 | data = *((u32*)command_data + processed_size/4); |
danaeb | 0:d3e390d62607 | 172 | |
danaeb | 0:d3e390d62607 | 173 | |
danaeb | 0:d3e390d62607 | 174 | start_time = (data & 0x000000FF); |
danaeb | 0:d3e390d62607 | 175 | duration = ((data & 0x0001FF00) >> 8); |
danaeb | 0:d3e390d62607 | 176 | day_cycle = ((data & 0x001E0000) >> (8+9)); |
danaeb | 0:d3e390d62607 | 177 | start_day = ((data & 0x03E00000) >> (8+9+4)); |
danaeb | 0:d3e390d62607 | 178 | use_stop_day = ((data & 0x80000000) > 0); |
danaeb | 0:d3e390d62607 | 179 | |
danaeb | 0:d3e390d62607 | 180 | if(use_stop_day){ |
danaeb | 0:d3e390d62607 | 181 | stop_day = ((data & 0x7C000000) >> (8+9+4+5)); |
danaeb | 0:d3e390d62607 | 182 | } |
danaeb | 0:d3e390d62607 | 183 | else{ |
danaeb | 0:d3e390d62607 | 184 | // trick to make it work when start day is 0: |
danaeb | 0:d3e390d62607 | 185 | // 0+32-1 = 31 |
danaeb | 0:d3e390d62607 | 186 | // if start day is 31: |
danaeb | 0:d3e390d62607 | 187 | // 31+32-1 % 32 = 62 % 32 = 30 |
danaeb | 0:d3e390d62607 | 188 | // |
danaeb | 0:d3e390d62607 | 189 | stop_day = (start_day + 32 - 1) % 32; |
danaeb | 0:d3e390d62607 | 190 | } |
danaeb | 0:d3e390d62607 | 191 | |
danaeb | 0:d3e390d62607 | 192 | // received start time is by 10 minutes time slots |
danaeb | 0:d3e390d62607 | 193 | // and calendar uses 5 minutes time slots |
danaeb | 0:d3e390d62607 | 194 | start_time *= 2; |
danaeb | 0:d3e390d62607 | 195 | |
danaeb | 0:d3e390d62607 | 196 | tr_info("\t command number: %d\r\n" |
danaeb | 0:d3e390d62607 | 197 | "\t\t start day: %d\r\n" |
danaeb | 0:d3e390d62607 | 198 | "\t\t stop day: %d\r\n" |
danaeb | 0:d3e390d62607 | 199 | "\t\t day cycle: %d\r\n" |
danaeb | 0:d3e390d62607 | 200 | "\t\t start time: %d:%d\r\n" |
danaeb | 0:d3e390d62607 | 201 | "\t\t duration %d", |
danaeb | 0:d3e390d62607 | 202 | i, start_day, stop_day, day_cycle, |
danaeb | 0:d3e390d62607 | 203 | (start_time*5)/60, ((start_time*5) % 60), duration); |
danaeb | 0:d3e390d62607 | 204 | // |
danaeb | 0:d3e390d62607 | 205 | // true -> add time slot |
danaeb | 0:d3e390d62607 | 206 | //_calendar->set_time_slot(true, start_day, stop_day, day_cycle, start_time, duration); |
danaeb | 0:d3e390d62607 | 207 | processed_size += 4; |
danaeb | 0:d3e390d62607 | 208 | ++i; |
danaeb | 0:d3e390d62607 | 209 | } |
danaeb | 0:d3e390d62607 | 210 | Rtc *rtc = Rtc::get_instance(); |
danaeb | 0:d3e390d62607 | 211 | |
danaeb | 0:d3e390d62607 | 212 | // check calendar in case we need to change latch state. |
danaeb | 0:d3e390d62607 | 213 | u32 current_time = rtc->get_time(); |
danaeb | 0:d3e390d62607 | 214 | //_calendar->check(current_time); |
danaeb | 0:d3e390d62607 | 215 | |
danaeb | 0:d3e390d62607 | 216 | return processed_size; |
danaeb | 0:d3e390d62607 | 217 | |
danaeb | 0:d3e390d62607 | 218 | } |
danaeb | 0:d3e390d62607 | 219 | u16 AppCommand::_sensor_configure_active(u8 command_id, u8* command_data, u16 command_data_size){ |
danaeb | 0:d3e390d62607 | 220 | u16 ret = 0; |
danaeb | 0:d3e390d62607 | 221 | tr_info("command config sensor"); |
danaeb | 0:d3e390d62607 | 222 | if(command_data_size < 2){ |
danaeb | 0:d3e390d62607 | 223 | return ret; |
danaeb | 0:d3e390d62607 | 224 | } |
danaeb | 0:d3e390d62607 | 225 | #if 0 |
danaeb | 0:d3e390d62607 | 226 | u16 data = ((u16*)command_data)[0]; |
danaeb | 0:d3e390d62607 | 227 | |
danaeb | 0:d3e390d62607 | 228 | bool is_sensor_enable; |
danaeb | 0:d3e390d62607 | 229 | |
danaeb | 0:d3e390d62607 | 230 | is_sensor_enable = ((data & 0x0001) > 0); |
danaeb | 0:d3e390d62607 | 231 | _sensor_configure(SENSOR_ID_TENSIOMETER_1, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 232 | |
danaeb | 0:d3e390d62607 | 233 | is_sensor_enable = ((data & 0x0002) > 0); |
danaeb | 0:d3e390d62607 | 234 | _sensor_configure(SENSOR_ID_TENSIOMETER_2, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 235 | |
danaeb | 0:d3e390d62607 | 236 | is_sensor_enable = ((data & 0x0004) > 0); |
danaeb | 0:d3e390d62607 | 237 | _sensor_configure(SENSOR_ID_TENSIOMETER_3, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 238 | |
danaeb | 0:d3e390d62607 | 239 | is_sensor_enable = ((data & 0x0008) > 0); |
danaeb | 0:d3e390d62607 | 240 | _sensor_configure(SENSOR_ID_PRESSURE_1, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 241 | |
danaeb | 0:d3e390d62607 | 242 | is_sensor_enable = ((data & 0x0010) > 0); |
danaeb | 0:d3e390d62607 | 243 | _sensor_configure(SENSOR_ID_PRESSURE_2, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 244 | |
danaeb | 0:d3e390d62607 | 245 | is_sensor_enable = ((data & 0x0020) > 0); |
danaeb | 0:d3e390d62607 | 246 | _sensor_configure(SENSOR_ID_PRESSURE_3, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 247 | |
danaeb | 0:d3e390d62607 | 248 | is_sensor_enable = ((data & 0x0040) > 0); |
danaeb | 0:d3e390d62607 | 249 | _sensor_configure(SENSOR_ID_PRESSURE_PIPE, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 250 | |
danaeb | 0:d3e390d62607 | 251 | is_sensor_enable = ((data & 0x0080) > 0); |
danaeb | 0:d3e390d62607 | 252 | _sensor_configure(SENSOR_ID_IMPULSE, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 253 | |
danaeb | 0:d3e390d62607 | 254 | is_sensor_enable = ((data & 0x0100) > 0); |
danaeb | 0:d3e390d62607 | 255 | _sensor_configure(SENSOR_ID_SOIL_TEMPERATURE, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 256 | |
danaeb | 0:d3e390d62607 | 257 | is_sensor_enable = ((data & 0x0200) > 0); |
danaeb | 0:d3e390d62607 | 258 | _sensor_configure(SENSOR_ID_BOARD_HUMIDITY, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 259 | |
danaeb | 0:d3e390d62607 | 260 | is_sensor_enable = ((data & 0x0400) > 0); |
danaeb | 0:d3e390d62607 | 261 | _sensor_configure(SENSOR_ID_BOARD_TEMPERATURE, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 262 | |
danaeb | 0:d3e390d62607 | 263 | is_sensor_enable = ((data & 0x0800) > 0); |
danaeb | 0:d3e390d62607 | 264 | _sensor_configure(SENSOR_ID_BAT, is_sensor_enable); |
danaeb | 0:d3e390d62607 | 265 | |
danaeb | 0:d3e390d62607 | 266 | u8 redundancy = ((data & 0xF000) >> 12)+1; |
danaeb | 0:d3e390d62607 | 267 | AppMain::set_history_size(redundancy); |
danaeb | 0:d3e390d62607 | 268 | |
danaeb | 0:d3e390d62607 | 269 | #endif |
danaeb | 0:d3e390d62607 | 270 | |
danaeb | 0:d3e390d62607 | 271 | ret = 2; |
danaeb | 0:d3e390d62607 | 272 | return ret; |
danaeb | 0:d3e390d62607 | 273 | } |
danaeb | 0:d3e390d62607 | 274 | |
danaeb | 0:d3e390d62607 | 275 | void AppCommand::_sensor_configure(u8 sensor_id, bool on_off){ |
danaeb | 0:d3e390d62607 | 276 | if(on_off){ |
danaeb | 0:d3e390d62607 | 277 | tr_debug(" \tenable sensor %d", sensor_id); |
danaeb | 0:d3e390d62607 | 278 | _sensor_manager->enable_sensor(sensor_id); |
danaeb | 0:d3e390d62607 | 279 | } |
danaeb | 0:d3e390d62607 | 280 | else{ |
danaeb | 0:d3e390d62607 | 281 | tr_debug(" \tdisable sensor %d", sensor_id); |
danaeb | 0:d3e390d62607 | 282 | _sensor_manager->disable_sensor(sensor_id); |
danaeb | 0:d3e390d62607 | 283 | } |
danaeb | 0:d3e390d62607 | 284 | } |
danaeb | 0:d3e390d62607 | 285 |