|
| 1 | +#include "../Modbus.h" |
| 2 | + |
| 3 | +void BuildSender(App* app, uint8_t* buf); |
| 4 | + |
| 5 | +////////////////////////// Manual Sender Scene ////////////////////////// |
| 6 | +const char* fns[] = {"0x01", "0x02", "0x03", "0x04", "0x05", "0x06", "0x0F", "0x10"}; |
| 7 | + |
| 8 | +void itemChangeCB(VariableItem* item) { |
| 9 | + App* app = variable_item_get_context(item); |
| 10 | + uint8_t* buf = app->msgBuf; |
| 11 | + uint8_t index = variable_item_get_current_value_index(item); |
| 12 | + uint8_t selectedIndex = variable_item_list_get_selected_item_index(app->varList); |
| 13 | + uint16_t Value; |
| 14 | + char str[10]; |
| 15 | + switch(selectedIndex) { |
| 16 | + case 0: |
| 17 | + snprintf(str, sizeof(str), "%d", index + 1); |
| 18 | + variable_item_set_current_value_text(item, strdup(str)); |
| 19 | + buf[0] = index + 1; |
| 20 | + break; |
| 21 | + case 1: |
| 22 | + variable_item_set_current_value_text(item, fns[index]); |
| 23 | + FUNCTION = index <= 0x05 ? index + 1 : index + 9; |
| 24 | + buf[4] = 0; |
| 25 | + buf[5] = 1; |
| 26 | + BuildSender(app, buf); |
| 27 | + break; |
| 28 | + case 2: |
| 29 | + snprintf(str, sizeof(str), "%d", index); |
| 30 | + variable_item_set_current_value_text(item, strdup(str)); |
| 31 | + buf[2] = index >> 8 & 0x00FF; |
| 32 | + buf[3] = index & 0x00FF; |
| 33 | + break; |
| 34 | + case 3: |
| 35 | + if(FUNCTION != 0x05 && FUNCTION != 0x06) { |
| 36 | + index++; |
| 37 | + snprintf(str, sizeof(str), "%d", index); |
| 38 | + variable_item_set_current_value_text(item, strdup(str)); |
| 39 | + buf[4] = index >> 8 & 0x00FF; |
| 40 | + buf[5] = index & 0x00FF; |
| 41 | + if(FUNCTION >= 0x0F) { |
| 42 | + Value = (buf[4] << 8 | buf[5]); |
| 43 | + if(FUNCTION == 0x0F) |
| 44 | + Value = Value % 8 ? Value / 8 + 1 : Value / 8; |
| 45 | + else |
| 46 | + Value = Value * 2; |
| 47 | + item = variable_item_list_get(app->varList, 4); |
| 48 | + snprintf(str, sizeof(str), "[ %d ]", Value); |
| 49 | + variable_item_set_current_value_text(item, strdup(str)); |
| 50 | + if(BYTECOUNT != Value) { |
| 51 | + BYTECOUNT = Value; |
| 52 | + BuildSender(app, buf); |
| 53 | + } |
| 54 | + } |
| 55 | + } else { |
| 56 | + Value = FUNCTION == 5 ? index ? 0xFF00 : 0x0000 : index; |
| 57 | + snprintf(str, sizeof(str), "0x%04X", Value); |
| 58 | + variable_item_set_current_value_text( |
| 59 | + item, FUNCTION == 0x05 ? index ? "ON" : "OFF" : str); |
| 60 | + buf[4] = Value >> 8 & 0x00FF; |
| 61 | + buf[5] = Value & 0x00FF; |
| 62 | + } |
| 63 | + break; |
| 64 | + default: |
| 65 | + Value = index; |
| 66 | + snprintf(str, sizeof(str), FUNCTION == 0x10 ? "0x%04X" : "0x%02X", Value); |
| 67 | + variable_item_set_current_value_text(item, str); |
| 68 | + if(FUNCTION == 0x0F) { |
| 69 | + selectedIndex += 2; |
| 70 | + buf[selectedIndex] = Value; |
| 71 | + } else { |
| 72 | + selectedIndex += selectedIndex - 3; |
| 73 | + |
| 74 | + buf[selectedIndex] = Value >> 8 & 0x0FF; |
| 75 | + buf[selectedIndex + 1] = Value & 0x00FF; |
| 76 | + } |
| 77 | + break; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void itemEnterCB(void* context, uint32_t index) { |
| 82 | + App* app = context; |
| 83 | + uint8_t* buf = app->msgBuf; |
| 84 | + uint8_t SendButton = FUNCTION >= 0x0F ? (FUNCTION == 0x0F ? BYTECOUNT : QUANTITY) + 5 : 4; |
| 85 | + scene_manager_set_scene_state(app->sceneManager, app_scene_manual_sender, index); |
| 86 | + if(index == SendButton) { |
| 87 | + scene_manager_set_scene_state(app->sceneManager, app_scene_sniffer, Sender_Option); |
| 88 | + scene_manager_next_scene(app->sceneManager, app_scene_sniffer); |
| 89 | + } |
| 90 | + |
| 91 | + else if(index == 1 || (FUNCTION >= 0x0F && index == 4)) { |
| 92 | + } else { |
| 93 | + if(!(FUNCTION == 0x05 && index == 3)) { |
| 94 | + scene_manager_set_scene_state(app->sceneManager, app_scene_byte_input, index); |
| 95 | + scene_manager_next_scene(app->sceneManager, app_scene_byte_input); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | +void BuildValues(App* app, uint16_t byteCount, uint8_t* buf, bool one) { |
| 100 | + VariableItem* item; |
| 101 | + char lbl[20]; |
| 102 | + char val[10]; |
| 103 | + for(uint16_t i = 0; i < byteCount; i += one ? 1 : 2) { |
| 104 | + snprintf(lbl, sizeof(lbl), one ? "Byte %d" : "Register %d", one ? i + 1 : i / 2 + 1); |
| 105 | + snprintf( |
| 106 | + val, sizeof(val), one ? "0x%02X" : "0x%04X", one ? buf[i] : buf[i] << 8 | buf[i + 1]); |
| 107 | + item = variable_item_list_add(app->varList, strdup(lbl), 255, itemChangeCB, app); |
| 108 | + variable_item_set_current_value_text(item, strdup(val)); |
| 109 | + variable_item_set_current_value_index( |
| 110 | + item, MIN(255, one ? buf[i] : buf[i] << 8 | buf[i + 1])); |
| 111 | + } |
| 112 | +} |
| 113 | +void BuildSender(App* app, uint8_t* buf) { |
| 114 | + variable_item_list_reset(app->varList); |
| 115 | + VariableItem* item; |
| 116 | + uint16_t Value = 0; |
| 117 | + char val[10]; |
| 118 | + SLAVE = MIN(SLAVE, 32); |
| 119 | + snprintf(val, sizeof(val), "%d", SLAVE); |
| 120 | + item = variable_item_list_add(app->varList, "Peripheral ID", 32, itemChangeCB, app); |
| 121 | + variable_item_set_current_value_text(item, strdup(val)); |
| 122 | + variable_item_set_current_value_index(item, SLAVE - 1); |
| 123 | + item = variable_item_list_add(app->varList, "Function", 8, itemChangeCB, app); |
| 124 | + variable_item_set_current_value_text(item, fns[FUNCTION <= 6 ? FUNCTION - 1 : FUNCTION - 9]); |
| 125 | + variable_item_set_current_value_index(item, FUNCTION <= 6 ? FUNCTION - 1 : FUNCTION - 9); |
| 126 | + Value = STARTADDRESS; |
| 127 | + snprintf(val, sizeof(val), "%d", Value); |
| 128 | + item = variable_item_list_add(app->varList, "Start Address", 255, itemChangeCB, app); |
| 129 | + variable_item_set_current_value_text(item, strdup(val)); |
| 130 | + variable_item_set_current_value_index(item, MIN(255, STARTADDRESS)); |
| 131 | + if(FUNCTION != 0x05 && FUNCTION != 0x06) { |
| 132 | + uint16_t max = FUNCTION == 0x10 ? 0x0A : |
| 133 | + FUNCTION == 0x0F ? 0x50 : |
| 134 | + FUNCTION <= 0x02 ? 0x7D0 : |
| 135 | + 0x7D; |
| 136 | + Value = MIN(buf[4] << 8 | buf[5], max); |
| 137 | + snprintf(val, sizeof(val), "%d", Value); |
| 138 | + item = variable_item_list_add(app->varList, "Quantity", max, itemChangeCB, app); |
| 139 | + variable_item_set_current_value_text(item, strdup(val)); |
| 140 | + variable_item_set_current_value_index(item, MIN(Value - 1, MIN(max, 255))); |
| 141 | + buf[4] = Value >> 8 & 0x00FF; |
| 142 | + buf[5] = Value & 0x00FF; |
| 143 | + } else { |
| 144 | + Value = buf[4] << 8 | buf[5]; |
| 145 | + snprintf(val, sizeof(val), "0x%04X", Value); |
| 146 | + item = variable_item_list_add( |
| 147 | + app->varList, "Value", FUNCTION == 0x05 ? 2 : 255, itemChangeCB, app); |
| 148 | + variable_item_set_current_value_text( |
| 149 | + item, FUNCTION == 0x05 ? Value ? "ON" : "OFF" : strdup(val)); |
| 150 | + variable_item_set_current_value_index( |
| 151 | + item, FUNCTION == 0x05 ? Value ? 1 : 0 : MIN(Value, 255)); |
| 152 | + Value = FUNCTION == 5 ? Value ? 0xFF00 : 0x0000 : Value; |
| 153 | + buf[4] = Value >> 8 & 0x00FF; |
| 154 | + buf[5] = Value & 0x00FF; |
| 155 | + } |
| 156 | + if(FUNCTION >= 0x0F) { |
| 157 | + Value = (buf[4] << 8 | buf[5]); |
| 158 | + if(FUNCTION == 0x0F) |
| 159 | + Value = Value % 8 ? Value / 8 + 1 : Value / 8; |
| 160 | + else |
| 161 | + Value = Value * 2; |
| 162 | + snprintf(val, sizeof(val), "[ %d ]", Value); |
| 163 | + item = variable_item_list_add(app->varList, "ByteCount", 1, NULL, app); |
| 164 | + variable_item_set_current_value_text(item, strdup(val)); |
| 165 | + variable_item_set_current_value_index(item, 0); |
| 166 | + BYTECOUNT = Value; |
| 167 | + app->msgLen = Value + 9; |
| 168 | + BuildValues(app, Value, buf + 7, FUNCTION == 0x0F ? true : false); |
| 169 | + } |
| 170 | + |
| 171 | + item = variable_item_list_add(app->varList, "Send Packet", 1, NULL, app); |
| 172 | + variable_item_list_set_enter_callback(app->varList, itemEnterCB, app); |
| 173 | + variable_item_set_current_value_text(item, ""); |
| 174 | + variable_item_set_current_value_index(item, 0); |
| 175 | +} |
| 176 | +void app_scene_manual_sender_on_enter(void* context) { |
| 177 | + App* app = context; |
| 178 | + BuildSender(app, app->msgBuf); |
| 179 | + variable_item_list_set_selected_item( |
| 180 | + app->varList, scene_manager_get_scene_state(app->sceneManager, app_scene_manual_sender)); |
| 181 | + view_dispatcher_switch_to_view(app->viewDispatcher, VarList_View); |
| 182 | +} |
| 183 | +bool app_scene_manual_sender_on_event(void* context, SceneManagerEvent event) { |
| 184 | + UNUSED(context); |
| 185 | + UNUSED(event); |
| 186 | + return false; |
| 187 | +} |
| 188 | +void app_scene_manual_sender_on_exit(void* context) { |
| 189 | + App* app = context; |
| 190 | + variable_item_list_reset(app->varList); |
| 191 | +} |
0 commit comments