xxxx
Dependencies: BufferedSoftSerial Noritakes iButton CRC16Modbus Displays Audiox
main.cpp@0:a8b6c10f03dc, 2020-08-26 (annotated)
- Committer:
- mauuuuul
- Date:
- Wed Aug 26 19:47:52 2020 +0000
- Revision:
- 0:a8b6c10f03dc
xxxx
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mauuuuul | 0:a8b6c10f03dc | 1 | #include "mbed.h" |
mauuuuul | 0:a8b6c10f03dc | 2 | #include "EthernetInterface.h" |
mauuuuul | 0:a8b6c10f03dc | 3 | #include <string> |
mauuuuul | 0:a8b6c10f03dc | 4 | #include <vector> |
mauuuuul | 0:a8b6c10f03dc | 5 | #include "Audio.h" |
mauuuuul | 0:a8b6c10f03dc | 6 | #include "iButton.h" |
mauuuuul | 0:a8b6c10f03dc | 7 | #include <bitset> |
mauuuuul | 0:a8b6c10f03dc | 8 | #include "CRC16Modbus.h" |
mauuuuul | 0:a8b6c10f03dc | 9 | #include "BufferedSoftSerial.h" |
mauuuuul | 0:a8b6c10f03dc | 10 | #include "Displays.h" |
mauuuuul | 0:a8b6c10f03dc | 11 | //#include <list> |
mauuuuul | 0:a8b6c10f03dc | 12 | //#include <algorithm> |
mauuuuul | 0:a8b6c10f03dc | 13 | #include <Watchdog.h> |
mauuuuul | 0:a8b6c10f03dc | 14 | |
mauuuuul | 0:a8b6c10f03dc | 15 | // Network interface |
mauuuuul | 0:a8b6c10f03dc | 16 | TCPSocket socket; |
mauuuuul | 0:a8b6c10f03dc | 17 | |
mauuuuul | 0:a8b6c10f03dc | 18 | Thread thd_Cek_Ibutton; |
mauuuuul | 0:a8b6c10f03dc | 19 | Thread thd_Buzzer; |
mauuuuul | 0:a8b6c10f03dc | 20 | Watchdog &watchdog = Watchdog::get_instance(); |
mauuuuul | 0:a8b6c10f03dc | 21 | |
mauuuuul | 0:a8b6c10f03dc | 22 | MyAudio buzz(D7); |
mauuuuul | 0:a8b6c10f03dc | 23 | Displays dsp(PTC8,PTC1); |
mauuuuul | 0:a8b6c10f03dc | 24 | int LCD_Step = 0; |
mauuuuul | 0:a8b6c10f03dc | 25 | int LCD_Step_4 = 0; |
mauuuuul | 0:a8b6c10f03dc | 26 | |
mauuuuul | 0:a8b6c10f03dc | 27 | #define IP "192.168.0.5" |
mauuuuul | 0:a8b6c10f03dc | 28 | #define GATEWAY "192.168.0.1" |
mauuuuul | 0:a8b6c10f03dc | 29 | #define MASK "255.255.255.0" |
mauuuuul | 0:a8b6c10f03dc | 30 | |
mauuuuul | 0:a8b6c10f03dc | 31 | #define IPSERVER "192.168.0.150" |
mauuuuul | 0:a8b6c10f03dc | 32 | #define PORTSERVER 12345 |
mauuuuul | 0:a8b6c10f03dc | 33 | |
mauuuuul | 0:a8b6c10f03dc | 34 | #define board_sum 2 |
mauuuuul | 0:a8b6c10f03dc | 35 | |
mauuuuul | 0:a8b6c10f03dc | 36 | char cmd_resets[] = "R00"; |
mauuuuul | 0:a8b6c10f03dc | 37 | char cek_con[] = "PING"; |
mauuuuul | 0:a8b6c10f03dc | 38 | |
mauuuuul | 0:a8b6c10f03dc | 39 | int Key_On_Process = 0; |
mauuuuul | 0:a8b6c10f03dc | 40 | |
mauuuuul | 0:a8b6c10f03dc | 41 | Serial dbg(USBTX,USBRX); |
mauuuuul | 0:a8b6c10f03dc | 42 | BufferedSoftSerial dv(PTB19,PTB18); |
mauuuuul | 0:a8b6c10f03dc | 43 | DigitalOut rede(PTC9,0); |
mauuuuul | 0:a8b6c10f03dc | 44 | iButton ibutton(D4); |
mauuuuul | 0:a8b6c10f03dc | 45 | |
mauuuuul | 0:a8b6c10f03dc | 46 | bool tap = false; |
mauuuuul | 0:a8b6c10f03dc | 47 | bool lasttap = false; |
mauuuuul | 0:a8b6c10f03dc | 48 | |
mauuuuul | 0:a8b6c10f03dc | 49 | int board_number; |
mauuuuul | 0:a8b6c10f03dc | 50 | uint8_t baris; |
mauuuuul | 0:a8b6c10f03dc | 51 | uint8_t kolom; |
mauuuuul | 0:a8b6c10f03dc | 52 | uint8_t real_id; |
mauuuuul | 0:a8b6c10f03dc | 53 | uint8_t real_blink_id[5] = {64,64,64,64,64}; |
mauuuuul | 0:a8b6c10f03dc | 54 | uint8_t blinking_now = 0; |
mauuuuul | 0:a8b6c10f03dc | 55 | uint8_t idle_wait_result = 0; |
mauuuuul | 0:a8b6c10f03dc | 56 | uint8_t ids; |
mauuuuul | 0:a8b6c10f03dc | 57 | |
mauuuuul | 0:a8b6c10f03dc | 58 | typedef uint8_t byte; |
mauuuuul | 0:a8b6c10f03dc | 59 | //r0,r1,r2,r3,r4,r5,r6,r7 |
mauuuuul | 0:a8b6c10f03dc | 60 | byte Row_Board_Last[5][8],Row_Board_Now[5][8],Row_Board_Predict[5][8]; |
mauuuuul | 0:a8b6c10f03dc | 61 | |
mauuuuul | 0:a8b6c10f03dc | 62 | int board_88[8][8]= { |
mauuuuul | 0:a8b6c10f03dc | 63 | {0,1,2,3,4,5,6,7}, |
mauuuuul | 0:a8b6c10f03dc | 64 | {8,9,10,11,12,13,14,15}, |
mauuuuul | 0:a8b6c10f03dc | 65 | {16,17,18,19,20,21,22,23}, |
mauuuuul | 0:a8b6c10f03dc | 66 | {24,25,26,27,28,29,30,31}, |
mauuuuul | 0:a8b6c10f03dc | 67 | {32,33,34,35,36,37,38,39}, |
mauuuuul | 0:a8b6c10f03dc | 68 | {40,41,42,43,44,45,46,47}, |
mauuuuul | 0:a8b6c10f03dc | 69 | {48,49,50,51,52,53,54,55}, |
mauuuuul | 0:a8b6c10f03dc | 70 | {56,57,58,59,60,61,62,63} |
mauuuuul | 0:a8b6c10f03dc | 71 | }; |
mauuuuul | 0:a8b6c10f03dc | 72 | |
mauuuuul | 0:a8b6c10f03dc | 73 | unsigned char buffer_crc[2]; |
mauuuuul | 0:a8b6c10f03dc | 74 | unsigned short _crc; |
mauuuuul | 0:a8b6c10f03dc | 75 | |
mauuuuul | 0:a8b6c10f03dc | 76 | byte respon_from_slave_temp[17]; |
mauuuuul | 0:a8b6c10f03dc | 77 | |
mauuuuul | 0:a8b6c10f03dc | 78 | bool intr_Done = false; |
mauuuuul | 0:a8b6c10f03dc | 79 | |
mauuuuul | 0:a8b6c10f03dc | 80 | struct Frame_Data_Received { |
mauuuuul | 0:a8b6c10f03dc | 81 | int ids; |
mauuuuul | 0:a8b6c10f03dc | 82 | string ibutton; |
mauuuuul | 0:a8b6c10f03dc | 83 | int _key_id; |
mauuuuul | 0:a8b6c10f03dc | 84 | int cmd_id; |
mauuuuul | 0:a8b6c10f03dc | 85 | } frm_data_received; |
mauuuuul | 0:a8b6c10f03dc | 86 | |
mauuuuul | 0:a8b6c10f03dc | 87 | byte respon_from_slave[32]; |
mauuuuul | 0:a8b6c10f03dc | 88 | |
mauuuuul | 0:a8b6c10f03dc | 89 | uint8_t step = 0; |
mauuuuul | 0:a8b6c10f03dc | 90 | bool isConnected = false; |
mauuuuul | 0:a8b6c10f03dc | 91 | |
mauuuuul | 0:a8b6c10f03dc | 92 | vector<int> salah_taruh; |
mauuuuul | 0:a8b6c10f03dc | 93 | vector<int> salah_ambil; |
mauuuuul | 0:a8b6c10f03dc | 94 | vector<int> salah_taruh_last; |
mauuuuul | 0:a8b6c10f03dc | 95 | vector<int> salah_ambil_last; |
mauuuuul | 0:a8b6c10f03dc | 96 | int salah_ambil_count; |
mauuuuul | 0:a8b6c10f03dc | 97 | int salah_taruh_count; |
mauuuuul | 0:a8b6c10f03dc | 98 | |
mauuuuul | 0:a8b6c10f03dc | 99 | bool done[5] = {true,true,true,true,true}; |
mauuuuul | 0:a8b6c10f03dc | 100 | |
mauuuuul | 0:a8b6c10f03dc | 101 | vector<int> lst_salah; |
mauuuuul | 0:a8b6c10f03dc | 102 | vector<int> lst_salah_hst; |
mauuuuul | 0:a8b6c10f03dc | 103 | vector<int>::iterator it; |
mauuuuul | 0:a8b6c10f03dc | 104 | vector<int> to_remove; |
mauuuuul | 0:a8b6c10f03dc | 105 | |
mauuuuul | 0:a8b6c10f03dc | 106 | vector<uint8_t> to_sends; |
mauuuuul | 0:a8b6c10f03dc | 107 | vector<uint8_t> to_sends_last; |
mauuuuul | 0:a8b6c10f03dc | 108 | |
mauuuuul | 0:a8b6c10f03dc | 109 | string ibutton_code; |
mauuuuul | 0:a8b6c10f03dc | 110 | vector<string> split(const string str, const string separator) |
mauuuuul | 0:a8b6c10f03dc | 111 | { |
mauuuuul | 0:a8b6c10f03dc | 112 | char *cstr = const_cast<char*>(str.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 113 | char *current; |
mauuuuul | 0:a8b6c10f03dc | 114 | |
mauuuuul | 0:a8b6c10f03dc | 115 | std::vector<std::string>arr; |
mauuuuul | 0:a8b6c10f03dc | 116 | current = strtok(cstr, separator.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 117 | while(current != NULL) { |
mauuuuul | 0:a8b6c10f03dc | 118 | arr.push_back(current); |
mauuuuul | 0:a8b6c10f03dc | 119 | current = strtok(NULL, separator.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 120 | } |
mauuuuul | 0:a8b6c10f03dc | 121 | return arr; |
mauuuuul | 0:a8b6c10f03dc | 122 | } |
mauuuuul | 0:a8b6c10f03dc | 123 | |
mauuuuul | 0:a8b6c10f03dc | 124 | void Cek_Ibutton(); |
mauuuuul | 0:a8b6c10f03dc | 125 | void Cek_Salah_Buzzer(); |
mauuuuul | 0:a8b6c10f03dc | 126 | string Byte_to_StringBinary(byte data_byte); |
mauuuuul | 0:a8b6c10f03dc | 127 | uint8_t StringBinary_to_Byte(string data_binary); |
mauuuuul | 0:a8b6c10f03dc | 128 | int Get_Row(int key_id); |
mauuuuul | 0:a8b6c10f03dc | 129 | unsigned short Get_CRC_Modbus(byte data_byte[]); |
mauuuuul | 0:a8b6c10f03dc | 130 | void Send_RS485(int board,int key_id,int key_id_blinking,bool Cek_Respon); |
mauuuuul | 0:a8b6c10f03dc | 131 | void Make_Prediction(int board_number,int baris, int kolom); |
mauuuuul | 0:a8b6c10f03dc | 132 | |
mauuuuul | 0:a8b6c10f03dc | 133 | void First_Update(); |
mauuuuul | 0:a8b6c10f03dc | 134 | vector<string> temp_data; |
mauuuuul | 0:a8b6c10f03dc | 135 | int res_cmp = 0; |
mauuuuul | 0:a8b6c10f03dc | 136 | string bin_string_now; |
mauuuuul | 0:a8b6c10f03dc | 137 | string bin_string_predict; |
mauuuuul | 0:a8b6c10f03dc | 138 | int keys; |
mauuuuul | 0:a8b6c10f03dc | 139 | bool First_Run = true; |
mauuuuul | 0:a8b6c10f03dc | 140 | bool dataButton = false; |
mauuuuul | 0:a8b6c10f03dc | 141 | |
mauuuuul | 0:a8b6c10f03dc | 142 | uint8_t ping_counter1 = 0; |
mauuuuul | 0:a8b6c10f03dc | 143 | uint8_t ping_counter2 = 0; |
mauuuuul | 0:a8b6c10f03dc | 144 | uint8_t ping_counter3 = 0; |
mauuuuul | 0:a8b6c10f03dc | 145 | uint8_t ping_counter4 = 0; |
mauuuuul | 0:a8b6c10f03dc | 146 | |
mauuuuul | 0:a8b6c10f03dc | 147 | void FindingServer() |
mauuuuul | 0:a8b6c10f03dc | 148 | { |
mauuuuul | 0:a8b6c10f03dc | 149 | printf("Finding Server.....\r\n\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 150 | |
mauuuuul | 0:a8b6c10f03dc | 151 | RECONNECT1: |
mauuuuul | 0:a8b6c10f03dc | 152 | EthernetInterface net; |
mauuuuul | 0:a8b6c10f03dc | 153 | net.set_network(IP,MASK,GATEWAY); |
mauuuuul | 0:a8b6c10f03dc | 154 | int stat1 = net.connect(); |
mauuuuul | 0:a8b6c10f03dc | 155 | //printf("stat1 = %d\r\n",stat1); |
mauuuuul | 0:a8b6c10f03dc | 156 | if(stat1 != 0) { |
mauuuuul | 0:a8b6c10f03dc | 157 | goto RECONNECT1; |
mauuuuul | 0:a8b6c10f03dc | 158 | } |
mauuuuul | 0:a8b6c10f03dc | 159 | // Show the network address |
mauuuuul | 0:a8b6c10f03dc | 160 | const char *ip = net.get_ip_address(); |
mauuuuul | 0:a8b6c10f03dc | 161 | |
mauuuuul | 0:a8b6c10f03dc | 162 | while(1) { |
mauuuuul | 0:a8b6c10f03dc | 163 | RECONNECT2: |
mauuuuul | 0:a8b6c10f03dc | 164 | // Open a socket on the network interface, and create a TCP connection to mbed.org |
mauuuuul | 0:a8b6c10f03dc | 165 | TCPSocket socket; |
mauuuuul | 0:a8b6c10f03dc | 166 | socket.open(&net); |
mauuuuul | 0:a8b6c10f03dc | 167 | socket.set_blocking(false); |
mauuuuul | 0:a8b6c10f03dc | 168 | int stat2 = socket.connect(IPSERVER, PORTSERVER); |
mauuuuul | 0:a8b6c10f03dc | 169 | //printf("stat2 = %d\r\n",stat2); |
mauuuuul | 0:a8b6c10f03dc | 170 | wait(1); |
mauuuuul | 0:a8b6c10f03dc | 171 | if(stat2 != 0) { |
mauuuuul | 0:a8b6c10f03dc | 172 | socket.close(); |
mauuuuul | 0:a8b6c10f03dc | 173 | goto RECONNECT2; |
mauuuuul | 0:a8b6c10f03dc | 174 | } |
mauuuuul | 0:a8b6c10f03dc | 175 | watchdog.start(5000); |
mauuuuul | 0:a8b6c10f03dc | 176 | isConnected = true; |
mauuuuul | 0:a8b6c10f03dc | 177 | printf("Connected to Server....\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 178 | |
mauuuuul | 0:a8b6c10f03dc | 179 | if(First_Run) { |
mauuuuul | 0:a8b6c10f03dc | 180 | printf("Process Start..\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 181 | //socket.send(cmd_resets, sizeof(cmd_resets)); |
mauuuuul | 0:a8b6c10f03dc | 182 | |
mauuuuul | 0:a8b6c10f03dc | 183 | First_Update(); |
mauuuuul | 0:a8b6c10f03dc | 184 | |
mauuuuul | 0:a8b6c10f03dc | 185 | for(int i = 0; i<to_sends.size(); i++) { |
mauuuuul | 0:a8b6c10f03dc | 186 | printf("%02X ", to_sends[i]); |
mauuuuul | 0:a8b6c10f03dc | 187 | } |
mauuuuul | 0:a8b6c10f03dc | 188 | |
mauuuuul | 0:a8b6c10f03dc | 189 | socket.send(to_sends.data(), to_sends.size()); |
mauuuuul | 0:a8b6c10f03dc | 190 | First_Run = false; |
mauuuuul | 0:a8b6c10f03dc | 191 | wait(0.05); |
mauuuuul | 0:a8b6c10f03dc | 192 | dsp.step0(); |
mauuuuul | 0:a8b6c10f03dc | 193 | printf("Send First Update Complete\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 194 | } else { |
mauuuuul | 0:a8b6c10f03dc | 195 | if(blinking_now == 0) { |
mauuuuul | 0:a8b6c10f03dc | 196 | //printf("Process Start..\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 197 | //socket.send(cmd_resets, sizeof(cmd_resets)); |
mauuuuul | 0:a8b6c10f03dc | 198 | |
mauuuuul | 0:a8b6c10f03dc | 199 | First_Update(); |
mauuuuul | 0:a8b6c10f03dc | 200 | step = 0; |
mauuuuul | 0:a8b6c10f03dc | 201 | if(frm_data_received.ids == 3) { |
mauuuuul | 0:a8b6c10f03dc | 202 | dsp.step4(to_string(frm_data_received._key_id).c_str(),false); |
mauuuuul | 0:a8b6c10f03dc | 203 | } else if(frm_data_received.ids== 1) { |
mauuuuul | 0:a8b6c10f03dc | 204 | dsp.step0(); |
mauuuuul | 0:a8b6c10f03dc | 205 | } else { |
mauuuuul | 0:a8b6c10f03dc | 206 | dsp.step0(); |
mauuuuul | 0:a8b6c10f03dc | 207 | } |
mauuuuul | 0:a8b6c10f03dc | 208 | //socket.send(reinterpret_cast<char*> (&to_sends[0]), to_sends.size()); |
mauuuuul | 0:a8b6c10f03dc | 209 | socket.send(to_sends.data(), to_sends.size()); |
mauuuuul | 0:a8b6c10f03dc | 210 | |
mauuuuul | 0:a8b6c10f03dc | 211 | } else { |
mauuuuul | 0:a8b6c10f03dc | 212 | dsp.step5(to_string(blinking_now).c_str(),true); |
mauuuuul | 0:a8b6c10f03dc | 213 | } |
mauuuuul | 0:a8b6c10f03dc | 214 | } |
mauuuuul | 0:a8b6c10f03dc | 215 | |
mauuuuul | 0:a8b6c10f03dc | 216 | //printf("%s\r\n",data_frame.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 217 | while(isConnected) { |
mauuuuul | 0:a8b6c10f03dc | 218 | watchdog.kick(); |
mauuuuul | 0:a8b6c10f03dc | 219 | //TODO : CekData Locker Terkini |
mauuuuul | 0:a8b6c10f03dc | 220 | |
mauuuuul | 0:a8b6c10f03dc | 221 | ibutton.DetectiButton(); |
mauuuuul | 0:a8b6c10f03dc | 222 | if(ibutton.IsTaping()) |
mauuuuul | 0:a8b6c10f03dc | 223 | tap = true; |
mauuuuul | 0:a8b6c10f03dc | 224 | else |
mauuuuul | 0:a8b6c10f03dc | 225 | tap = false; |
mauuuuul | 0:a8b6c10f03dc | 226 | |
mauuuuul | 0:a8b6c10f03dc | 227 | if(tap != lasttap) { |
mauuuuul | 0:a8b6c10f03dc | 228 | lasttap = tap; |
mauuuuul | 0:a8b6c10f03dc | 229 | if(tap) { |
mauuuuul | 0:a8b6c10f03dc | 230 | ibutton_code.clear(); |
mauuuuul | 0:a8b6c10f03dc | 231 | ibutton_code = ibutton.GetData(); |
mauuuuul | 0:a8b6c10f03dc | 232 | if((dataButton.length()== 12) && (dataButton == false)){ |
mauuuuul | 0:a8b6c10f03dc | 233 | dataButton = true; |
mauuuuul | 0:a8b6c10f03dc | 234 | printf("[Data Button : %s]\r\n", dataButton.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 235 | } |
mauuuuul | 0:a8b6c10f03dc | 236 | } |
mauuuuul | 0:a8b6c10f03dc | 237 | } |
mauuuuul | 0:a8b6c10f03dc | 238 | |
mauuuuul | 0:a8b6c10f03dc | 239 | switch(step) { |
mauuuuul | 0:a8b6c10f03dc | 240 | case 0: { |
mauuuuul | 0:a8b6c10f03dc | 241 | ping_counter1++; |
mauuuuul | 0:a8b6c10f03dc | 242 | if(ping_counter1 == 10) { |
mauuuuul | 0:a8b6c10f03dc | 243 | int res_cek = socket.send(cek_con,sizeof(cek_con)); |
mauuuuul | 0:a8b6c10f03dc | 244 | ping_counter1 = 0; |
mauuuuul | 0:a8b6c10f03dc | 245 | wait(0.05); |
mauuuuul | 0:a8b6c10f03dc | 246 | if(res_cek <= 0) { |
mauuuuul | 0:a8b6c10f03dc | 247 | printf("Server Has Been Closed..\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 248 | isConnected = false; |
mauuuuul | 0:a8b6c10f03dc | 249 | socket.close(); |
mauuuuul | 0:a8b6c10f03dc | 250 | dsp.init(); |
mauuuuul | 0:a8b6c10f03dc | 251 | |
mauuuuul | 0:a8b6c10f03dc | 252 | continue; |
mauuuuul | 0:a8b6c10f03dc | 253 | } |
mauuuuul | 0:a8b6c10f03dc | 254 | } |
mauuuuul | 0:a8b6c10f03dc | 255 | |
mauuuuul | 0:a8b6c10f03dc | 256 | for(uint8_t i = 0; i<board_sum; i++) { |
mauuuuul | 0:a8b6c10f03dc | 257 | //wait_ms(50); |
mauuuuul | 0:a8b6c10f03dc | 258 | Send_RS485(i,64,real_blink_id[i],true); |
mauuuuul | 0:a8b6c10f03dc | 259 | //printf("%d %d %d %d %d\r\n",real_blink_id_1,real_blink_id_2,real_blink_id_3,real_blink_id_4,real_blink_id_5); |
mauuuuul | 0:a8b6c10f03dc | 260 | |
mauuuuul | 0:a8b6c10f03dc | 261 | for(uint8_t a = 4; a<12; a++) { |
mauuuuul | 0:a8b6c10f03dc | 262 | Row_Board_Now[i][a-4] = respon_from_slave[a]; |
mauuuuul | 0:a8b6c10f03dc | 263 | } |
mauuuuul | 0:a8b6c10f03dc | 264 | } |
mauuuuul | 0:a8b6c10f03dc | 265 | salah_ambil.clear(); |
mauuuuul | 0:a8b6c10f03dc | 266 | salah_taruh.clear(); |
mauuuuul | 0:a8b6c10f03dc | 267 | |
mauuuuul | 0:a8b6c10f03dc | 268 | lst_salah.clear(); |
mauuuuul | 0:a8b6c10f03dc | 269 | to_remove.clear(); |
mauuuuul | 0:a8b6c10f03dc | 270 | blinking_now = 0; |
mauuuuul | 0:a8b6c10f03dc | 271 | for(uint8_t i = 0; i<board_sum; i++) { |
mauuuuul | 0:a8b6c10f03dc | 272 | res_cmp = 0; |
mauuuuul | 0:a8b6c10f03dc | 273 | //printf("Now : "); |
mauuuuul | 0:a8b6c10f03dc | 274 | // for(int v=0; v<(sizeof(Row_Board_Now[i])/sizeof(Row_Board_Now[i][0])); v++) printf("%02X ",Row_Board_Now[i][v]); |
mauuuuul | 0:a8b6c10f03dc | 275 | // printf("\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 276 | // |
mauuuuul | 0:a8b6c10f03dc | 277 | // printf("Last : "); |
mauuuuul | 0:a8b6c10f03dc | 278 | // for(int v=0; v<(sizeof(Row_Board_Last[i])/sizeof(Row_Board_Last[i][0])); v++) printf("%02X ",Row_Board_Last[i][v]); |
mauuuuul | 0:a8b6c10f03dc | 279 | // printf("\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 280 | |
mauuuuul | 0:a8b6c10f03dc | 281 | res_cmp = memcmp(Row_Board_Now[i],Row_Board_Last[i], (sizeof(Row_Board_Now[i])/sizeof(Row_Board_Now[i][0]))); |
mauuuuul | 0:a8b6c10f03dc | 282 | //dbg.printf("memcmp :%d\r\n",res_cmp); |
mauuuuul | 0:a8b6c10f03dc | 283 | if(res_cmp != 0) { |
mauuuuul | 0:a8b6c10f03dc | 284 | for(uint8_t j = 0; j<8; j++) { |
mauuuuul | 0:a8b6c10f03dc | 285 | if(Row_Board_Now[i][j] != Row_Board_Last[i][j]) { |
mauuuuul | 0:a8b6c10f03dc | 286 | |
mauuuuul | 0:a8b6c10f03dc | 287 | keys = 0; |
mauuuuul | 0:a8b6c10f03dc | 288 | bin_string_now = Byte_to_StringBinary(Row_Board_Now[i][j]); |
mauuuuul | 0:a8b6c10f03dc | 289 | bin_string_predict = Byte_to_StringBinary(Row_Board_Last[i][j]); |
mauuuuul | 0:a8b6c10f03dc | 290 | |
mauuuuul | 0:a8b6c10f03dc | 291 | for (uint8_t k = 0; k < 8; k++) { |
mauuuuul | 0:a8b6c10f03dc | 292 | if(bin_string_now[abs(k-7)] != bin_string_predict[abs(k-7)]) { |
mauuuuul | 0:a8b6c10f03dc | 293 | keys = (40*j)+k+(8*i)+1; |
mauuuuul | 0:a8b6c10f03dc | 294 | done[i] = false; |
mauuuuul | 0:a8b6c10f03dc | 295 | lst_salah.push_back(keys); |
mauuuuul | 0:a8b6c10f03dc | 296 | //printf("Salah : %d",keys); |
mauuuuul | 0:a8b6c10f03dc | 297 | if(bin_string_predict[abs(k-7)] == '0') { |
mauuuuul | 0:a8b6c10f03dc | 298 | salah_taruh.push_back(keys); |
mauuuuul | 0:a8b6c10f03dc | 299 | } else { |
mauuuuul | 0:a8b6c10f03dc | 300 | salah_ambil.push_back(keys); |
mauuuuul | 0:a8b6c10f03dc | 301 | } |
mauuuuul | 0:a8b6c10f03dc | 302 | } |
mauuuuul | 0:a8b6c10f03dc | 303 | } |
mauuuuul | 0:a8b6c10f03dc | 304 | } |
mauuuuul | 0:a8b6c10f03dc | 305 | } |
mauuuuul | 0:a8b6c10f03dc | 306 | } else if (res_cmp == 0) { |
mauuuuul | 0:a8b6c10f03dc | 307 | real_blink_id[i] = 64; |
mauuuuul | 0:a8b6c10f03dc | 308 | done[i] = true; |
mauuuuul | 0:a8b6c10f03dc | 309 | |
mauuuuul | 0:a8b6c10f03dc | 310 | //memcpy(Row_Board1_Last,Row_Board1_Now,sizeof(Row_Board1_Last)); |
mauuuuul | 0:a8b6c10f03dc | 311 | |
mauuuuul | 0:a8b6c10f03dc | 312 | Send_RS485(i,64,64,false); |
mauuuuul | 0:a8b6c10f03dc | 313 | } |
mauuuuul | 0:a8b6c10f03dc | 314 | } |
mauuuuul | 0:a8b6c10f03dc | 315 | |
mauuuuul | 0:a8b6c10f03dc | 316 | salah_ambil_count = 0; |
mauuuuul | 0:a8b6c10f03dc | 317 | salah_taruh_count = 0; |
mauuuuul | 0:a8b6c10f03dc | 318 | |
mauuuuul | 0:a8b6c10f03dc | 319 | if(lst_salah.size() > 0) { |
mauuuuul | 0:a8b6c10f03dc | 320 | |
mauuuuul | 0:a8b6c10f03dc | 321 | int blink_id = 64; |
mauuuuul | 0:a8b6c10f03dc | 322 | |
mauuuuul | 0:a8b6c10f03dc | 323 | bool ada = false; |
mauuuuul | 0:a8b6c10f03dc | 324 | |
mauuuuul | 0:a8b6c10f03dc | 325 | if(lst_salah_hst.size() > 0) { |
mauuuuul | 0:a8b6c10f03dc | 326 | //printf("jumlah hst = %d\r\n",lst_salah_hst.size()); |
mauuuuul | 0:a8b6c10f03dc | 327 | for(int q = 0; q<lst_salah_hst.size(); q++) { |
mauuuuul | 0:a8b6c10f03dc | 328 | ada = false; |
mauuuuul | 0:a8b6c10f03dc | 329 | //printf("lst_hst_recent%d = %d\r\n",q+1,lst_salah_hst[q]); |
mauuuuul | 0:a8b6c10f03dc | 330 | |
mauuuuul | 0:a8b6c10f03dc | 331 | for(int r = 0; r<lst_salah.size(); r++) { |
mauuuuul | 0:a8b6c10f03dc | 332 | if(lst_salah[r] == lst_salah_hst[q]) { |
mauuuuul | 0:a8b6c10f03dc | 333 | ada = true; |
mauuuuul | 0:a8b6c10f03dc | 334 | } |
mauuuuul | 0:a8b6c10f03dc | 335 | } |
mauuuuul | 0:a8b6c10f03dc | 336 | if(!ada) { |
mauuuuul | 0:a8b6c10f03dc | 337 | to_remove.push_back(lst_salah_hst[q]); |
mauuuuul | 0:a8b6c10f03dc | 338 | } |
mauuuuul | 0:a8b6c10f03dc | 339 | } |
mauuuuul | 0:a8b6c10f03dc | 340 | |
mauuuuul | 0:a8b6c10f03dc | 341 | if(to_remove.size() > 0) { |
mauuuuul | 0:a8b6c10f03dc | 342 | for(int t = 0; t<to_remove.size(); t++) { |
mauuuuul | 0:a8b6c10f03dc | 343 | lst_salah_hst.erase(remove(lst_salah_hst.begin(), lst_salah_hst.end(), to_remove[t]), lst_salah_hst.end()); |
mauuuuul | 0:a8b6c10f03dc | 344 | } |
mauuuuul | 0:a8b6c10f03dc | 345 | } |
mauuuuul | 0:a8b6c10f03dc | 346 | |
mauuuuul | 0:a8b6c10f03dc | 347 | for(int w = 0; w<lst_salah.size(); w++) { |
mauuuuul | 0:a8b6c10f03dc | 348 | it = find (lst_salah_hst.begin(), lst_salah_hst.end(), lst_salah[w]); |
mauuuuul | 0:a8b6c10f03dc | 349 | if (it != lst_salah_hst.end()) { |
mauuuuul | 0:a8b6c10f03dc | 350 | continue; |
mauuuuul | 0:a8b6c10f03dc | 351 | } |
mauuuuul | 0:a8b6c10f03dc | 352 | lst_salah_hst.push_back(lst_salah[w]); |
mauuuuul | 0:a8b6c10f03dc | 353 | } |
mauuuuul | 0:a8b6c10f03dc | 354 | } else { |
mauuuuul | 0:a8b6c10f03dc | 355 | for(int u = 0; u<lst_salah.size(); u++) { |
mauuuuul | 0:a8b6c10f03dc | 356 | lst_salah_hst.push_back(lst_salah[u]); |
mauuuuul | 0:a8b6c10f03dc | 357 | } |
mauuuuul | 0:a8b6c10f03dc | 358 | |
mauuuuul | 0:a8b6c10f03dc | 359 | } |
mauuuuul | 0:a8b6c10f03dc | 360 | |
mauuuuul | 0:a8b6c10f03dc | 361 | blink_id = lst_salah_hst[lst_salah_hst.size()-1]; |
mauuuuul | 0:a8b6c10f03dc | 362 | |
mauuuuul | 0:a8b6c10f03dc | 363 | int board_number_blink = (blink_id - 1)%40; |
mauuuuul | 0:a8b6c10f03dc | 364 | |
mauuuuul | 0:a8b6c10f03dc | 365 | if(board_number_blink>= 0 && board_number_blink<= 7)board_number_blink = 0; |
mauuuuul | 0:a8b6c10f03dc | 366 | else if(board_number_blink>= 8 && board_number_blink<= 15)board_number_blink = 1; |
mauuuuul | 0:a8b6c10f03dc | 367 | else if(board_number_blink>= 16 && board_number_blink<= 23)board_number_blink = 2; |
mauuuuul | 0:a8b6c10f03dc | 368 | else if(board_number_blink>= 24 && board_number_blink<= 31)board_number_blink = 3; |
mauuuuul | 0:a8b6c10f03dc | 369 | else if(board_number_blink>= 32 && board_number_blink<= 39)board_number_blink = 4; |
mauuuuul | 0:a8b6c10f03dc | 370 | |
mauuuuul | 0:a8b6c10f03dc | 371 | //printf("board_number_blink = %d\r\n",board_number_blink); |
mauuuuul | 0:a8b6c10f03dc | 372 | |
mauuuuul | 0:a8b6c10f03dc | 373 | uint8_t baris_blink = (blink_id - 1)/40; |
mauuuuul | 0:a8b6c10f03dc | 374 | uint8_t kolom_blink = (blink_id - 1)%8; |
mauuuuul | 0:a8b6c10f03dc | 375 | blink_id = board_88[baris_blink][kolom_blink]; |
mauuuuul | 0:a8b6c10f03dc | 376 | |
mauuuuul | 0:a8b6c10f03dc | 377 | //printf("Board blink : %d,Kolom blink : %d, Baris blink : %d\r\n",board_number_blink,kolom_blink,baris_blink); |
mauuuuul | 0:a8b6c10f03dc | 378 | for(int i =0; i<board_sum; i++) { |
mauuuuul | 0:a8b6c10f03dc | 379 | if(board_number_blink == i) { |
mauuuuul | 0:a8b6c10f03dc | 380 | real_blink_id[i] = blink_id; |
mauuuuul | 0:a8b6c10f03dc | 381 | } else { |
mauuuuul | 0:a8b6c10f03dc | 382 | real_blink_id[i] = 64; |
mauuuuul | 0:a8b6c10f03dc | 383 | } |
mauuuuul | 0:a8b6c10f03dc | 384 | } |
mauuuuul | 0:a8b6c10f03dc | 385 | blinking_now = lst_salah_hst[lst_salah_hst.size()-1]; |
mauuuuul | 0:a8b6c10f03dc | 386 | |
mauuuuul | 0:a8b6c10f03dc | 387 | salah_ambil_count = salah_ambil.size(); |
mauuuuul | 0:a8b6c10f03dc | 388 | salah_taruh_count = salah_taruh.size(); |
mauuuuul | 0:a8b6c10f03dc | 389 | to_sends.clear(); |
mauuuuul | 0:a8b6c10f03dc | 390 | |
mauuuuul | 0:a8b6c10f03dc | 391 | to_sends.push_back(0x02); |
mauuuuul | 0:a8b6c10f03dc | 392 | to_sends.push_back((blinking_now>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 393 | to_sends.push_back(blinking_now & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 394 | to_sends.push_back((salah_ambil_count>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 395 | to_sends.push_back(salah_ambil_count & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 396 | to_sends.push_back((salah_taruh_count>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 397 | to_sends.push_back(salah_taruh_count&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 398 | |
mauuuuul | 0:a8b6c10f03dc | 399 | for(int y = 0; y<salah_ambil.size(); y++) { |
mauuuuul | 0:a8b6c10f03dc | 400 | to_sends.push_back((salah_ambil[y] >> 8) & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 401 | to_sends.push_back(salah_ambil[y] & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 402 | } |
mauuuuul | 0:a8b6c10f03dc | 403 | |
mauuuuul | 0:a8b6c10f03dc | 404 | for(int y = 0; y<salah_taruh.size(); y++) { |
mauuuuul | 0:a8b6c10f03dc | 405 | to_sends.push_back((salah_taruh[y] >> 8) & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 406 | to_sends.push_back(salah_taruh[y] & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 407 | } |
mauuuuul | 0:a8b6c10f03dc | 408 | |
mauuuuul | 0:a8b6c10f03dc | 409 | if(to_sends_last != to_sends) { |
mauuuuul | 0:a8b6c10f03dc | 410 | |
mauuuuul | 0:a8b6c10f03dc | 411 | socket.send(to_sends.data(), to_sends.size()); |
mauuuuul | 0:a8b6c10f03dc | 412 | to_sends_last.clear(); |
mauuuuul | 0:a8b6c10f03dc | 413 | copy(to_sends.begin(), to_sends.end(), back_inserter(to_sends_last)); |
mauuuuul | 0:a8b6c10f03dc | 414 | dsp.step5(to_string(blinking_now).c_str(),true); |
mauuuuul | 0:a8b6c10f03dc | 415 | } |
mauuuuul | 0:a8b6c10f03dc | 416 | } else { |
mauuuuul | 0:a8b6c10f03dc | 417 | if(done[0] && done[1] && done[2] && done[3] && done[4]) { |
mauuuuul | 0:a8b6c10f03dc | 418 | lst_salah_hst.clear(); |
mauuuuul | 0:a8b6c10f03dc | 419 | blinking_now = 0; |
mauuuuul | 0:a8b6c10f03dc | 420 | memset(real_blink_id,64,(sizeof(real_blink_id) / sizeof(real_blink_id[0]))); |
mauuuuul | 0:a8b6c10f03dc | 421 | |
mauuuuul | 0:a8b6c10f03dc | 422 | salah_ambil_count = 0; |
mauuuuul | 0:a8b6c10f03dc | 423 | salah_taruh_count = 0; |
mauuuuul | 0:a8b6c10f03dc | 424 | |
mauuuuul | 0:a8b6c10f03dc | 425 | step = 1; |
mauuuuul | 0:a8b6c10f03dc | 426 | |
mauuuuul | 0:a8b6c10f03dc | 427 | to_sends.clear(); |
mauuuuul | 0:a8b6c10f03dc | 428 | to_sends.push_back(0x02); |
mauuuuul | 0:a8b6c10f03dc | 429 | to_sends.push_back(0x00);//blinking now byte1 |
mauuuuul | 0:a8b6c10f03dc | 430 | to_sends.push_back(0x00);//blinking now byte2 |
mauuuuul | 0:a8b6c10f03dc | 431 | to_sends.push_back(0x00);//salah_ambil byte1 |
mauuuuul | 0:a8b6c10f03dc | 432 | to_sends.push_back(0x00);//salah_ambil byte2 |
mauuuuul | 0:a8b6c10f03dc | 433 | to_sends.push_back(0x00);//salah_taruh byte1 |
mauuuuul | 0:a8b6c10f03dc | 434 | to_sends.push_back(0x00);//salah_taruh byte2 |
mauuuuul | 0:a8b6c10f03dc | 435 | |
mauuuuul | 0:a8b6c10f03dc | 436 | if(to_sends_last != to_sends) { |
mauuuuul | 0:a8b6c10f03dc | 437 | socket.send(to_sends.data(), to_sends.size()); |
mauuuuul | 0:a8b6c10f03dc | 438 | to_sends_last.clear(); |
mauuuuul | 0:a8b6c10f03dc | 439 | copy(to_sends.begin(), to_sends.end(), back_inserter(to_sends_last)); |
mauuuuul | 0:a8b6c10f03dc | 440 | |
mauuuuul | 0:a8b6c10f03dc | 441 | } |
mauuuuul | 0:a8b6c10f03dc | 442 | if(frm_data_received.ids == 3) { |
mauuuuul | 0:a8b6c10f03dc | 443 | dsp.step4(to_string(frm_data_received._key_id).c_str(),false); |
mauuuuul | 0:a8b6c10f03dc | 444 | } else if(frm_data_received.ids== 1) { |
mauuuuul | 0:a8b6c10f03dc | 445 | dsp.step0(); |
mauuuuul | 0:a8b6c10f03dc | 446 | } else { |
mauuuuul | 0:a8b6c10f03dc | 447 | dsp.step0(); |
mauuuuul | 0:a8b6c10f03dc | 448 | } |
mauuuuul | 0:a8b6c10f03dc | 449 | //wait_ms(50); |
mauuuuul | 0:a8b6c10f03dc | 450 | dsp.step5(to_string(blinking_now).c_str(),true); |
mauuuuul | 0:a8b6c10f03dc | 451 | |
mauuuuul | 0:a8b6c10f03dc | 452 | } else { |
mauuuuul | 0:a8b6c10f03dc | 453 | blinking_now = 0; |
mauuuuul | 0:a8b6c10f03dc | 454 | memset(real_blink_id,64,(sizeof(real_blink_id) / sizeof(real_blink_id[0]))); |
mauuuuul | 0:a8b6c10f03dc | 455 | } |
mauuuuul | 0:a8b6c10f03dc | 456 | } |
mauuuuul | 0:a8b6c10f03dc | 457 | } |
mauuuuul | 0:a8b6c10f03dc | 458 | break; |
mauuuuul | 0:a8b6c10f03dc | 459 | |
mauuuuul | 0:a8b6c10f03dc | 460 | case 1: { |
mauuuuul | 0:a8b6c10f03dc | 461 | char rbuffer[64]; |
mauuuuul | 0:a8b6c10f03dc | 462 | |
mauuuuul | 0:a8b6c10f03dc | 463 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
mauuuuul | 0:a8b6c10f03dc | 464 | if(rcount < 0) { |
mauuuuul | 0:a8b6c10f03dc | 465 | step = 0; |
mauuuuul | 0:a8b6c10f03dc | 466 | } else if(rcount > 0) { |
mauuuuul | 0:a8b6c10f03dc | 467 | rbuffer[rcount] = '\0'; |
mauuuuul | 0:a8b6c10f03dc | 468 | |
mauuuuul | 0:a8b6c10f03dc | 469 | string data; |
mauuuuul | 0:a8b6c10f03dc | 470 | |
mauuuuul | 0:a8b6c10f03dc | 471 | for(int i = 0; i<sizeof(rbuffer); i++) { |
mauuuuul | 0:a8b6c10f03dc | 472 | if(rbuffer[i] == '\0')break; |
mauuuuul | 0:a8b6c10f03dc | 473 | data += rbuffer[i]; |
mauuuuul | 0:a8b6c10f03dc | 474 | } |
mauuuuul | 0:a8b6c10f03dc | 475 | |
mauuuuul | 0:a8b6c10f03dc | 476 | vector<string> res = split(data, "~"); |
mauuuuul | 0:a8b6c10f03dc | 477 | |
mauuuuul | 0:a8b6c10f03dc | 478 | if(sizeof(res) != 0) { |
mauuuuul | 0:a8b6c10f03dc | 479 | temp_data = split(res[0], ";"); |
mauuuuul | 0:a8b6c10f03dc | 480 | |
mauuuuul | 0:a8b6c10f03dc | 481 | if(temp_data[0] == "3" && temp_data[1] == "1") { |
mauuuuul | 0:a8b6c10f03dc | 482 | dsp.step4(to_string(frm_data_received._key_id).c_str(),false); |
mauuuuul | 0:a8b6c10f03dc | 483 | } else if(temp_data[0] == "3" && temp_data[1] == "0") { |
mauuuuul | 0:a8b6c10f03dc | 484 | dsp.step0(); |
mauuuuul | 0:a8b6c10f03dc | 485 | } else { |
mauuuuul | 0:a8b6c10f03dc | 486 | frm_data_received.ids = atoi(temp_data[0].c_str()); |
mauuuuul | 0:a8b6c10f03dc | 487 | frm_data_received.ibutton = temp_data[1]; |
mauuuuul | 0:a8b6c10f03dc | 488 | frm_data_received._key_id = atoi(temp_data[2].c_str()); |
mauuuuul | 0:a8b6c10f03dc | 489 | frm_data_received.cmd_id = atoi(temp_data[3].c_str()); |
mauuuuul | 0:a8b6c10f03dc | 490 | |
mauuuuul | 0:a8b6c10f03dc | 491 | |
mauuuuul | 0:a8b6c10f03dc | 492 | printf("id = %d\r\n",frm_data_received.ids); |
mauuuuul | 0:a8b6c10f03dc | 493 | printf("ibutton = %s\r\n",frm_data_received.ibutton.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 494 | printf("keys = %d\r\n",frm_data_received._key_id); |
mauuuuul | 0:a8b6c10f03dc | 495 | printf("cmd = %d\r\n",frm_data_received.cmd_id); |
mauuuuul | 0:a8b6c10f03dc | 496 | |
mauuuuul | 0:a8b6c10f03dc | 497 | if(frm_data_received.ids== 1) { |
mauuuuul | 0:a8b6c10f03dc | 498 | wait(1); |
mauuuuul | 0:a8b6c10f03dc | 499 | dsp.step1(true); |
mauuuuul | 0:a8b6c10f03dc | 500 | step = 2; |
mauuuuul | 0:a8b6c10f03dc | 501 | } else if(frm_data_received.ids == 3) { |
mauuuuul | 0:a8b6c10f03dc | 502 | step = 3; |
mauuuuul | 0:a8b6c10f03dc | 503 | |
mauuuuul | 0:a8b6c10f03dc | 504 | //dsp.step4(to_string(frm_data_received._key_id).c_str(),true); |
mauuuuul | 0:a8b6c10f03dc | 505 | } |
mauuuuul | 0:a8b6c10f03dc | 506 | } |
mauuuuul | 0:a8b6c10f03dc | 507 | } |
mauuuuul | 0:a8b6c10f03dc | 508 | } else if(rcount == 0) { |
mauuuuul | 0:a8b6c10f03dc | 509 | printf("Server Has Been Closed..\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 510 | |
mauuuuul | 0:a8b6c10f03dc | 511 | isConnected = false; |
mauuuuul | 0:a8b6c10f03dc | 512 | step = 0;// |
mauuuuul | 0:a8b6c10f03dc | 513 | // blinking_now = 0; |
mauuuuul | 0:a8b6c10f03dc | 514 | // real_blink_id_1 = 64; |
mauuuuul | 0:a8b6c10f03dc | 515 | // real_blink_id_2 = 64; |
mauuuuul | 0:a8b6c10f03dc | 516 | // real_blink_id_3 = 64; |
mauuuuul | 0:a8b6c10f03dc | 517 | // real_blink_id_4 = 64; |
mauuuuul | 0:a8b6c10f03dc | 518 | // real_blink_id_5 = 64; |
mauuuuul | 0:a8b6c10f03dc | 519 | dsp.init(); |
mauuuuul | 0:a8b6c10f03dc | 520 | socket.close(); |
mauuuuul | 0:a8b6c10f03dc | 521 | } |
mauuuuul | 0:a8b6c10f03dc | 522 | |
mauuuuul | 0:a8b6c10f03dc | 523 | if(ibutton_code != "") { |
mauuuuul | 0:a8b6c10f03dc | 524 | string data_to_send = "5;" + ibutton_code + ";~"; |
mauuuuul | 0:a8b6c10f03dc | 525 | printf("%s\r\n",data_to_send.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 526 | wait(1); |
mauuuuul | 0:a8b6c10f03dc | 527 | |
mauuuuul | 0:a8b6c10f03dc | 528 | socket.send(data_to_send.c_str(), data_to_send.length()); |
mauuuuul | 0:a8b6c10f03dc | 529 | printf("Send ibutton\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 530 | |
mauuuuul | 0:a8b6c10f03dc | 531 | ibutton_code.clear(); |
mauuuuul | 0:a8b6c10f03dc | 532 | } |
mauuuuul | 0:a8b6c10f03dc | 533 | } |
mauuuuul | 0:a8b6c10f03dc | 534 | break; |
mauuuuul | 0:a8b6c10f03dc | 535 | |
mauuuuul | 0:a8b6c10f03dc | 536 | case 2: { |
mauuuuul | 0:a8b6c10f03dc | 537 | buzz.play(1); |
mauuuuul | 0:a8b6c10f03dc | 538 | bool restart = false; |
mauuuuul | 0:a8b6c10f03dc | 539 | while(frm_data_received.ibutton.c_str() != ibutton_code) { |
mauuuuul | 0:a8b6c10f03dc | 540 | wait(0.05); |
mauuuuul | 0:a8b6c10f03dc | 541 | ping_counter2++; |
mauuuuul | 0:a8b6c10f03dc | 542 | |
mauuuuul | 0:a8b6c10f03dc | 543 | if(ping_counter2==25) { |
mauuuuul | 0:a8b6c10f03dc | 544 | int res_cek = socket.send(cek_con,sizeof(cek_con)); |
mauuuuul | 0:a8b6c10f03dc | 545 | ping_counter2 = 0; |
mauuuuul | 0:a8b6c10f03dc | 546 | wait(0.05); |
mauuuuul | 0:a8b6c10f03dc | 547 | if(res_cek <= 0) { |
mauuuuul | 0:a8b6c10f03dc | 548 | printf("Server Has Been Closed..\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 549 | isConnected = false; |
mauuuuul | 0:a8b6c10f03dc | 550 | socket.close(); |
mauuuuul | 0:a8b6c10f03dc | 551 | dsp.init(); |
mauuuuul | 0:a8b6c10f03dc | 552 | restart = true; |
mauuuuul | 0:a8b6c10f03dc | 553 | |
mauuuuul | 0:a8b6c10f03dc | 554 | break; |
mauuuuul | 0:a8b6c10f03dc | 555 | } |
mauuuuul | 0:a8b6c10f03dc | 556 | } |
mauuuuul | 0:a8b6c10f03dc | 557 | } |
mauuuuul | 0:a8b6c10f03dc | 558 | if(restart) { |
mauuuuul | 0:a8b6c10f03dc | 559 | continue; |
mauuuuul | 0:a8b6c10f03dc | 560 | } else { |
mauuuuul | 0:a8b6c10f03dc | 561 | ibutton_code.clear(); |
mauuuuul | 0:a8b6c10f03dc | 562 | printf("IButton verified\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 563 | dsp.step2(to_string(frm_data_received._key_id).c_str()); |
mauuuuul | 0:a8b6c10f03dc | 564 | step = 3; |
mauuuuul | 0:a8b6c10f03dc | 565 | |
mauuuuul | 0:a8b6c10f03dc | 566 | } |
mauuuuul | 0:a8b6c10f03dc | 567 | } |
mauuuuul | 0:a8b6c10f03dc | 568 | break; |
mauuuuul | 0:a8b6c10f03dc | 569 | |
mauuuuul | 0:a8b6c10f03dc | 570 | case 3: { |
mauuuuul | 0:a8b6c10f03dc | 571 | ping_counter3++; |
mauuuuul | 0:a8b6c10f03dc | 572 | |
mauuuuul | 0:a8b6c10f03dc | 573 | if(ping_counter3 == 10) { |
mauuuuul | 0:a8b6c10f03dc | 574 | int res_cek = socket.send(cek_con,sizeof(cek_con)); |
mauuuuul | 0:a8b6c10f03dc | 575 | ping_counter3 = 0; |
mauuuuul | 0:a8b6c10f03dc | 576 | wait(0.05); |
mauuuuul | 0:a8b6c10f03dc | 577 | if(res_cek <= 0) { |
mauuuuul | 0:a8b6c10f03dc | 578 | printf("Server Has Been Closed..\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 579 | isConnected = false; |
mauuuuul | 0:a8b6c10f03dc | 580 | socket.close(); |
mauuuuul | 0:a8b6c10f03dc | 581 | dsp.init(); |
mauuuuul | 0:a8b6c10f03dc | 582 | continue; |
mauuuuul | 0:a8b6c10f03dc | 583 | } |
mauuuuul | 0:a8b6c10f03dc | 584 | } |
mauuuuul | 0:a8b6c10f03dc | 585 | |
mauuuuul | 0:a8b6c10f03dc | 586 | buzz.play(1); |
mauuuuul | 0:a8b6c10f03dc | 587 | |
mauuuuul | 0:a8b6c10f03dc | 588 | board_number = (frm_data_received._key_id - 1)%40; |
mauuuuul | 0:a8b6c10f03dc | 589 | |
mauuuuul | 0:a8b6c10f03dc | 590 | if(board_number>= 0 && board_number<= 7)board_number = 0; |
mauuuuul | 0:a8b6c10f03dc | 591 | if(board_number>= 8 && board_number<= 15)board_number = 1; |
mauuuuul | 0:a8b6c10f03dc | 592 | if(board_number>= 16 && board_number<= 23)board_number = 2; |
mauuuuul | 0:a8b6c10f03dc | 593 | if(board_number>= 24 && board_number<= 31)board_number = 3; |
mauuuuul | 0:a8b6c10f03dc | 594 | if(board_number>= 32 && board_number<= 39)board_number = 4; |
mauuuuul | 0:a8b6c10f03dc | 595 | printf("KEY[board] = %d\r\n",board_number); |
mauuuuul | 0:a8b6c10f03dc | 596 | baris = (frm_data_received._key_id - 1)/40; |
mauuuuul | 0:a8b6c10f03dc | 597 | printf("KEY[baris] = %d\r\n",baris); |
mauuuuul | 0:a8b6c10f03dc | 598 | kolom = (frm_data_received._key_id - 1)%8; |
mauuuuul | 0:a8b6c10f03dc | 599 | printf("KEY[kolom] = %d\r\n",kolom); |
mauuuuul | 0:a8b6c10f03dc | 600 | idle_wait_result = 0; |
mauuuuul | 0:a8b6c10f03dc | 601 | real_id = board_88[baris][kolom]; |
mauuuuul | 0:a8b6c10f03dc | 602 | printf("Real Id = %d\r\n",real_id); |
mauuuuul | 0:a8b6c10f03dc | 603 | |
mauuuuul | 0:a8b6c10f03dc | 604 | Make_Prediction(board_number,baris,kolom); |
mauuuuul | 0:a8b6c10f03dc | 605 | |
mauuuuul | 0:a8b6c10f03dc | 606 | step = 4; |
mauuuuul | 0:a8b6c10f03dc | 607 | |
mauuuuul | 0:a8b6c10f03dc | 608 | } |
mauuuuul | 0:a8b6c10f03dc | 609 | break; |
mauuuuul | 0:a8b6c10f03dc | 610 | |
mauuuuul | 0:a8b6c10f03dc | 611 | case 4: { |
mauuuuul | 0:a8b6c10f03dc | 612 | ping_counter4++; |
mauuuuul | 0:a8b6c10f03dc | 613 | |
mauuuuul | 0:a8b6c10f03dc | 614 | if(ping_counter4 == 10) { |
mauuuuul | 0:a8b6c10f03dc | 615 | int res_cek = socket.send(cek_con,sizeof(cek_con)); |
mauuuuul | 0:a8b6c10f03dc | 616 | ping_counter4 = 0; |
mauuuuul | 0:a8b6c10f03dc | 617 | wait(0.05); |
mauuuuul | 0:a8b6c10f03dc | 618 | if(res_cek <= 0) { |
mauuuuul | 0:a8b6c10f03dc | 619 | LCD_Step_4 = 0; |
mauuuuul | 0:a8b6c10f03dc | 620 | printf("Server Has Been Closed..\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 621 | isConnected = false; |
mauuuuul | 0:a8b6c10f03dc | 622 | socket.close(); |
mauuuuul | 0:a8b6c10f03dc | 623 | dsp.init(); |
mauuuuul | 0:a8b6c10f03dc | 624 | continue; |
mauuuuul | 0:a8b6c10f03dc | 625 | } |
mauuuuul | 0:a8b6c10f03dc | 626 | |
mauuuuul | 0:a8b6c10f03dc | 627 | } |
mauuuuul | 0:a8b6c10f03dc | 628 | |
mauuuuul | 0:a8b6c10f03dc | 629 | for(uint8_t i = 0; i<board_sum; i++) { |
mauuuuul | 0:a8b6c10f03dc | 630 | wait_ms(100); |
mauuuuul | 0:a8b6c10f03dc | 631 | if(board_number == i) { |
mauuuuul | 0:a8b6c10f03dc | 632 | Send_RS485(i,real_id,real_blink_id[i],true); |
mauuuuul | 0:a8b6c10f03dc | 633 | } else { |
mauuuuul | 0:a8b6c10f03dc | 634 | Send_RS485(i,64,real_blink_id[i],true); |
mauuuuul | 0:a8b6c10f03dc | 635 | } |
mauuuuul | 0:a8b6c10f03dc | 636 | |
mauuuuul | 0:a8b6c10f03dc | 637 | printf("Data has been received successfully\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 638 | for(uint8_t a = 4; a<12; a++) { |
mauuuuul | 0:a8b6c10f03dc | 639 | Row_Board_Now[i][a-4] = respon_from_slave[a]; |
mauuuuul | 0:a8b6c10f03dc | 640 | //printf("Resp : %02X || BoardLast %d : %02X\r\n",respon_from_slave[a],i+1,Row_Board2_Last[temp_id-5]); |
mauuuuul | 0:a8b6c10f03dc | 641 | } |
mauuuuul | 0:a8b6c10f03dc | 642 | } |
mauuuuul | 0:a8b6c10f03dc | 643 | |
mauuuuul | 0:a8b6c10f03dc | 644 | salah_ambil.clear(); |
mauuuuul | 0:a8b6c10f03dc | 645 | salah_taruh.clear(); |
mauuuuul | 0:a8b6c10f03dc | 646 | |
mauuuuul | 0:a8b6c10f03dc | 647 | lst_salah.clear(); |
mauuuuul | 0:a8b6c10f03dc | 648 | |
mauuuuul | 0:a8b6c10f03dc | 649 | for(uint8_t i = 0; i<board_sum; i++) { |
mauuuuul | 0:a8b6c10f03dc | 650 | res_cmp = 0; |
mauuuuul | 0:a8b6c10f03dc | 651 | res_cmp = memcmp(Row_Board_Now[i],Row_Board_Predict[i], (sizeof(Row_Board_Now[i])/sizeof(Row_Board_Now[i][0]))); |
mauuuuul | 0:a8b6c10f03dc | 652 | if(res_cmp != 0) { |
mauuuuul | 0:a8b6c10f03dc | 653 | for(uint8_t j = 0; j<8; j++) { |
mauuuuul | 0:a8b6c10f03dc | 654 | if(Row_Board_Now[i][j] != Row_Board_Predict[i][j]) { |
mauuuuul | 0:a8b6c10f03dc | 655 | keys = 0; |
mauuuuul | 0:a8b6c10f03dc | 656 | bin_string_now = Byte_to_StringBinary(Row_Board_Now[i][j]); |
mauuuuul | 0:a8b6c10f03dc | 657 | bin_string_predict = Byte_to_StringBinary(Row_Board_Predict[i][j]); |
mauuuuul | 0:a8b6c10f03dc | 658 | |
mauuuuul | 0:a8b6c10f03dc | 659 | for (uint8_t k = 0; k < 8; k++) { |
mauuuuul | 0:a8b6c10f03dc | 660 | if(bin_string_now[abs(k-7)] != bin_string_predict[abs(k-7)]) { |
mauuuuul | 0:a8b6c10f03dc | 661 | keys = (40*j)+k+(8*i)+1; |
mauuuuul | 0:a8b6c10f03dc | 662 | if(keys != frm_data_received._key_id) { |
mauuuuul | 0:a8b6c10f03dc | 663 | lst_salah.push_back(keys); |
mauuuuul | 0:a8b6c10f03dc | 664 | if(bin_string_predict[abs(k-7)] == '0') { |
mauuuuul | 0:a8b6c10f03dc | 665 | salah_taruh.push_back(keys); |
mauuuuul | 0:a8b6c10f03dc | 666 | } else if(bin_string_predict[abs(k-7)] == '1') { |
mauuuuul | 0:a8b6c10f03dc | 667 | salah_ambil.push_back(keys); |
mauuuuul | 0:a8b6c10f03dc | 668 | } |
mauuuuul | 0:a8b6c10f03dc | 669 | } else { |
mauuuuul | 0:a8b6c10f03dc | 670 | done[i] = false; |
mauuuuul | 0:a8b6c10f03dc | 671 | } |
mauuuuul | 0:a8b6c10f03dc | 672 | } |
mauuuuul | 0:a8b6c10f03dc | 673 | } |
mauuuuul | 0:a8b6c10f03dc | 674 | } |
mauuuuul | 0:a8b6c10f03dc | 675 | } |
mauuuuul | 0:a8b6c10f03dc | 676 | } else if (res_cmp == 0) { |
mauuuuul | 0:a8b6c10f03dc | 677 | printf("Board %d Match\r\n",i+1); |
mauuuuul | 0:a8b6c10f03dc | 678 | real_blink_id[i] = 64; |
mauuuuul | 0:a8b6c10f03dc | 679 | done[i] = true; |
mauuuuul | 0:a8b6c10f03dc | 680 | |
mauuuuul | 0:a8b6c10f03dc | 681 | memcpy(Row_Board_Last[i],Row_Board_Now[i],(sizeof(Row_Board_Last[i])/sizeof(Row_Board_Last[i][0]))); |
mauuuuul | 0:a8b6c10f03dc | 682 | |
mauuuuul | 0:a8b6c10f03dc | 683 | Send_RS485(i,64,64,false); |
mauuuuul | 0:a8b6c10f03dc | 684 | } |
mauuuuul | 0:a8b6c10f03dc | 685 | } |
mauuuuul | 0:a8b6c10f03dc | 686 | |
mauuuuul | 0:a8b6c10f03dc | 687 | salah_ambil_count = 0; |
mauuuuul | 0:a8b6c10f03dc | 688 | salah_taruh_count = 0; |
mauuuuul | 0:a8b6c10f03dc | 689 | |
mauuuuul | 0:a8b6c10f03dc | 690 | //printf("Size salah = %d\r\n",lst_salah.size()); |
mauuuuul | 0:a8b6c10f03dc | 691 | if(lst_salah.size() > 0) { |
mauuuuul | 0:a8b6c10f03dc | 692 | int blink_id = 64; |
mauuuuul | 0:a8b6c10f03dc | 693 | |
mauuuuul | 0:a8b6c10f03dc | 694 | bool ada = false; |
mauuuuul | 0:a8b6c10f03dc | 695 | //for(int p = 0; p<lst_salah.size(); p++) printf("lst salah all%d = %d\r\n",p+1,lst_salah[p]); |
mauuuuul | 0:a8b6c10f03dc | 696 | |
mauuuuul | 0:a8b6c10f03dc | 697 | if(lst_salah_hst.size() != 0) { |
mauuuuul | 0:a8b6c10f03dc | 698 | |
mauuuuul | 0:a8b6c10f03dc | 699 | //printf("jumlah hst = %d\r\n",lst_salah_hst.size()); |
mauuuuul | 0:a8b6c10f03dc | 700 | for(int q = 0; q<lst_salah_hst.size(); q++) { |
mauuuuul | 0:a8b6c10f03dc | 701 | ada = false; |
mauuuuul | 0:a8b6c10f03dc | 702 | |
mauuuuul | 0:a8b6c10f03dc | 703 | for(int r = 0; r<lst_salah.size(); r++) { |
mauuuuul | 0:a8b6c10f03dc | 704 | if(lst_salah[r] == lst_salah_hst[q]) { |
mauuuuul | 0:a8b6c10f03dc | 705 | ada = true; |
mauuuuul | 0:a8b6c10f03dc | 706 | //break; |
mauuuuul | 0:a8b6c10f03dc | 707 | } |
mauuuuul | 0:a8b6c10f03dc | 708 | } |
mauuuuul | 0:a8b6c10f03dc | 709 | if(!ada) { |
mauuuuul | 0:a8b6c10f03dc | 710 | to_remove.push_back(lst_salah_hst[q]); |
mauuuuul | 0:a8b6c10f03dc | 711 | } |
mauuuuul | 0:a8b6c10f03dc | 712 | } |
mauuuuul | 0:a8b6c10f03dc | 713 | |
mauuuuul | 0:a8b6c10f03dc | 714 | if(to_remove.size() > 0) { |
mauuuuul | 0:a8b6c10f03dc | 715 | for(int t = 0; t<to_remove.size(); t++) { |
mauuuuul | 0:a8b6c10f03dc | 716 | lst_salah_hst.erase(remove(lst_salah_hst.begin(), lst_salah_hst.end(), to_remove[t]), lst_salah_hst.end()); |
mauuuuul | 0:a8b6c10f03dc | 717 | } |
mauuuuul | 0:a8b6c10f03dc | 718 | } |
mauuuuul | 0:a8b6c10f03dc | 719 | |
mauuuuul | 0:a8b6c10f03dc | 720 | for(int w = 0; w<lst_salah.size(); w++) { |
mauuuuul | 0:a8b6c10f03dc | 721 | it = find (lst_salah_hst.begin(), lst_salah_hst.end(), lst_salah[w]); |
mauuuuul | 0:a8b6c10f03dc | 722 | if (it != lst_salah_hst.end()) { |
mauuuuul | 0:a8b6c10f03dc | 723 | continue; |
mauuuuul | 0:a8b6c10f03dc | 724 | } |
mauuuuul | 0:a8b6c10f03dc | 725 | lst_salah_hst.push_back(lst_salah[w]); |
mauuuuul | 0:a8b6c10f03dc | 726 | } |
mauuuuul | 0:a8b6c10f03dc | 727 | } else { |
mauuuuul | 0:a8b6c10f03dc | 728 | |
mauuuuul | 0:a8b6c10f03dc | 729 | for(int u = 0; u<lst_salah.size(); u++) { |
mauuuuul | 0:a8b6c10f03dc | 730 | lst_salah_hst.push_back(lst_salah[u]); |
mauuuuul | 0:a8b6c10f03dc | 731 | } |
mauuuuul | 0:a8b6c10f03dc | 732 | } |
mauuuuul | 0:a8b6c10f03dc | 733 | |
mauuuuul | 0:a8b6c10f03dc | 734 | blink_id = lst_salah_hst[lst_salah_hst.size()-1]; |
mauuuuul | 0:a8b6c10f03dc | 735 | //printf("blink_id = %d\r\n",blink_id); |
mauuuuul | 0:a8b6c10f03dc | 736 | |
mauuuuul | 0:a8b6c10f03dc | 737 | int board_number_blink = (blink_id - 1)%40; |
mauuuuul | 0:a8b6c10f03dc | 738 | |
mauuuuul | 0:a8b6c10f03dc | 739 | if(board_number_blink>= 0 && board_number_blink<= 7)board_number_blink = 0; |
mauuuuul | 0:a8b6c10f03dc | 740 | else if(board_number_blink>= 8 && board_number_blink<= 15)board_number_blink = 1; |
mauuuuul | 0:a8b6c10f03dc | 741 | else if(board_number_blink>= 16 && board_number_blink<= 23)board_number_blink = 2; |
mauuuuul | 0:a8b6c10f03dc | 742 | else if(board_number_blink>= 24 && board_number_blink<= 31)board_number_blink = 3; |
mauuuuul | 0:a8b6c10f03dc | 743 | else if(board_number_blink>= 32 && board_number_blink<= 39)board_number_blink = 4; |
mauuuuul | 0:a8b6c10f03dc | 744 | |
mauuuuul | 0:a8b6c10f03dc | 745 | //printf("board_number_blink = %d\r\n",board_number_blink); |
mauuuuul | 0:a8b6c10f03dc | 746 | |
mauuuuul | 0:a8b6c10f03dc | 747 | int baris_blink = (blink_id - 1)/40; |
mauuuuul | 0:a8b6c10f03dc | 748 | int kolom_blink = (blink_id - 1)%8; |
mauuuuul | 0:a8b6c10f03dc | 749 | |
mauuuuul | 0:a8b6c10f03dc | 750 | blink_id = board_88[baris_blink][kolom_blink]; |
mauuuuul | 0:a8b6c10f03dc | 751 | |
mauuuuul | 0:a8b6c10f03dc | 752 | //printf("Board blink : %d,Kolom blink : %d, Baris blink : %d\r\n",board_number_blink,kolom_blink,baris_blink); |
mauuuuul | 0:a8b6c10f03dc | 753 | for(int i =0; i<board_sum; i++) { |
mauuuuul | 0:a8b6c10f03dc | 754 | if(board_number_blink == i) { |
mauuuuul | 0:a8b6c10f03dc | 755 | real_blink_id[i] = blink_id; |
mauuuuul | 0:a8b6c10f03dc | 756 | } else { |
mauuuuul | 0:a8b6c10f03dc | 757 | real_blink_id[i] = 64; |
mauuuuul | 0:a8b6c10f03dc | 758 | } |
mauuuuul | 0:a8b6c10f03dc | 759 | } |
mauuuuul | 0:a8b6c10f03dc | 760 | |
mauuuuul | 0:a8b6c10f03dc | 761 | blinking_now = lst_salah_hst[lst_salah_hst.size()-1]; |
mauuuuul | 0:a8b6c10f03dc | 762 | |
mauuuuul | 0:a8b6c10f03dc | 763 | salah_ambil_count = salah_ambil.size(); |
mauuuuul | 0:a8b6c10f03dc | 764 | salah_taruh_count = salah_taruh.size(); |
mauuuuul | 0:a8b6c10f03dc | 765 | to_sends.clear(); |
mauuuuul | 0:a8b6c10f03dc | 766 | to_sends.push_back(frm_data_received.ids); |
mauuuuul | 0:a8b6c10f03dc | 767 | to_sends.push_back((blinking_now>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 768 | to_sends.push_back(blinking_now & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 769 | to_sends.push_back((salah_ambil_count>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 770 | to_sends.push_back(salah_ambil_count & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 771 | to_sends.push_back((salah_taruh_count>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 772 | to_sends.push_back((salah_taruh_count>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 773 | |
mauuuuul | 0:a8b6c10f03dc | 774 | for(int y = 0; y<salah_ambil.size(); y++) { |
mauuuuul | 0:a8b6c10f03dc | 775 | to_sends.push_back((salah_ambil[y] >> 8) & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 776 | to_sends.push_back(salah_ambil[y] & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 777 | } |
mauuuuul | 0:a8b6c10f03dc | 778 | |
mauuuuul | 0:a8b6c10f03dc | 779 | for(int y = 0; y<salah_taruh.size(); y++) { |
mauuuuul | 0:a8b6c10f03dc | 780 | to_sends.push_back((salah_taruh[y] >> 8) & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 781 | to_sends.push_back(salah_taruh[y] & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 782 | } |
mauuuuul | 0:a8b6c10f03dc | 783 | |
mauuuuul | 0:a8b6c10f03dc | 784 | if(to_sends_last != to_sends) { |
mauuuuul | 0:a8b6c10f03dc | 785 | socket.send(to_sends.data(), to_sends.size()); |
mauuuuul | 0:a8b6c10f03dc | 786 | to_sends_last.clear(); |
mauuuuul | 0:a8b6c10f03dc | 787 | copy(to_sends.begin(), to_sends.end(), back_inserter(to_sends_last)); |
mauuuuul | 0:a8b6c10f03dc | 788 | |
mauuuuul | 0:a8b6c10f03dc | 789 | dsp.step5(to_string(blinking_now).c_str(),true); |
mauuuuul | 0:a8b6c10f03dc | 790 | |
mauuuuul | 0:a8b6c10f03dc | 791 | LCD_Step_4 = 0; |
mauuuuul | 0:a8b6c10f03dc | 792 | } |
mauuuuul | 0:a8b6c10f03dc | 793 | } else { |
mauuuuul | 0:a8b6c10f03dc | 794 | if(done[0] && done[1] && done[2] && done[3] && done[4]) { |
mauuuuul | 0:a8b6c10f03dc | 795 | lst_salah_hst.clear(); |
mauuuuul | 0:a8b6c10f03dc | 796 | blinking_now = 0; |
mauuuuul | 0:a8b6c10f03dc | 797 | memset(real_blink_id,64,(sizeof(real_blink_id) / sizeof(real_blink_id[0]))); |
mauuuuul | 0:a8b6c10f03dc | 798 | |
mauuuuul | 0:a8b6c10f03dc | 799 | salah_ambil_count = 0; |
mauuuuul | 0:a8b6c10f03dc | 800 | salah_taruh_count = 0; |
mauuuuul | 0:a8b6c10f03dc | 801 | |
mauuuuul | 0:a8b6c10f03dc | 802 | ibutton_code.clear(); |
mauuuuul | 0:a8b6c10f03dc | 803 | |
mauuuuul | 0:a8b6c10f03dc | 804 | to_sends.clear(); |
mauuuuul | 0:a8b6c10f03dc | 805 | to_sends.push_back(frm_data_received.ids); |
mauuuuul | 0:a8b6c10f03dc | 806 | to_sends.push_back(0x00);//blinking_now byte1 |
mauuuuul | 0:a8b6c10f03dc | 807 | to_sends.push_back(0x00);//blinking_now byte2 |
mauuuuul | 0:a8b6c10f03dc | 808 | to_sends.push_back(0x00);//salah_ambil byte1 |
mauuuuul | 0:a8b6c10f03dc | 809 | to_sends.push_back(0x00);//salah_ambil byte2 |
mauuuuul | 0:a8b6c10f03dc | 810 | to_sends.push_back(0x00);//salah_taruh byte1 |
mauuuuul | 0:a8b6c10f03dc | 811 | to_sends.push_back(0x00);//salah_taruh byte2 |
mauuuuul | 0:a8b6c10f03dc | 812 | |
mauuuuul | 0:a8b6c10f03dc | 813 | if(to_sends_last != to_sends) { |
mauuuuul | 0:a8b6c10f03dc | 814 | socket.send(to_sends.data(), to_sends.size()); |
mauuuuul | 0:a8b6c10f03dc | 815 | to_sends_last.clear(); |
mauuuuul | 0:a8b6c10f03dc | 816 | copy(to_sends.begin(), to_sends.end(), back_inserter(to_sends_last)); |
mauuuuul | 0:a8b6c10f03dc | 817 | } |
mauuuuul | 0:a8b6c10f03dc | 818 | ibutton_code.clear(); |
mauuuuul | 0:a8b6c10f03dc | 819 | to_sends.clear(); |
mauuuuul | 0:a8b6c10f03dc | 820 | LCD_Step_4 = 0; |
mauuuuul | 0:a8b6c10f03dc | 821 | |
mauuuuul | 0:a8b6c10f03dc | 822 | step = 0; |
mauuuuul | 0:a8b6c10f03dc | 823 | |
mauuuuul | 0:a8b6c10f03dc | 824 | if(frm_data_received.ids == 3) { |
mauuuuul | 0:a8b6c10f03dc | 825 | dsp.step4(to_string(frm_data_received._key_id).c_str(),false); |
mauuuuul | 0:a8b6c10f03dc | 826 | } else if(frm_data_received.ids == 1) { |
mauuuuul | 0:a8b6c10f03dc | 827 | dsp.step0(); |
mauuuuul | 0:a8b6c10f03dc | 828 | } |
mauuuuul | 0:a8b6c10f03dc | 829 | } else { |
mauuuuul | 0:a8b6c10f03dc | 830 | |
mauuuuul | 0:a8b6c10f03dc | 831 | char rbuffer[64]; |
mauuuuul | 0:a8b6c10f03dc | 832 | |
mauuuuul | 0:a8b6c10f03dc | 833 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
mauuuuul | 0:a8b6c10f03dc | 834 | if(rcount < 0) { |
mauuuuul | 0:a8b6c10f03dc | 835 | |
mauuuuul | 0:a8b6c10f03dc | 836 | } else if(rcount > 0) { |
mauuuuul | 0:a8b6c10f03dc | 837 | rbuffer[rcount] = '\0'; |
mauuuuul | 0:a8b6c10f03dc | 838 | |
mauuuuul | 0:a8b6c10f03dc | 839 | string data; |
mauuuuul | 0:a8b6c10f03dc | 840 | |
mauuuuul | 0:a8b6c10f03dc | 841 | for(int i = 0; i<sizeof(rbuffer); i++) { |
mauuuuul | 0:a8b6c10f03dc | 842 | if(rbuffer[i] == '\0')break; |
mauuuuul | 0:a8b6c10f03dc | 843 | data += rbuffer[i]; |
mauuuuul | 0:a8b6c10f03dc | 844 | } |
mauuuuul | 0:a8b6c10f03dc | 845 | |
mauuuuul | 0:a8b6c10f03dc | 846 | vector<string> res = split(data, "~"); |
mauuuuul | 0:a8b6c10f03dc | 847 | |
mauuuuul | 0:a8b6c10f03dc | 848 | if(sizeof(res) != 0) { |
mauuuuul | 0:a8b6c10f03dc | 849 | temp_data = split(res[0], ";"); |
mauuuuul | 0:a8b6c10f03dc | 850 | |
mauuuuul | 0:a8b6c10f03dc | 851 | if(temp_data[0] == "1" && temp_data[1] == "0") { |
mauuuuul | 0:a8b6c10f03dc | 852 | ibutton_code.clear(); |
mauuuuul | 0:a8b6c10f03dc | 853 | to_sends.clear(); |
mauuuuul | 0:a8b6c10f03dc | 854 | LCD_Step_4 = 0; |
mauuuuul | 0:a8b6c10f03dc | 855 | step = 0; |
mauuuuul | 0:a8b6c10f03dc | 856 | dsp.step0(); |
mauuuuul | 0:a8b6c10f03dc | 857 | continue; |
mauuuuul | 0:a8b6c10f03dc | 858 | } |
mauuuuul | 0:a8b6c10f03dc | 859 | } |
mauuuuul | 0:a8b6c10f03dc | 860 | } else if(rcount == 0) { |
mauuuuul | 0:a8b6c10f03dc | 861 | printf("Server Has Been Closed..\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 862 | |
mauuuuul | 0:a8b6c10f03dc | 863 | isConnected = false; |
mauuuuul | 0:a8b6c10f03dc | 864 | step = 0;// |
mauuuuul | 0:a8b6c10f03dc | 865 | dsp.init(); |
mauuuuul | 0:a8b6c10f03dc | 866 | socket.close(); |
mauuuuul | 0:a8b6c10f03dc | 867 | } |
mauuuuul | 0:a8b6c10f03dc | 868 | |
mauuuuul | 0:a8b6c10f03dc | 869 | memset(real_blink_id,64,(sizeof(real_blink_id)/sizeof(real_blink_id[0]))); |
mauuuuul | 0:a8b6c10f03dc | 870 | blinking_now = 0; |
mauuuuul | 0:a8b6c10f03dc | 871 | |
mauuuuul | 0:a8b6c10f03dc | 872 | idle_wait_result++; |
mauuuuul | 0:a8b6c10f03dc | 873 | |
mauuuuul | 0:a8b6c10f03dc | 874 | if(idle_wait_result == 200) { |
mauuuuul | 0:a8b6c10f03dc | 875 | to_sends.clear(); |
mauuuuul | 0:a8b6c10f03dc | 876 | to_sends.push_back(frm_data_received.ids); |
mauuuuul | 0:a8b6c10f03dc | 877 | to_sends.push_back((real_id>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 878 | to_sends.push_back(real_id & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 879 | to_sends.push_back(0x00);//salah_ambil byte1 |
mauuuuul | 0:a8b6c10f03dc | 880 | to_sends.push_back(0x00);//salah_ambil byte2 |
mauuuuul | 0:a8b6c10f03dc | 881 | to_sends.push_back(0x00);//salah_taruh byte1 |
mauuuuul | 0:a8b6c10f03dc | 882 | to_sends.push_back(0x00);//salah_taruh byte2 |
mauuuuul | 0:a8b6c10f03dc | 883 | |
mauuuuul | 0:a8b6c10f03dc | 884 | if(to_sends_last != to_sends) { |
mauuuuul | 0:a8b6c10f03dc | 885 | socket.send(to_sends.data(), to_sends.size()); |
mauuuuul | 0:a8b6c10f03dc | 886 | to_sends_last.clear(); |
mauuuuul | 0:a8b6c10f03dc | 887 | copy(to_sends.begin(), to_sends.end(), back_inserter(to_sends_last)); |
mauuuuul | 0:a8b6c10f03dc | 888 | } |
mauuuuul | 0:a8b6c10f03dc | 889 | } |
mauuuuul | 0:a8b6c10f03dc | 890 | |
mauuuuul | 0:a8b6c10f03dc | 891 | if(LCD_Step_4 == 0) { |
mauuuuul | 0:a8b6c10f03dc | 892 | if(frm_data_received.ids== 1) { |
mauuuuul | 0:a8b6c10f03dc | 893 | dsp.step2(to_string(frm_data_received._key_id).c_str()); |
mauuuuul | 0:a8b6c10f03dc | 894 | wait(1); |
mauuuuul | 0:a8b6c10f03dc | 895 | LCD_Step_4 = 1; |
mauuuuul | 0:a8b6c10f03dc | 896 | } else if(frm_data_received.ids == 3) { |
mauuuuul | 0:a8b6c10f03dc | 897 | dsp.step4(to_string(frm_data_received._key_id).c_str(),true); |
mauuuuul | 0:a8b6c10f03dc | 898 | wait(1); |
mauuuuul | 0:a8b6c10f03dc | 899 | LCD_Step_4 = 1; |
mauuuuul | 0:a8b6c10f03dc | 900 | } |
mauuuuul | 0:a8b6c10f03dc | 901 | } |
mauuuuul | 0:a8b6c10f03dc | 902 | } |
mauuuuul | 0:a8b6c10f03dc | 903 | } |
mauuuuul | 0:a8b6c10f03dc | 904 | |
mauuuuul | 0:a8b6c10f03dc | 905 | } |
mauuuuul | 0:a8b6c10f03dc | 906 | break; |
mauuuuul | 0:a8b6c10f03dc | 907 | } |
mauuuuul | 0:a8b6c10f03dc | 908 | } |
mauuuuul | 0:a8b6c10f03dc | 909 | } |
mauuuuul | 0:a8b6c10f03dc | 910 | } |
mauuuuul | 0:a8b6c10f03dc | 911 | |
mauuuuul | 0:a8b6c10f03dc | 912 | int main() |
mauuuuul | 0:a8b6c10f03dc | 913 | { |
mauuuuul | 0:a8b6c10f03dc | 914 | //serial_rx_tick.attach(&RS485_Recv,0.01); |
mauuuuul | 0:a8b6c10f03dc | 915 | for(int i = 0; i<5; i++) { |
mauuuuul | 0:a8b6c10f03dc | 916 | memset(Row_Board_Last[i],0,(sizeof(Row_Board_Last[i])/sizeof(Row_Board_Last[i][0]))); |
mauuuuul | 0:a8b6c10f03dc | 917 | memset(Row_Board_Now[i],0,(sizeof(Row_Board_Now[i])/sizeof(Row_Board_Now[i][0]))); |
mauuuuul | 0:a8b6c10f03dc | 918 | memset(Row_Board_Predict[i],0,(sizeof(Row_Board_Predict[i])/sizeof(Row_Board_Predict[i][0]))); |
mauuuuul | 0:a8b6c10f03dc | 919 | } |
mauuuuul | 0:a8b6c10f03dc | 920 | |
mauuuuul | 0:a8b6c10f03dc | 921 | ids = 0; |
mauuuuul | 0:a8b6c10f03dc | 922 | to_sends_last.clear(); |
mauuuuul | 0:a8b6c10f03dc | 923 | to_sends_last.push_back(2); |
mauuuuul | 0:a8b6c10f03dc | 924 | to_sends_last.push_back(0x00);//blinking now byte1 |
mauuuuul | 0:a8b6c10f03dc | 925 | to_sends_last.push_back(0x00);//blinking now byte2 |
mauuuuul | 0:a8b6c10f03dc | 926 | to_sends_last.push_back(0x00);//salah_ambil byte1 |
mauuuuul | 0:a8b6c10f03dc | 927 | to_sends_last.push_back(0x00);//salah_ambil byte2 |
mauuuuul | 0:a8b6c10f03dc | 928 | to_sends_last.push_back(0x00);//salah_taruh byte1 |
mauuuuul | 0:a8b6c10f03dc | 929 | to_sends_last.push_back(0x00);//salah_taruh byte2 |
mauuuuul | 0:a8b6c10f03dc | 930 | |
mauuuuul | 0:a8b6c10f03dc | 931 | wait(0.01); |
mauuuuul | 0:a8b6c10f03dc | 932 | dsp.init(); |
mauuuuul | 0:a8b6c10f03dc | 933 | |
mauuuuul | 0:a8b6c10f03dc | 934 | // thd_Cek_Ibutton.start(Cek_Ibutton); |
mauuuuul | 0:a8b6c10f03dc | 935 | thd_Buzzer.start(Cek_Salah_Buzzer); |
mauuuuul | 0:a8b6c10f03dc | 936 | //thd_LCD.start(Cek_LCD_State); |
mauuuuul | 0:a8b6c10f03dc | 937 | //thd_RS485_Recv.start(RS485_Recv); |
mauuuuul | 0:a8b6c10f03dc | 938 | |
mauuuuul | 0:a8b6c10f03dc | 939 | FindingServer(); |
mauuuuul | 0:a8b6c10f03dc | 940 | } |
mauuuuul | 0:a8b6c10f03dc | 941 | |
mauuuuul | 0:a8b6c10f03dc | 942 | string Byte_to_StringBinary(byte data_byte) |
mauuuuul | 0:a8b6c10f03dc | 943 | { |
mauuuuul | 0:a8b6c10f03dc | 944 | string data_temp; |
mauuuuul | 0:a8b6c10f03dc | 945 | for(int i = 0; i <8; i++) { |
mauuuuul | 0:a8b6c10f03dc | 946 | if((data_byte>>i)&0x01) { |
mauuuuul | 0:a8b6c10f03dc | 947 | data_temp[abs(i-7)] += '1'; |
mauuuuul | 0:a8b6c10f03dc | 948 | } else data_temp[abs(i-7)] += '0'; |
mauuuuul | 0:a8b6c10f03dc | 949 | } |
mauuuuul | 0:a8b6c10f03dc | 950 | return data_temp; |
mauuuuul | 0:a8b6c10f03dc | 951 | } |
mauuuuul | 0:a8b6c10f03dc | 952 | |
mauuuuul | 0:a8b6c10f03dc | 953 | uint8_t StringBinary_to_Byte(string data_binary) |
mauuuuul | 0:a8b6c10f03dc | 954 | { |
mauuuuul | 0:a8b6c10f03dc | 955 | uint8_t data_temp = 0; |
mauuuuul | 0:a8b6c10f03dc | 956 | bitset<8> b(data_binary); |
mauuuuul | 0:a8b6c10f03dc | 957 | unsigned char c = ( b.to_ulong() & 0xFF); |
mauuuuul | 0:a8b6c10f03dc | 958 | data_temp = static_cast<uint8_t>(c); |
mauuuuul | 0:a8b6c10f03dc | 959 | |
mauuuuul | 0:a8b6c10f03dc | 960 | return data_temp; |
mauuuuul | 0:a8b6c10f03dc | 961 | } |
mauuuuul | 0:a8b6c10f03dc | 962 | |
mauuuuul | 0:a8b6c10f03dc | 963 | int Get_Row(int key_id) |
mauuuuul | 0:a8b6c10f03dc | 964 | { |
mauuuuul | 0:a8b6c10f03dc | 965 | int data_temp = 0; |
mauuuuul | 0:a8b6c10f03dc | 966 | |
mauuuuul | 0:a8b6c10f03dc | 967 | data_temp = key_id/(board_sum*8); |
mauuuuul | 0:a8b6c10f03dc | 968 | |
mauuuuul | 0:a8b6c10f03dc | 969 | return data_temp; |
mauuuuul | 0:a8b6c10f03dc | 970 | } |
mauuuuul | 0:a8b6c10f03dc | 971 | |
mauuuuul | 0:a8b6c10f03dc | 972 | unsigned short Get_CRC_Modbus(byte data_byte[]) |
mauuuuul | 0:a8b6c10f03dc | 973 | { |
mauuuuul | 0:a8b6c10f03dc | 974 | unsigned short data_temp; |
mauuuuul | 0:a8b6c10f03dc | 975 | |
mauuuuul | 0:a8b6c10f03dc | 976 | data_temp = calculate_crc16_Modbus(reinterpret_cast<char*>(data_byte), 6); |
mauuuuul | 0:a8b6c10f03dc | 977 | |
mauuuuul | 0:a8b6c10f03dc | 978 | return data_temp; |
mauuuuul | 0:a8b6c10f03dc | 979 | } |
mauuuuul | 0:a8b6c10f03dc | 980 | byte to_send[6] = {0x00,0x03,0x14,0x15,0x01,0x00}; |
mauuuuul | 0:a8b6c10f03dc | 981 | void Send_RS485(int board,int key_id,int key_id_blinking,bool Cek_Respon) |
mauuuuul | 0:a8b6c10f03dc | 982 | { |
mauuuuul | 0:a8b6c10f03dc | 983 | to_send[0] = board+1; |
mauuuuul | 0:a8b6c10f03dc | 984 | to_send[2] = key_id; |
mauuuuul | 0:a8b6c10f03dc | 985 | to_send[3] = key_id_blinking; |
mauuuuul | 0:a8b6c10f03dc | 986 | //printf("Calculating crc : "); |
mauuuuul | 0:a8b6c10f03dc | 987 | unsigned short __crc = Get_CRC_Modbus(to_send); |
mauuuuul | 0:a8b6c10f03dc | 988 | |
mauuuuul | 0:a8b6c10f03dc | 989 | //printf("%02X\r\n",__crc); |
mauuuuul | 0:a8b6c10f03dc | 990 | memcpy(buffer_crc,(unsigned char*)&(__crc),sizeof(short)); |
mauuuuul | 0:a8b6c10f03dc | 991 | byte _eof = 0x0D; |
mauuuuul | 0:a8b6c10f03dc | 992 | |
mauuuuul | 0:a8b6c10f03dc | 993 | byte all_to_send[9]; |
mauuuuul | 0:a8b6c10f03dc | 994 | |
mauuuuul | 0:a8b6c10f03dc | 995 | all_to_send[0] = board+1; |
mauuuuul | 0:a8b6c10f03dc | 996 | all_to_send[1] = to_send[1]; |
mauuuuul | 0:a8b6c10f03dc | 997 | all_to_send[2] = to_send[2]; |
mauuuuul | 0:a8b6c10f03dc | 998 | all_to_send[3] = to_send[3]; |
mauuuuul | 0:a8b6c10f03dc | 999 | all_to_send[4] = to_send[4]; |
mauuuuul | 0:a8b6c10f03dc | 1000 | all_to_send[5] = to_send[5]; |
mauuuuul | 0:a8b6c10f03dc | 1001 | all_to_send[6] = buffer_crc[1]; |
mauuuuul | 0:a8b6c10f03dc | 1002 | all_to_send[7] = buffer_crc[0]; |
mauuuuul | 0:a8b6c10f03dc | 1003 | all_to_send[8] = _eof; |
mauuuuul | 0:a8b6c10f03dc | 1004 | |
mauuuuul | 0:a8b6c10f03dc | 1005 | Resend: |
mauuuuul | 0:a8b6c10f03dc | 1006 | |
mauuuuul | 0:a8b6c10f03dc | 1007 | // printf("Data sent : "); |
mauuuuul | 0:a8b6c10f03dc | 1008 | // for(int i = 0; i<9; i++) { |
mauuuuul | 0:a8b6c10f03dc | 1009 | // printf("%02X ",all_to_send[i]); |
mauuuuul | 0:a8b6c10f03dc | 1010 | // } |
mauuuuul | 0:a8b6c10f03dc | 1011 | // printf("\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 1012 | |
mauuuuul | 0:a8b6c10f03dc | 1013 | printf("Data recv : "); |
mauuuuul | 0:a8b6c10f03dc | 1014 | for(int i = 0; i<15; i++) { |
mauuuuul | 0:a8b6c10f03dc | 1015 | printf("%02X ",respon_from_slave[i]); |
mauuuuul | 0:a8b6c10f03dc | 1016 | } |
mauuuuul | 0:a8b6c10f03dc | 1017 | printf("\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 1018 | wait(0.05); |
mauuuuul | 0:a8b6c10f03dc | 1019 | rede = 1; |
mauuuuul | 0:a8b6c10f03dc | 1020 | for(int i = 0; i<9; i++) { |
mauuuuul | 0:a8b6c10f03dc | 1021 | dv.putc(all_to_send[i]); |
mauuuuul | 0:a8b6c10f03dc | 1022 | } |
mauuuuul | 0:a8b6c10f03dc | 1023 | |
mauuuuul | 0:a8b6c10f03dc | 1024 | if(Cek_Respon) printf("Send Complete to %d\r\n",board+1); |
mauuuuul | 0:a8b6c10f03dc | 1025 | |
mauuuuul | 0:a8b6c10f03dc | 1026 | if(!Cek_Respon) { |
mauuuuul | 0:a8b6c10f03dc | 1027 | intr_Done = true; |
mauuuuul | 0:a8b6c10f03dc | 1028 | } else { |
mauuuuul | 0:a8b6c10f03dc | 1029 | //printf("Trying to received%d..\r\n",board+1); |
mauuuuul | 0:a8b6c10f03dc | 1030 | memset(respon_from_slave, 0, (sizeof respon_from_slave/sizeof respon_from_slave[0])); |
mauuuuul | 0:a8b6c10f03dc | 1031 | intr_Done = false; |
mauuuuul | 0:a8b6c10f03dc | 1032 | ids = 0; |
mauuuuul | 0:a8b6c10f03dc | 1033 | rede = 0; |
mauuuuul | 0:a8b6c10f03dc | 1034 | |
mauuuuul | 0:a8b6c10f03dc | 1035 | while(!intr_Done) { |
mauuuuul | 0:a8b6c10f03dc | 1036 | while(dv.readable()) { |
mauuuuul | 0:a8b6c10f03dc | 1037 | |
mauuuuul | 0:a8b6c10f03dc | 1038 | char tempbuf = dv.getc(); |
mauuuuul | 0:a8b6c10f03dc | 1039 | if(ids == 0 && tempbuf == 0xFF) { |
mauuuuul | 0:a8b6c10f03dc | 1040 | |
mauuuuul | 0:a8b6c10f03dc | 1041 | } else { |
mauuuuul | 0:a8b6c10f03dc | 1042 | respon_from_slave[ids] = tempbuf; |
mauuuuul | 0:a8b6c10f03dc | 1043 | ids++; |
mauuuuul | 0:a8b6c10f03dc | 1044 | } |
mauuuuul | 0:a8b6c10f03dc | 1045 | } |
mauuuuul | 0:a8b6c10f03dc | 1046 | |
mauuuuul | 0:a8b6c10f03dc | 1047 | // printf("Data recv : "); |
mauuuuul | 0:a8b6c10f03dc | 1048 | // for(int i = 0; i<17; i++) { |
mauuuuul | 0:a8b6c10f03dc | 1049 | // printf("%02X ",respon_from_slave[i]); |
mauuuuul | 0:a8b6c10f03dc | 1050 | // } |
mauuuuul | 0:a8b6c10f03dc | 1051 | // printf("\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 1052 | //printf("Trying to checking..\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 1053 | |
mauuuuul | 0:a8b6c10f03dc | 1054 | if((respon_from_slave[0] == board+1) && (respon_from_slave[14] == 0x0D)) intr_Done = true; |
mauuuuul | 0:a8b6c10f03dc | 1055 | else goto Resend; |
mauuuuul | 0:a8b6c10f03dc | 1056 | } |
mauuuuul | 0:a8b6c10f03dc | 1057 | } |
mauuuuul | 0:a8b6c10f03dc | 1058 | } |
mauuuuul | 0:a8b6c10f03dc | 1059 | |
mauuuuul | 0:a8b6c10f03dc | 1060 | |
mauuuuul | 0:a8b6c10f03dc | 1061 | void Cek_Ibutton() |
mauuuuul | 0:a8b6c10f03dc | 1062 | { |
mauuuuul | 0:a8b6c10f03dc | 1063 | // while(1) { |
mauuuuul | 0:a8b6c10f03dc | 1064 | // |
mauuuuul | 0:a8b6c10f03dc | 1065 | // } |
mauuuuul | 0:a8b6c10f03dc | 1066 | } |
mauuuuul | 0:a8b6c10f03dc | 1067 | |
mauuuuul | 0:a8b6c10f03dc | 1068 | void Cek_Salah_Buzzer() |
mauuuuul | 0:a8b6c10f03dc | 1069 | { |
mauuuuul | 0:a8b6c10f03dc | 1070 | while(1) { |
mauuuuul | 0:a8b6c10f03dc | 1071 | if(real_blink_id[0] != 64 || real_blink_id[1] != 64 || real_blink_id[2] != 64 || real_blink_id[3] != 64|| real_blink_id[4] != 64) { |
mauuuuul | 0:a8b6c10f03dc | 1072 | buzz.play(3); |
mauuuuul | 0:a8b6c10f03dc | 1073 | } |
mauuuuul | 0:a8b6c10f03dc | 1074 | wait(0.2); |
mauuuuul | 0:a8b6c10f03dc | 1075 | } |
mauuuuul | 0:a8b6c10f03dc | 1076 | } |
mauuuuul | 0:a8b6c10f03dc | 1077 | |
mauuuuul | 0:a8b6c10f03dc | 1078 | string frames; |
mauuuuul | 0:a8b6c10f03dc | 1079 | void First_Update() |
mauuuuul | 0:a8b6c10f03dc | 1080 | { |
mauuuuul | 0:a8b6c10f03dc | 1081 | frames.clear(); |
mauuuuul | 0:a8b6c10f03dc | 1082 | |
mauuuuul | 0:a8b6c10f03dc | 1083 | for(uint8_t i = 0; i<board_sum; i++) { |
mauuuuul | 0:a8b6c10f03dc | 1084 | wait_ms(50); |
mauuuuul | 0:a8b6c10f03dc | 1085 | intr_Done = false; |
mauuuuul | 0:a8b6c10f03dc | 1086 | ids = 0; |
mauuuuul | 0:a8b6c10f03dc | 1087 | Send_RS485(i,64,64,true); |
mauuuuul | 0:a8b6c10f03dc | 1088 | printf("Send Done\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 1089 | for(uint8_t a = 4; a<12; a++) { |
mauuuuul | 0:a8b6c10f03dc | 1090 | Row_Board_Last[i][a-4] = respon_from_slave[a]; |
mauuuuul | 0:a8b6c10f03dc | 1091 | //printf("%02X\r\n",Row_Board1_Last[temp_id-5]); |
mauuuuul | 0:a8b6c10f03dc | 1092 | } |
mauuuuul | 0:a8b6c10f03dc | 1093 | } |
mauuuuul | 0:a8b6c10f03dc | 1094 | |
mauuuuul | 0:a8b6c10f03dc | 1095 | string bin_string; |
mauuuuul | 0:a8b6c10f03dc | 1096 | salah_ambil.clear(); |
mauuuuul | 0:a8b6c10f03dc | 1097 | salah_taruh.clear(); |
mauuuuul | 0:a8b6c10f03dc | 1098 | string key_ids; |
mauuuuul | 0:a8b6c10f03dc | 1099 | for (uint8_t i = 0; i < board_sum; i++) { |
mauuuuul | 0:a8b6c10f03dc | 1100 | for (uint8_t j = 0; j < 8; j++) { |
mauuuuul | 0:a8b6c10f03dc | 1101 | bin_string = Byte_to_StringBinary(Row_Board_Last[i][j]); |
mauuuuul | 0:a8b6c10f03dc | 1102 | //printf("board[%d][%d] = %s\r\n",i,j,bin_string.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 1103 | for (uint8_t k = 0; k < 8; k++) { |
mauuuuul | 0:a8b6c10f03dc | 1104 | if (bin_string[abs(k-7)] == '1') { |
mauuuuul | 0:a8b6c10f03dc | 1105 | salah_ambil.push_back((40*j)+k+(8*i)+1);//Salah Ambil -> Key_On |
mauuuuul | 0:a8b6c10f03dc | 1106 | } |
mauuuuul | 0:a8b6c10f03dc | 1107 | } |
mauuuuul | 0:a8b6c10f03dc | 1108 | } |
mauuuuul | 0:a8b6c10f03dc | 1109 | } |
mauuuuul | 0:a8b6c10f03dc | 1110 | |
mauuuuul | 0:a8b6c10f03dc | 1111 | to_sends.clear(); |
mauuuuul | 0:a8b6c10f03dc | 1112 | to_sends.push_back(0x04); |
mauuuuul | 0:a8b6c10f03dc | 1113 | to_sends.push_back(0x00); |
mauuuuul | 0:a8b6c10f03dc | 1114 | to_sends.push_back(0x00); |
mauuuuul | 0:a8b6c10f03dc | 1115 | to_sends.push_back((salah_ambil.size()>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 1116 | to_sends.push_back(salah_ambil.size() & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 1117 | to_sends.push_back((salah_taruh.size()>>8)&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 1118 | to_sends.push_back(salah_taruh.size()&0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 1119 | |
mauuuuul | 0:a8b6c10f03dc | 1120 | for(int y = 0; y<salah_ambil.size(); y++) { |
mauuuuul | 0:a8b6c10f03dc | 1121 | to_sends.push_back((salah_ambil[y] >> 8) & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 1122 | to_sends.push_back(salah_ambil[y] & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 1123 | } |
mauuuuul | 0:a8b6c10f03dc | 1124 | |
mauuuuul | 0:a8b6c10f03dc | 1125 | for(int y = 0; y<salah_taruh.size(); y++) { |
mauuuuul | 0:a8b6c10f03dc | 1126 | to_sends.push_back((salah_taruh[y] >> 8) & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 1127 | to_sends.push_back(salah_taruh[y] & 0x0FF); |
mauuuuul | 0:a8b6c10f03dc | 1128 | } |
mauuuuul | 0:a8b6c10f03dc | 1129 | } |
mauuuuul | 0:a8b6c10f03dc | 1130 | string data_bin_pred; |
mauuuuul | 0:a8b6c10f03dc | 1131 | void Make_Prediction(int board_no,int baris, int kolom) |
mauuuuul | 0:a8b6c10f03dc | 1132 | { |
mauuuuul | 0:a8b6c10f03dc | 1133 | printf("baris = %d\r\n",baris); |
mauuuuul | 0:a8b6c10f03dc | 1134 | printf("kolom = %d\r\n",kolom); |
mauuuuul | 0:a8b6c10f03dc | 1135 | |
mauuuuul | 0:a8b6c10f03dc | 1136 | for(int i = 0; i<board_sum; i++) { |
mauuuuul | 0:a8b6c10f03dc | 1137 | memcpy(Row_Board_Predict[i],Row_Board_Last[i],(sizeof(Row_Board_Predict[i])/sizeof (Row_Board_Predict[i][0]))); |
mauuuuul | 0:a8b6c10f03dc | 1138 | if(board_no == i) { |
mauuuuul | 0:a8b6c10f03dc | 1139 | printf("Predict Before: "); |
mauuuuul | 0:a8b6c10f03dc | 1140 | for(int j = 0; j<8; j++) { |
mauuuuul | 0:a8b6c10f03dc | 1141 | printf("%02X ",Row_Board_Predict[i][j]); |
mauuuuul | 0:a8b6c10f03dc | 1142 | } |
mauuuuul | 0:a8b6c10f03dc | 1143 | printf("\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 1144 | data_bin_pred = Byte_to_StringBinary(Row_Board_Predict[i][baris]); |
mauuuuul | 0:a8b6c10f03dc | 1145 | printf("Before %s\r\n",data_bin_pred.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 1146 | |
mauuuuul | 0:a8b6c10f03dc | 1147 | if(frm_data_received.cmd_id == 1) { |
mauuuuul | 0:a8b6c10f03dc | 1148 | data_bin_pred[abs(kolom-7)] = '0'; |
mauuuuul | 0:a8b6c10f03dc | 1149 | } else if(frm_data_received.cmd_id == 2) { |
mauuuuul | 0:a8b6c10f03dc | 1150 | data_bin_pred[abs(kolom-7)] = '1'; |
mauuuuul | 0:a8b6c10f03dc | 1151 | } |
mauuuuul | 0:a8b6c10f03dc | 1152 | printf("After %s\r\n",data_bin_pred.c_str()); |
mauuuuul | 0:a8b6c10f03dc | 1153 | uint8_t datas = stoi(data_bin_pred,0,2); |
mauuuuul | 0:a8b6c10f03dc | 1154 | Row_Board_Predict[i][baris] = datas; |
mauuuuul | 0:a8b6c10f03dc | 1155 | printf("Predict After: "); |
mauuuuul | 0:a8b6c10f03dc | 1156 | for(int i = 0; i<8; i++) { |
mauuuuul | 0:a8b6c10f03dc | 1157 | printf("%02X ",Row_Board_Predict[i][i]); |
mauuuuul | 0:a8b6c10f03dc | 1158 | } |
mauuuuul | 0:a8b6c10f03dc | 1159 | printf("\r\n"); |
mauuuuul | 0:a8b6c10f03dc | 1160 | |
mauuuuul | 0:a8b6c10f03dc | 1161 | } |
mauuuuul | 0:a8b6c10f03dc | 1162 | } |
mauuuuul | 0:a8b6c10f03dc | 1163 | } |