Components and supplies
SparkFun FTDI Basic Breakout - 3.3V
Arduino Pro Mini 328 - 3.3V/8MHz
HC-05 Bluetooth Module
Project description
Code
CLOCK Arduino code
arduino
1/* Arduino Net-P (i2c processors network) by Zonca Marco 2020 2 * 'netMyNAME' ("CLOCK ") max 8 char, 'netMyID' (p9) max 2 char, 3 * address='netMyAddress' (0x16) max 0xFF, 'netMyPx' (9) 4 * 5 * implemented commands (* = to do): HELP | ?, dht, gettemp, gettime, settime, red, green 6 * 7 */ 8 9#include <Wire.h> 10#include <ds3231.h> // RTC real time clock 11#include <dht.h> 12 13const int netMyPx = 9; // netMyPx 0-9 14const int netMyAddress = 0x16; // i2c address 0x16=HEX 22=DEC 15const char *netMyID = "p9"; // netMyID 16const char *netMyNAME = "CLOCK "; // netMyNAME 17const int GreenLedPin=8; 18const int RedLedPin=9; 19const int DHTPin=14; 20const int ResetPin=17; // A3 put low for reset 21 22char _CR[2]=""; // CR 23char _BEL[2]=""; // BEL 24char _ENQ[2]=""; // ENQ 25char _ACK[2]=""; // ACK 26char _SPACE[2]=""; // SPACE 27char _MYADDR[2]=""; // netMyAddress 28 29char lineString[65]=""; 30char recCommand[65]=""; 31byte recAddress=0; 32byte recType=0; 33char data[2]=""; 34bool isNetDataWaiting=false; 35bool isKnownCommand = false; 36bool isRedOn=false; 37bool isGreenOn=false; 38bool isBusy=false; 39 40struct ts t; // RTC 41dht DHT; // HG + Temp sensor 42 43void setup() { 44 //Serial.begin(38400); 45 Wire.begin(netMyAddress); 46 Wire.onReceive(receiveEvent); // i2c event 47 Wire.onRequest(netWhoIsEvent); // i2c whois 48 _CR[0] = 13; // CR 49 _BEL[0] = 7; // BEL 50 _ENQ[0] = 5; // ENQ 51 _ACK[0] = 6; // ACK 52 _SPACE[0] = 32; // SPACE 53 _MYADDR[0] = netMyAddress; 54 pinMode(GreenLedPin, OUTPUT); 55 pinMode(RedLedPin, OUTPUT); 56 digitalWrite(RedLedPin,LOW); 57 digitalWrite(GreenLedPin,LOW); 58 DS3231_init(DS3231_INTCN); // RTC 59 DHT.read11(DHTPin); // DHT 60} // end setup() 61 62void loop() { 63 if (isNetDataWaiting == true) { 64 execNetCommand(); 65 } 66} // end loop() 67 68void execNetCommand() { // executes received command 69 isBusy=true; 70 char park[65]=""; 71 char command[65]=""; 72 if (recType == _BEL[0]) { // char(7) = BEL command type 73 isKnownCommand=false; 74 if (s_compare(recCommand,"")==0) { // empty command just do nothing 75 isKnownCommand=true; 76 prompt(); 77 } 78 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,3); // red 79 if (s_compare(park,"red ") == 0) { 80 s_substring(command,sizeof(command),recCommand,sizeof(recCommand),4,strlen(recCommand)-1); 81 if (strcmp(command, "on") == 0 || strcmp(command, "off") == 0 || strcmp(command, "?") == 0) { 82 isKnownCommand=true; 83 ledred(command); 84 } 85 } 86 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,5); // green 87 if (s_compare(park,"green ") == 0) { 88 s_substring(command,sizeof(command),recCommand,sizeof(recCommand),6,strlen(recCommand)-1); 89 if (strcmp(command, "on") == 0 || strcmp(command, "off") == 0 || strcmp(command, "?") == 0) { 90 isKnownCommand=true; 91 ledgreen(command); 92 } 93 } 94 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,4); // reset 95 if (s_compare(park,"reset") == 0) { 96 isKnownCommand=true; 97 reset(); 98 } 99 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,2); // dht 100 if (s_compare(park,"dht") == 0) { 101 isKnownCommand=true; 102 dht(); 103 } 104 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,6); // gettemp 105 if (s_compare(park,"gettemp") == 0) { 106 isKnownCommand=true; 107 gettemp(); 108 } 109 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,6); // gettime 110 if (s_compare(park,"gettime") == 0) { 111 isKnownCommand=true; 112 gettime(); 113 } 114 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,7); // settime 115 if (s_compare(park,"settime ") == 0) { 116 s_substring(command,sizeof(command),recCommand,sizeof(recCommand),8,strlen(recCommand)-1); 117 if (strlen(command) == 19) { 118 isKnownCommand=true; 119 settime(command); 120 } 121 } 122 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,0); // ? 123 if (s_compare(park,"?") == 0) { 124 isKnownCommand=true; 125 help(); 126 } 127 if (isKnownCommand == false) { 128 s_assign(lineString,"unknown cmd",sizeof(lineString)); 129 dispLine(); 130 prompt(); 131 } 132 } // endif recType 133 s_clear(recCommand,sizeof(recCommand)); 134 recType=0; 135 recAddress=0; 136 isNetDataWaiting=false; 137 isBusy=false; 138} // end execNetCommand() 139 140void reset() { // ------------------------------------------------------------ RESET 141 analogWrite(ResetPin,LOW); 142} 143 144void help() { // ---------------------------------------------- HELP 145 s_assign(lineString, "red <on off ?>", sizeof(lineString)); 146 dispLine(); 147 s_assign(lineString, "green <on off ?>", sizeof(lineString)); 148 dispLine(); 149 s_assign(lineString, "dht", sizeof(lineString)); 150 dispLine(); 151 s_assign(lineString, "gettemp", sizeof(lineString)); 152 dispLine(); 153 s_assign(lineString, "gettime", sizeof(lineString)); 154 dispLine(); 155 s_assign(lineString, "settime <DD/MM/YYYY HH:MM:SS>", sizeof(lineString)); 156 dispLine(); 157 s_assign(lineString, "reset", sizeof(lineString)); 158 dispLine(); 159 sprintf(lineString,"(free=%d)",s_freemem()); 160 dispLine(); 161 prompt(); 162} // end help() 163 164void ledred(const char *_s) { // ---------------------------------------------- RED 165 if (strcmp(_s, "on")==0) { 166 digitalWrite(RedLedPin,HIGH); 167 isRedOn=true; 168 } 169 if (strcmp(_s, "off")==0) { 170 digitalWrite(RedLedPin,LOW); 171 isRedOn=false; 172 } 173 if (isRedOn) { 174 sprintf(lineString, "%s","ON"); 175 } else { 176 sprintf(lineString, "%s","OFF"); 177 } 178 dispLine(); 179 prompt(); 180} // end ledred() 181 182void ledgreen(const char *_s) { // ---------------------------------------------- GREEN 183 if (strcmp(_s, "on")==0) { 184 digitalWrite(GreenLedPin,HIGH); 185 isGreenOn=true; 186 } 187 if (strcmp(_s, "off")==0) { 188 digitalWrite(GreenLedPin,LOW); 189 isGreenOn=false; 190 } 191 if (isGreenOn) { 192 sprintf(lineString, "%s","ON"); 193 } else { 194 sprintf(lineString, "%s","OFF"); 195 } 196 dispLine(); 197 prompt(); 198} // end ledgreen() 199 200void dht() { // ---------------------------------------------- DHT 201 float temp=0; 202 int iintt=0; 203 int idect=0; 204 int iinth=0; 205 int idech=0; 206 DHT.read11(DHTPin); // DHT 207 temp = DHT.temperature; // read DHT temp C 208 iintt=temp; 209 idect=float((temp-iintt)*100); 210 temp = DHT.humidity; // read DHT humidity 211 iinth=temp; 212 idech=float((temp-iinth)*100); 213 sprintf (lineString, "%d.%dC %d.%d", iintt, idect, iinth, idech); 214 s_concat(lineString,"%",sizeof(lineString)); 215 dispLine(); 216 prompt(); 217} // end gettemp() 218 219void gettemp() { // ---------------------------------------------- GETTEMP 220 float temp=0; 221 int iint=0; 222 int idec=0; 223 temp = DS3231_get_treg(); // read RTC temp C 224 iint=temp; 225 idec=float((temp-iint)*100); 226 sprintf (lineString, "%d.%dC", iint, idec); 227 dispLine(); 228 prompt(); 229} // end gettemp() 230 231void gettime() { // ---------------------------------------------- GETTIME 232 DS3231_get(&t); // read RTC date & time 233 sprintf(lineString, "%02d/%02d/%04d %02d:%02d:%02d", t.mday, t.mon, t.year, t.hour, t.min, t.sec); 234 dispLine(); 235 prompt(); 236} // end gettime() 237 238void settime(const char* _s) { // ---------------------------------------------- SETTIME 239 char park[5]=""; 240 s_substring(park, sizeof(park), _s, strlen(_s), 0,1); 241 t.mday=atoi(park); 242 s_substring(park, sizeof(park), _s, strlen(_s), 3,4); 243 t.mon=atoi(park); 244 s_substring(park, sizeof(park), _s, strlen(_s), 6,9); 245 t.year=atoi(park); 246 s_substring(park, sizeof(park), _s, strlen(_s), 11,12); 247 t.hour=atoi(park); 248 s_substring(park, sizeof(park), _s, strlen(_s), 14,15); 249 t.min=atoi(park); 250 s_substring(park, sizeof(park), _s, strlen(_s), 17,18); 251 t.sec=atoi(park); 252 DS3231_set(t); // write RTC 253 gettime(); // answer with new time 254} // end settime() 255 256 257void prompt() { // prompt, ready for commands 258 sprintf(lineString,"%d>\ ",netMyPx); 259 dispLine(); 260} // end prompt() 261 262void dispLine() { // print information 263 s_concat(lineString, _CR, sizeof(lineString)); 264 txBackToSender(); // tx 265 s_clear(lineString, sizeof(lineString)); 266 delay(100); 267} // end dispLine() 268 269void receiveEvent(int howMany) { // i2c event incoming requests 270 if (isBusy==false) { 271 int counter=0; 272 s_clear(recCommand, sizeof(recCommand)); // sender command 273 recAddress=0; // sender address 274 recType=0; // sender cmd type 275 if (howMany == 0) { // ignores empty requests 276 isNetDataWaiting=false; 277 } else { 278 while (Wire.available()) { 279 data[0]=Wire.read(); 280 if (counter==0) { recType = data[0]; } // 1st char = cmd type 281 if (counter==1) { recAddress = data[0]; } // 2nd char = sender address 282 if (counter >1) { // other chars = command 283 s_concat(recCommand, data, sizeof(recCommand)); 284 } 285 counter++; 286 } 287 isNetDataWaiting=true; 288 } 289 }//endisBusy 290} // end receiveEvent() 291 292void netWhoIsEvent() { // i2c event: 11 bytes = 1=char(5) ENQ, 2-9=netMyNAME 10=netMyPx 11=netMyAddress 293 if (isBusy==false) { 294 char ccommand[65]=""; 295 char park[3]=""; 296 s_assign(ccommand, _ENQ, sizeof(ccommand)); // ENQ 297 s_concat(ccommand, netMyNAME, sizeof(ccommand)); // netMyNAME 298 s_concat(ccommand, itoa(netMyPx,park,10), sizeof(ccommand)); // netMyPx 299 s_concat(ccommand, _MYADDR, sizeof(ccommand)); // netMyAddress 300 Wire.write(ccommand); 301 } 302} // end netWhoIsEvent() 303 304void txBackToSender() { // tx back to sender 305 char ccommand[65]=""; 306 s_assign(ccommand, _ACK, sizeof(ccommand)); // ACK=answer data 307 s_concat(ccommand, _MYADDR, sizeof(ccommand)); // netMyAddress 308 s_concat(ccommand, lineString, sizeof(ccommand)); // data 309 Wire.beginTransmission (recAddress); 310 Wire.write (ccommand); 311 Wire.endTransmission (true); 312} // end txBackToSender() 313 314//------------------------------------------------------------------------- CLEAR 315void s_clear(char *dest_source_string, const int dest_sizeof) { // fills-up with NUL=chr(0) 316 memset(dest_source_string, 0, dest_sizeof); 317} // end s_clear() 318 319//------------------------------------------------------------------------- ASSIGN 320bool s_assign(char *dest_string, const char *source_string, const int dest_sizeof) { // copies source to dest 321 int _LenS=strlen(source_string); // how many bytes 322 if (_LenS > (dest_sizeof - 1)) { 323 return true; // err 1 324 } else { 325 strcpy(dest_string, source_string); 326 return false; // ok 0 327 } 328} // end s_assign() 329 330//------------------------------------------------------------------------- SUBSTRING 331bool s_substring(char *dest_string, const int dest_sizeof, const char *source_string, 332 const int source_sizeof, const int source_from, const int source_to) { // copies source(from, to) to dest 333 if ((source_from < 0) || (source_to < source_from) || ((source_to - source_from + 1) > (dest_sizeof - 1)) 334 || (source_to >= (source_sizeof-1)) || ((source_to - source_from + 1) > (strlen(source_string)))) { 335 dest_string[0]=0; // NUL 336 return true; // err 1 337 } else { 338 int _Count=0; 339 for (int i=source_from;i<(source_to+1);i++) { 340 dest_string[_Count]=source_string[i]; 341 _Count++; 342 } 343 dest_string[_Count]=0; // ends with NUL 344 return false; // ok 0 345 } 346} // end s_substring() 347 348//------------------------------------------------------------------------- CONCAT 349bool s_concat(char *dest_string, const char *source_string, const int dest_sizeof) { // append source to dest 350 int _LenS=strlen(source_string); // how many bytes source 351 int _LenD=strlen(dest_string); // how many bytes dest 352 if ((_LenS + _LenD) > (dest_sizeof - 1)) { 353 return true; // err 1 354 } else { 355 strcat(dest_string, source_string); 356 return false; // ok 0 357 } 358} // end s_concat() 359 360//------------------------------------------------------------------------- COMPARE 361bool s_compare(const char *dest_string, const char *source_string) { // compares source with dest 362 int _LenS=strlen(source_string); // how many bytes source 363 int _LenD=strlen(dest_string); // how many bytes dest 364 if (_LenS != _LenD) { // different length 365 return true; // are different 1 366 } else { 367 if (strcmp(dest_string, source_string) == 0) { 368 return false; // are the same 0 369 } else { 370 return true; // are different 1 371 } 372 } 373} // end s_compare() 374 375//------------------------------------------------------------------------- FREEMEM 376int s_freemem() { 377 extern int __heap_start,*__brkval; 378 int v; 379 return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); 380} // end s_freemem() 381
SD Arduino code
arduino
1/* Arduino Net-P (i2c processors network) by Zonca Marco 2020 2 * 'netMyNAME' ("SD ") max 8 char, 'netMyID' (p1) max 2 char, 3 * address='netMyAddress' max 0xFF, 'netMyPx' (1) 4 * 5 * implemented commands (* = to do): mr, mw 6 * dir, mkdir, rmdir, del, type, size, rename*, reset, ? 7 * 8 * (m... commands work on \\MEMORY\\Px\directory, plain text file 'cr'+'lf' terminating line/record) 9 * (sd... commands work on SD memory card) 10 * 11 * ?: ? [ask for help 12 * i.e. ? 13 * returns a list of available commands on board and free memory] 14 * 15 * mw/mwa: mw <FileName>=<Value> [writes/modifies Value in file name 16 * i.e. mw temp01=24 (FileName=Value) 17 * returns "written"] (mwa will append Value to the file) 18 * mr/mra: mr <FileName> [reads Value from corrisponding file name 19 * i.e. mr temp01 (FileName is temp01, Value is 24) 20 * returns 'Value' or "not found"] (mra will read multilines Value from the file) 21 * 22 * reset: reset [resets MPU 23 * i.e. reset] 24 * dir: dir <path> [reads file names starting from 'path' 25 * i.e. dir /] 26 * returns a list of file 'names' and 'size'... ends with 'n found'] 27 * mkdir: mkdir <dirname> [build a new directory (and also relative subdirectories) 28 * i.e. mkdir /music, sdmkdir /logic/params/binary 29 * returns "built" or "error"] 30 * rmdir: rmdir <dirname> [delete a dir 31 * i.e. rmdir /music 32 * returns "removed" or "error or not empty" ] 33 * rename: rename <OldFileName> <NewFileName> [renames a file 34 * i.e. rename myfile.txt bestfile.txt 35 * returns "renamed" or "not found"] 36 * del: del <FileName> [delete a file 37 * i.e. del myfile.txt 38 * returns "deleted" or "not found"] 39 * type: type <FileName> <Mode> [reads a file and displays in mode 0=CHR, 1=BIN, 2=HEX 40 * i.e. type myfile.txt CHR 41 * returns the content of the file ... and filesize 'n' and bytes red 'n' at the end] 42 * eeupl: eeupl <Filename> [read a file and upload it to the EEPROM, write and verify all data 43 * i.e. eeupl /mysketch.hex 44 * returns upload, written, verified...] 45 * 46 * SD card attached to SPI bus as follows: 47 * --------------------------------------- 48 * MOSI - pin 11 49 * MISO - pin 12 50 * CLK - pin 13 51 * CS - pin 10 see 'chipSelect' below 52 */ 53 54#include <Wire.h> 55#include <SdFat.h> 56 57const int netMyPx = 1; // netMyPx 0-9 58const int netMyAddress = 0x0b; // i2c address 0x0b=HEX 11=DEC 59const char *netMyID = "p1"; // netMyID 60const char *netMyNAME = "SD "; // netMyNAME 8 bytes 61const char *memDIR = "/MEMORY/"; // memory directory 62const char *logDIR = "/LOG/"; // log directory 63const int chipSelect = 10; // CS 64const int eprom1addr=80; // 1st eprom address (80, 81, etc. (DEC)) 65const int eprom1pgsz=16; // 1st eprom memory page size (16, 128, etc.) 66const long eprom1size=256; // 1st eprom size (258, 1024, etc.) 67const int ResetPin=17; // A3 put low for reset 68 69char _CR[2]=""; // CR 70char _LF[2]=""; // LF 71char _BEL[2]=""; // BEL 72char _ENQ[2]=""; // ENQ 73char _ACK[2]=""; // ACK 74char _NUL[2]=""; // NUL 75char _SPACE[2]=""; // SPACE 76char _MYADDR[2]=""; // netMyAddress 77 78char lineString[65]=""; 79char recCommand[65]=""; 80char data[2]=""; 81byte recAddress=0; 82byte recType=0; 83bool isNetDataWaiting=false; 84bool isKnownCommand=false; 85bool sdOpened=false; 86bool isBusy=false; 87 88SdFat sd; 89 90void setup() { 91// Serial.begin(38400); 92 Wire.begin(netMyAddress); 93 Wire.onReceive(receiveEvent); // i2c event 94 Wire.onRequest(netWhoIsEvent); // i2c whois 95 _CR[0] = 13; // CR 96 _LF[0] = 10; // LF 97 _BEL[0] = 7; // BEL 98 _ENQ[0] = 5; // ENQ 99 _ACK[0] = 6; // ACK 100 _NUL[0] = 0; // NUL 101 _SPACE[0] = 32; // SPACE 102 _MYADDR[0] = netMyAddress; 103 if (!sd.begin(chipSelect,SPI_FULL_SPEED)) { // in case try SPI_HALF_SPEED 104 sdOpened=false; 105 } else { 106 sdOpened=true; 107 } 108} // end setup() 109 110void loop() { 111 if (isNetDataWaiting == true) { 112 execNetCommand(); 113 } 114} // end loop() 115 116 117void sdreset() { // ------------------------------------------------------------ RESET 118 analogWrite(ResetPin,LOW); 119} 120 121void sddelete(const char *_s) { // ---------------------------------------------- DELETE 122 if (!sd.exists(_s)) { // check if file does exist 123 s_assign(lineString,"not found!",sizeof(lineString)); 124 dispLine(); 125 } else { 126 if (!sd.remove(_s)) { // remove file 127 s_assign(lineString,"error!",sizeof(lineString)); 128 dispLine(); 129 } else { 130 s_assign(lineString,"deleted",sizeof(lineString)); 131 dispLine(); 132 } 133 } 134 prompt(); 135} // end sddelete() 136 137 138void sdeeupload(const char *_s) { // ------------------------------------------- EEUPLOAD 139 File file; 140 long count=0; 141 long uc=0; 142 byte err=0; 143 byte EE=0; 144 long fsize=0; 145 bool isDone=false; 146 byte EErr=0; 147 148 while (isDone==false) { 149 if (!sd.exists(_s)) { // check if name exists // ---------------- preliminary checks 150 s_assign(lineString,"nm not found",sizeof(lineString)); 151 dispLine(); 152 isDone=true; 153 break; 154 } 155 file=sd.open(_s); 156 if (!file.isOpen()) { 157 s_assign(lineString,"nm open!",sizeof(lineString)); 158 dispLine(); 159 isDone=true; 160 break; 161 } 162 fsize=file.fileSize(); 163 if (fsize > (eprom1size-16-1)) { // must be less then size-header-NUL 164 s_assign(lineString,"too big!",sizeof(lineString)); 165 dispLine(); 166 file.close(); 167 isDone=true; 168 break; 169 } 170 sprintf(lineString,"(%ld) upload...",fsize); // ----------------------------- write 171 dispLine(); 172 while (file.available()) { 173 data[0]=file.read(); 174 EE=data[0]; 175 uc=count+16; 176 writeEEPROM (eprom1addr, uc, EE, &EErr); 177 if (EErr!=0) { 178 sprintf(lineString,"error (%d) wr EE!",EErr); 179 dispLine(); 180 file.close(); 181 isDone=true; 182 break; //break this while, later the main while 183 } 184 count++; 185 }//endwhile 186 file.close(); 187 if (isDone==true) {break;} // breaks main while 188 189 EE=_NUL[0]; // --------------------------------------------- write finish with a NUL 190 uc=count+16; // count already+1 191 writeEEPROM (eprom1addr, uc, EE, &EErr); 192 if (EErr!=0) { 193 sprintf(lineString,"error (%d) NUL EE!",EErr); 194 dispLine(); 195 isDone=true; 196 break; 197 } 198 sprintf(lineString,"(%ld) written...",count); 199 dispLine(); 200 201 file=sd.open(_s); // -------------------------------------------------------------- verify 202 if (!file.isOpen()) { 203 s_assign(lineString,"error opn nm!",sizeof(lineString)); 204 dispLine(); 205 isDone=true; 206 break; 207 } 208 count=0; 209 while (file.available()) { 210 data[0]=file.read(); 211 uc=count+16; 212 EE=readEEPROM (eprom1addr, uc, &EErr); 213 if (EErr!=0) { 214 sprintf(lineString,"error (%d) rd EE!",EErr); 215 dispLine(); 216 file.close(); 217 isDone=true; 218 break; 219 } 220 if (byte(data[0])!=EE) { 221 sprintf(lineString,"diff vrf (%ld) in EE!",count); 222 dispLine(); 223 file.close(); 224 isDone=true; 225 break; //break this while, later the main while 226 } 227 count++; 228 }//endwhile 229 file.close(); 230 if (isDone==true) { break; } // breaks main while 231 232 uc=count+16; // ----------------------------------------------- verify finish NUL 233 EE=readEEPROM (eprom1addr, uc, &EErr); 234 if (EErr!=0) { 235 sprintf(lineString,"error (%d) vrf EE!",EErr); 236 dispLine(); 237 isDone=true; 238 break; 239 } else { 240 if (EE!=byte(_NUL[0])) { 241 sprintf(lineString,"(%ld) not NUL EE!",uc); 242 dispLine(); 243 isDone=true; 244 break; 245 } 246 } 247 sprintf(lineString,"(%ld) verified",count); 248 dispLine(); 249 isDone=true; 250 }//whileisDone 251 prompt(); 252} // end sdeeupload() 253 254 255void sdmr(const char *_s, const boolean _all) { // ---------------------------------------- MR / MRA 256 byte cnt=0; 257 if (!sd.exists(_s)) { // check if name exists 258 s_assign(lineString,"not found",sizeof(lineString)); 259 dispLine(); 260 } else { 261 File file; 262 file=sd.open(_s); 263 if (!file.isOpen()) { 264 s_assign(lineString,"open!",sizeof(lineString)); 265 dispLine(); 266 } 267 s_clear(lineString,sizeof(lineString)); 268 while (file.available()) { 269 data[0]=file.read(); 270 if (data[0]!=_CR[0] && data[0]!=_LF[0]) { 271 s_concat(lineString,data,sizeof(lineString)); 272 cnt++; 273 } 274 if (cnt > (sizeof(lineString)-1)) { 275 s_assign(lineString,"too long!",sizeof(lineString)); 276 dispLine(); 277 break; 278 } 279 if (_all==true && data[0]==_LF[0]) { // multiline-log reading MRA 280 cnt += 2; // include CR+LF counting 281 dispLine(); 282 } 283 } 284 file.close(); 285 if (_all==false) { dispLine(); } 286 sprintf(lineString,"(%d) bytes",cnt); 287 dispLine(); 288 } 289 prompt(); 290} // end sdmw() 291 292 293void sdmw(const char *_s, const char *_v, const boolean _append) { // -------------------------- MW / MWA 294 if (_append==false && sd.exists(_s)) { // check if name exists then delete it (MW only) 295 if (!sd.remove(_s)) { 296 s_assign(lineString,"rm error!",sizeof(lineString)); 297 dispLine(); 298 } 299 } 300 SdFile file(_s, O_WRONLY | O_CREAT | O_APPEND); 301 if (!file.isOpen()) { 302 s_assign(lineString,"opening!",sizeof(lineString)); 303 dispLine(); 304 } else { 305 file.println(_v); 306 file.close(); 307 if (_append==false) { 308 s_assign(lineString,"written",sizeof(lineString)); 309 } else { 310 s_assign(lineString,"added",sizeof(lineString)); 311 } 312 dispLine(); 313 } 314 prompt(); 315} // end sdmw() 316 317void sddir(const char *_s) { // ----------------------------- DIR 318 int fcount=0; 319 int dcount=0; 320 long fsize=0; 321 SdFile root; 322 SdFile file; 323 bool isDone=false; 324 char fname[14]=""; 325 while (isDone==false) { 326 if (!root.open(_s)) { // dir starting from 'path' 327 s_assign(lineString,"not found!",sizeof(lineString)); 328 dispLine(); 329 isDone=true; 330 break; 331 } 332 while (file.openNext (&root, O_RDONLY)) { 333 if (!file.isHidden()) { 334 if (file.isDir()) { 335 file.getName(fname,13); 336 s_assign(lineString,fname,sizeof(lineString)); 337 s_concat(lineString," <dir>",sizeof(lineString)); 338 dispLine(); 339 dcount++; 340 } 341 if (file.isFile()) { 342 file.getName(fname,13); 343 fsize=file.fileSize(); 344 s_assign(lineString,fname,sizeof(lineString)); 345 s_concat(lineString," (",sizeof(lineString)); 346 sprintf(fname,"%ld",fsize); 347 s_concat(lineString,fname,sizeof(lineString)); 348 s_concat(lineString,")",sizeof(lineString)); 349 dispLine(); 350 fcount++; 351 } 352 }//end !file.hidden 353 file.close(); 354 } //end whileOpenNext 355 root.close(); 356 s_assign(lineString,"(dir=",sizeof(lineString)); 357 sprintf(fname,"%d",dcount); 358 s_concat(lineString,fname,sizeof(lineString)); 359 s_concat(lineString," file=",sizeof(lineString)); 360 sprintf(fname,"%d",fcount); 361 s_concat(lineString,fname,sizeof(lineString)); 362 s_concat(lineString,")",sizeof(lineString)); 363 dispLine(); 364 isDone=true; 365 }//end while isDone 366 prompt(); 367} // end sddir() 368 369 370void sdhelp() { // ---------------------------------------------- HELP 371 sprintf(lineString,"%s","dir <path>"); 372 dispLine(); 373 sprintf(lineString,"%s","mr|mra <nm>"); 374 dispLine(); 375 sprintf(lineString,"%s","mw|mwa <nm>=<val>"); 376 dispLine(); 377 sprintf(lineString,"%s","del <nm>"); 378 dispLine(); 379 sprintf(lineString,"%s","eeupl <nm>"); 380 dispLine(); 381 sprintf(lineString,"%s","reset"); 382 dispLine(); 383 sprintf(lineString,"(free=%d)",s_freemem()); 384 dispLine(); 385 prompt(); 386} // end help() 387 388void prompt() { // prompt, ready for commands 389 sprintf(lineString,"%d>\ ",netMyPx); 390 dispLine(); 391} // end prompt() 392 393void dispLine() { // print information 394 s_concat(lineString, _CR, sizeof(lineString)); 395 txBackToSender(); // tx 396 s_clear(lineString, sizeof(lineString)); 397 delay(100); 398} // end dispLine() 399 400void receiveEvent(int howMany) { // i2c event incoming requests 401 if (isBusy==false) { 402 int counter=0; 403 s_clear(recCommand, sizeof(recCommand)); // sender command 404 recAddress=0; // sender address 405 recType=0; // sender cmd type 406 if (howMany == 0) { // ignores empty requests 407 isNetDataWaiting=false; 408 } else { 409 while (Wire.available()) { 410 data[0]=Wire.read(); 411 if (counter==0) { recType = data[0]; } // 1st char = cmd type 412 if (counter==1) { recAddress = data[0]; } // 2nd char = sender address 413 if (counter >1) { // other chars = command 414 s_concat(recCommand, data, sizeof(recCommand)); 415 } 416 counter++; 417 } 418 isNetDataWaiting=true; 419 } 420 }//endifbusy 421} // end receiveEvent() 422 423void netWhoIsEvent() { // i2c event: 11 bytes = 1=char(5) ENQ, 2-9=netMyNAME 10=netMyPx 11=netMyAddress 424 if (isBusy==false) { 425 char ccommand[65]=""; 426 char park[3]=""; 427 s_concat(ccommand, _ENQ, sizeof(ccommand)); // ENQ=answer whois 428 s_concat(ccommand, netMyNAME, sizeof(ccommand)); // netMyNAME 429 s_concat(ccommand, itoa(netMyPx,park,10), sizeof(ccommand)); // netMyPx 430 s_concat(ccommand, _MYADDR, sizeof(ccommand)); // netMyAddress 431 Wire.write(ccommand); 432 } 433} // end netWhoIsEvent() 434 435 436void txBackToSender() { // tx back to sender 437 char ccommand[65]=""; 438 s_assign(ccommand, _ACK, sizeof(ccommand)); // ACK=answer data 439 s_concat(ccommand, _MYADDR, sizeof(ccommand)); // netMyAddress 440 s_concat(ccommand, lineString, sizeof(ccommand)); // data 441 Wire.beginTransmission (recAddress); 442 Wire.write (ccommand); 443 Wire.endTransmission (true); 444} // end txBackToSender() 445 446 447void execNetCommand() { // executes received command 448 isBusy=true; 449 char park[65]=""; 450 char command[65]=""; 451 int x=0; 452 int its=-1; 453 if (recType == _BEL[0]) { // char(7) = BEL command type 454 isKnownCommand=false; 455 if (s_compare(recCommand,"")==0) { // empty command just do nothing 456 isKnownCommand=true; 457 prompt(); 458 } 459 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,0); // ? 460 if (s_compare(park,"?") == 0) { 461 isKnownCommand=true; 462 sdhelp(); 463 } 464 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,4); // reset 465 if (s_compare(park,"reset") == 0) { 466 isKnownCommand=true; 467 sdreset(); 468 } 469 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,2); // mw 470 if (s_compare(park,"mw ") == 0) { 471 for (x=0; x<strlen(recCommand);x++) { // search for "=" 472 s_substring(data,sizeof(data),recCommand,sizeof(recCommand),x,x); 473 if (strcmp(data,"=") == 0) { 474 its=x; 475 } 476 } 477 if (its >= 0) { 478 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),3,its-1); // filename 479 s_assign(command,memDIR,sizeof(command)); // memDIR 480 s_concat(command,park,sizeof(command)); // memDIR+filename 481 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),its+1,strlen(recCommand)-1); // value 482 isKnownCommand=true; 483 sdmw(command,park,false); 484 } 485 } 486 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,3); // mwa (append) 487 if (s_compare(park,"mwa ") == 0) { 488 for (x=0; x<strlen(recCommand);x++) { // search for "=" 489 s_substring(data,sizeof(data),recCommand,sizeof(recCommand),x,x); 490 if (strcmp(data,"=") == 0) { 491 its=x; 492 } 493 } 494 if (its >= 0) { 495 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),4,its-1); // filename 496 s_assign(command,logDIR,sizeof(command)); // logDIR 497 s_concat(command,park,sizeof(command)); // logDIR+filename 498 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),its+1,strlen(recCommand)-1); // value 499 isKnownCommand=true; 500 sdmw(command,park,true); 501 } 502 } 503 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,2); // mr 504 if (s_compare(park,"mr ") == 0) { 505 isKnownCommand=true; 506 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),3,strlen(recCommand)-1); // filename 507 s_assign(command,memDIR,sizeof(command)); // memDIR 508 s_concat(command,park,sizeof(command)); // memDIR+filename 509 sdmr(command,false); 510 } 511 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,3); // mra (multiline/log) 512 if (s_compare(park,"mra ") == 0) { 513 isKnownCommand=true; 514 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),4,strlen(recCommand)-1); // filename 515 s_assign(command,logDIR,sizeof(command)); // logDIR 516 s_concat(command,park,sizeof(command)); // logDIR+filename 517 sdmr(command,true); 518 } 519 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,3); // del 520 if (s_compare(park,"del ") == 0) { 521 isKnownCommand=true; 522 s_substring(command,sizeof(command),recCommand,sizeof(recCommand),4,strlen(recCommand)-1); 523 sddelete(command); 524 } 525 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,5); // eeupload 526 if (s_compare(park,"eeupl ") == 0) { 527 isKnownCommand=true; 528 s_substring(command,sizeof(command),recCommand,sizeof(recCommand),6,strlen(recCommand)-1); 529 sdeeupload(command); 530 } 531 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,3); // dir file 532 if (s_compare(park,"dir ") == 0) { 533 isKnownCommand=true; 534 s_substring(command,sizeof(command),recCommand,sizeof(recCommand),4,strlen(recCommand)-1); 535 sddir(command); 536 } 537 if (isKnownCommand == false) { 538 s_assign(lineString,"unknown command",sizeof(lineString)); 539 dispLine(); 540 prompt(); 541 } 542 } // endif recType 543 s_clear(recCommand,sizeof(recCommand)); 544 recType=0; 545 recAddress=0; 546 isNetDataWaiting=false; 547 isBusy=false; 548} // end execNetCommand() 549 550// reading eeprom 551byte readEEPROM (const int _disk, const unsigned int _addr, byte *EErr ) { 552 *EErr=0; 553 byte _rxbyte=0; 554 Wire.beginTransmission(_disk); 555 Wire.write(_addr); 556 if (Wire.endTransmission() == 0) { 557 Wire.requestFrom(_disk,1); 558 if (Wire.available()) { 559 _rxbyte = Wire.read(); 560 } else { 561 *EErr = 1; //"READ no data available" 562 } 563 } else { 564 *EErr = 2; //"READ eTX error" 565 } 566 Wire.endTransmission (true); 567 return _rxbyte; 568} 569 570// writing eeprom 571void writeEEPROM (const int _disk, const unsigned int _addr, byte _txbyte, byte *EErr) { 572 *EErr=0; 573 Wire.beginTransmission(_disk); 574 Wire.write(_addr); 575 Wire.write(_txbyte); 576 if (Wire.endTransmission() != 0) { 577 *EErr = 3; //"WRITING eTX error" 578 } 579 Wire.endTransmission (true); 580 delay(5); 581} 582 583 584//------------------------------------------------------------------------- CLEAR 585void s_clear(char *dest_source_string, const int dest_sizeof) { // fills-up with NUL=chr(0) 586 memset(dest_source_string, 0, dest_sizeof); 587} // end s_clear() 588 589//------------------------------------------------------------------------- ASSIGN 590bool s_assign(char *dest_string, const char *source_string, const int dest_sizeof) { // copies source to dest 591 int _LenS=strlen(source_string); // how many bytes 592 if (_LenS > (dest_sizeof - 1)) { 593 return true; // err 1 594 } else { 595 strcpy(dest_string, source_string); 596 return false; // ok 0 597 } 598} // end s_assign() 599 600//------------------------------------------------------------------------- SUBSTRING 601bool s_substring(char *dest_string, const int dest_sizeof, const char *source_string, 602 const int source_sizeof, const int source_from, const int source_to) { // copies source(from, to) to dest 603 if ((source_from < 0) || (source_to < source_from) || ((source_to - source_from + 1) > (dest_sizeof - 1)) 604 || (source_to >= (source_sizeof-1)) || ((source_to - source_from + 1) > (strlen(source_string)))) { 605 dest_string[0]=0; // NUL 606 return true; // err 1 607 } else { 608 int _Count=0; 609 for (int i=source_from;i<(source_to+1);i++) { 610 dest_string[_Count]=source_string[i]; 611 _Count++; 612 } 613 dest_string[_Count]=0; // ends with NUL 614 return false; // ok 0 615 } 616} // end s_substring() 617 618//------------------------------------------------------------------------- CONCAT 619bool s_concat(char *dest_string, const char *source_string, const int dest_sizeof) { // append source to dest 620 int _LenS=strlen(source_string); // how many bytes source 621 int _LenD=strlen(dest_string); // how many bytes dest 622 if ((_LenS + _LenD) > (dest_sizeof - 1)) { 623 return true; // err 1 624 } else { 625 strcat(dest_string, source_string); 626 return false; // ok 0 627 } 628} // end s_concat() 629 630//------------------------------------------------------------------------- COMPARE 631bool s_compare(const char *dest_string, const char *source_string) { // compares source with dest 632 int _LenS=strlen(source_string); // how many bytes source 633 int _LenD=strlen(dest_string); // how many bytes dest 634 if (_LenS != _LenD) { // different length 635 return true; // are different 1 636 } else { 637 if (strcmp(dest_string, source_string) == 0) { 638 return false; // are the same 0 639 } else { 640 return true; // are different 1 641 } 642 } 643} // end s_compare() 644 645//------------------------------------------------------------------------- FREEMEM 646int s_freemem() { 647 extern int __heap_start,*__brkval; 648 int v; 649 return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); 650} // end s_freemem() 651 652 653 654//************************************************************************************* 655 656/* 657 658void sdtype(String filename, byte mode) { // ----------------------------- TYPE 659 char data=0; // sdtype, displays the content of a file 660 int count=0; // mode 0=CHAR 1=BIN 2=HEX 661 byte vert=0; 662 bool isAlreadyAtNL=true; 663 String STemp=""; 664 STemp.reserve(64); 665 File SDfile; 666 if (!SD.exists(filename)) { // check if file does exist 667 lineString="SD file not found!"; 668 dispLine(lineString); 669 } else { 670 SDfile = SD.open(filename); // open file 671 while(SDfile.available()) { // read all file, byte by byte 672 data = (SDfile.read()); 673 switch (mode) { 674 case 0: // CHAR 675 lineString=String(data); 676 dispChar(lineString); 677 break; 678 case 1: // BIN 679 STemp="00000000"+String(data,BIN); // fill with non significant bits 680 lineString=STemp.substring(STemp.length()-9,STemp.length())+" "; 681 dispChar(lineString); 682 vert ++; 683 if (vert == 3) { 684 dispLine(""); // newline 685 isAlreadyAtNL=true; 686 vert=0; 687 } else { 688 isAlreadyAtNL=false; 689 } 690 break; 691 case 2: // HEX 692 STemp="00"+String(data,HEX)+" "; // fill with non significant 0 693 lineString=STemp.substring(STemp.length()-3,STemp.length())+" "; 694 dispChar(lineString); 695 vert ++; 696 if (vert == 8) { 697 dispLine(""); // newline 698 isAlreadyAtNL=true; 699 vert=0; 700 } else { 701 isAlreadyAtNL=false; 702 } 703 break; 704 } 705 count ++; 706 } //end while 707 if (isAlreadyAtNL==false) {dispLine("");}; // empty line at the end 708 lineString="file size ("+String(SDfile.size(),DEC)+"), bytes read ("+String(count,DEC)+")"; // file size 709 dispLine(lineString); 710 SDfile.close(); 711 } 712 prompt(); 713} // end sdtype() 714 715 716 if (recCommand.substring(0,4) == "type") { 717 isKnownCommand=true; 718 command=recCommand.substring(5,recCommand.length()-4); 719 if (recCommand.substring(recCommand.length()-3,recCommand.length())=="CHR") { x=0; }; 720 if (recCommand.substring(recCommand.length()-3,recCommand.length())=="BIN") { x=1; }; 721 if (recCommand.substring(recCommand.length()-3,recCommand.length())=="HEX") { x=2; }; 722 sdtype(command,x); 723 } 724 725 726 727 728 729 730void sdfilesize(const char *_s) { // ------------------------------------------- FILESIZE 731 File file; 732 bool isDone=false; 733 long fsize=0; 734 while (isDone==false) { 735 if (!sd.exists(_s)) { // check if name exists 736 s_assign(lineString,"nm not found",sizeof(lineString)); 737 dispLine(); 738 isDone=true; 739 break; 740 } 741 file=sd.open(_s); 742 if (!file.isOpen()) { 743 s_assign(lineString,"nm open!",sizeof(lineString)); 744 dispLine(); 745 isDone=true; 746 break; 747 } 748 fsize=file.fileSize(); 749 sprintf(lineString,"size=%ld",fsize); 750 dispLine(); 751 isDone=true; 752 }//endwhile 753 prompt(); 754} 755 756 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,4); // size 757 if (s_compare(park,"size ") == 0) { 758 isKnownCommand=true; 759 s_substring(command,sizeof(command),recCommand,sizeof(recCommand),5,strlen(recCommand)-1); 760 sdfilesize(command); 761 } 762 763 764void sdrmdir(const char *_s) { // ----------------------------- RMDIR 765 if (!sd.exists(_s)) { // check if dir exists 766 s_assign(lineString,"not found!",sizeof(lineString)); 767 dispLine(); 768 } else { 769 if (sd.rmdir(_s)) { // deletes dir 770 s_assign(lineString,"removed",sizeof(lineString)); 771 dispLine(); 772 } else { 773 s_assign(lineString,"not empty!",sizeof(lineString)); 774 dispLine(); 775 } 776 } 777 prompt(); 778} // end sdrmdir() 779 780 781void sdmkdir(const char *_s) { // ----------------------------- MKDIR 782 if (sd.exists(_s)) { // check if dir already exists 783 s_assign(lineString,"nm exists!",sizeof(lineString)); 784 dispLine(); 785 } else { 786 if (sd.mkdir(_s)) { // builds dir 787 s_assign(lineString,"made",sizeof(lineString)); 788 dispLine(); 789 } else { 790 s_assign(lineString,"nm error!",sizeof(lineString)); 791 dispLine(); 792 } 793 } 794 prompt(); 795} // end sdmkdir() 796 797 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,5); // mkdir 798 if (s_compare(park,"mkdir ") == 0) { 799 isKnownCommand=true; 800 s_substring(command,sizeof(command),recCommand,sizeof(recCommand),6,strlen(recCommand)-1); 801 sdmkdir(command); 802 } 803 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,5); // rmdir 804 if (s_compare(park,"rmdir ") == 0) { 805 isKnownCommand=true; 806 s_substring(command,sizeof(command),recCommand,sizeof(recCommand),6,strlen(recCommand)-1); 807 sdrmdir(command); 808 } 809 810 811 812*/ 813
CONSOLE Arduino code
arduino
1/* Arduino Net-P (i2c processors network) by Zonca Marco 2020 2 * 'netMyNAME' ("CONSOLE ") max 8 char, 'netMyID' (p0) max p9, address='netMyAddress' max 0xFF, 'netMyPx' (0) 3 * 4 * implemented commands (* = to do): scan, ckpower, ?, <pxname> <cmd>, reset 5 * 6 * scan: scan [inquiry i2c bus, 7 * i.e. scan 8 * returns a list of active addresses on the bus in form "DDD (0xEE)"... 9 * ends with "'n' found"] 10 * ckpower: ckpower [inquiry power situation 11 * i.e. ckpower 12 * returns "v3=3.3 v5=5.0 vraw=7.0-12.0"] 13 * ?: ? [ask for the list of commands 14 * i.e. ? 15 * returns a list of available commands on board] 16 * reset: reset [force the MPU to reset 17 * i.e. reset] 18 * 19 * <pxname> <cmd>: [sends <cmd> to the processor <pxname> 20 * i.e. SD dir / 21 * returns result of <cmd> from processor <pxname>] 22 * 23 */ 24 25#include <Adafruit_GFX_AS.h> // Core graphics library customized 26#include <Adafruit_ILI9341_AS.h> // Hardware-specific library customized 27#include <SPI.h> 28#include <SoftwareSerial.h> 29#include <Wire.h> 30#include <NewTone.h> 31#include <PS2Keyboard.h> 32 33// tft pins used for the MINI PRO, we must use hardware SPI 34#define _sclk 13 35#define _miso 12 // Not used 36#define _mosi 11 37#define _cs 10 38#define _rst 8 39#define _dc 9 40 41// Must use hardware SPI for speed 42Adafruit_ILI9341_AS tft = Adafruit_ILI9341_AS(_cs, _dc, _rst); 43 44// The scrolling area must be a integral multiple of TEXT_HEIGHT 45#define TEXT_HEIGHT 16 // Height of text to be printed and scrolled 46#define BOT_FIXED_AREA 0 // Number of lines in bottom fixed area (lines counted from bottom of screen) 47#define TOP_FIXED_AREA 16 // Number of lines in top fixed area (lines counted from top of screen) 48 49#define btTX 4 // to "TX-O" pin of bluetooth terminal 50#define btRX 5 // to "RX-I" pin of bluetooth terminal 51#define btSPEED 38400 // communication speed both serial/keyboard and bluetooth 52 53SoftwareSerial bluetooth(btTX, btRX); // bluetooth terminal TX+RX 54PS2Keyboard keyboard; 55 56// The initial y coordinate of the top of the scrolling area 57uint16_t yStart = TOP_FIXED_AREA; 58// yArea must be a integral multiple of TEXT_HEIGHT 59uint16_t yArea = 320-TOP_FIXED_AREA-BOT_FIXED_AREA; 60// The initial y coordinate of the top of the bottom text line 61uint16_t yDraw = 320 - BOT_FIXED_AREA - TEXT_HEIGHT; 62 63// Keep track of the drawing x coordinate 64uint16_t xPos = 0; 65// For the byte we read from the serial port and display 66char data[2] = ""; 67 68// We have to blank the top line each time the display is scrolled, but this takes up to 13 milliseconds 69// for a full width line, meanwhile the serial buffer may be filling... and overflowing 70// We can speed up scrolling of short text lines by just blanking the character we drew 71// We keep all the strings pixel lengths to optimise the speed of the top line blanking 72int blank[19]; 73 74// other staff 75char lineString[65]=""; 76char command[65]=""; 77char recCommand[65]=""; 78byte recAddress=0; 79byte recType=0; 80char park[65]=""; 81char netRegisterNames[10][10]={}; 82 83bool displayAlsoOnSerial = false; 84bool isKnownCommand = false; 85bool isNetDataWaiting = false; 86bool isBusy=false; 87byte netRegisterAddrs[10] = {}; 88unsigned long prevSCANBUSmillis=0; 89 90const int buzzerPin = 7; 91const int vin3Pin = 14; 92const int vin5Pin = 15; 93const int vin12Pin = 16; 94const int netMyPx = 0; // netMyPx 0-9 95const int netMyAddress = 0x0a; // i2c address 96const char *netMyID = "p0"; // netMyID 97const char *netMyNAME = "CONSOLE "; // netMyNAME 98const int ps2DataPin = 2; 99const int ps2IRQpin = 3; 100const int SCANBUSinterval = 30000; 101const int ResetPin=17; // A3 put low for reset 102 103char _CR[2]=""; // CR 104char _BEL[2]=""; // BEL 105char _ENQ[2]=""; // ENQ 106char _ACK[2]=""; // ACK 107char _SPACE[2]=""; // SPACE 108char _MYADDR[2]=""; // netMyAddress 109 110void setup() { 111 //Serial.begin(btSPEED); // serial/keyboard RX only 112 bluetooth.begin(btSPEED); // bluetooth terminal TX/RX 113 keyboard.begin(ps2DataPin, ps2IRQpin); 114 Wire.begin(netMyAddress); 115 Wire.onReceive(receiveEvent); // i2c event 116 117 _CR[0] = 13; // CR 118 _BEL[0] = 7; // BEL 119 _ENQ[0] = 5; // ENQ 120 _ACK[0] = 6; // ACK 121 _SPACE[0] = 32; // SPACE 122 _MYADDR[0] = netMyAddress; 123 124 netRegisterAddrs[0]=netMyAddress; 125 s_trimall(park,netMyNAME,sizeof(park)); 126 s_assign(netRegisterNames[0],park,sizeof(netRegisterNames[0])); // 0 it is me 127 128 // Setup the TFT display 129 tft.init(); 130 tft.setRotation(0); 131 tft.fillScreen(ILI9341_BLACK); 132 133 // Setup scroll area 134 setupScrollArea(TOP_FIXED_AREA, BOT_FIXED_AREA); 135 136 // Top banner 137 tft.setTextColor(ILI9341_BLACK, ILI9341_GREEN); 138 tft.fillRect(0,0,240,16, ILI9341_GREEN); 139 s_assign(park," Net-P Console ",sizeof(park)); 140 tft.drawCentreString(park,120,0,2); 141 142 // Change colour for scrolling zone 143 tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); 144 145 // Zero the array 146 for (byte i = 0; i<18; i++) blank[i]=0; 147 148 delay(2000); // lets other Px to start before "scanbus" 149 scanbus(false); 150 sprintf(lineString,"%s\ ","Ready"); 151 dispLine(); 152 NewTone(buzzerPin,200,50); 153 prompt(); 154} 155 156void loop(void) { 157 if ((prevSCANBUSmillis+SCANBUSinterval) < millis()) { // plug & play 'px' 158 scanbus(false); 159 prevSCANBUSmillis=millis(); 160 } 161 if (bluetooth.available() > 0) { // one character from bluetooth terminal if any 162 data[0]=bluetooth.read(); 163 if (displayAlsoOnSerial==true) { // usually false 164 //Serial.print((char)data[0]); // to serial/keyboard 165 if (data[0] == '\r') { // cr=cr+lf 166 //Serial.print('\n'); 167 } 168 } 169 tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); 170 dispData(); // send to display 171 evaluateCommands(); // send to evaluate commands 172 } 173 if (keyboard.available()) { // one character from ps2 serial keyboard if any 174 data[0] = keyboard.read(); 175 tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); 176 bluetooth.print((char)data[0]); // send to bluetooth terminal 177 dispData(); // send to display 178 evaluateCommands(); // send to evaluate commands 179 } 180 if (isNetDataWaiting==true) { 181 execNetCommand(); 182 } 183} // end loop() 184 185 186void evaluateCommands() { // evaluate commands 187 byte x=0; 188 byte ln=0; 189 char cmd[65]=""; 190 if (data[0] == 13) { // CR (eol) pressed 191 isKnownCommand=false; 192 if (s_compare(command,"")==0) { // empty command just do nothing 193 isKnownCommand=true; 194 prompt(); 195 } 196 s_substring(park,sizeof(park),command,sizeof(command),0,4); // reset 197 if (s_compare(park,"reset") == 0) { 198 isKnownCommand=true; 199 reset(); 200 } 201 s_substring(park,sizeof(park),command,sizeof(command),0,3); // scanbus 202 if (s_compare(park,"scan") == 0) { 203 isKnownCommand=true; 204 scanbus(true); // true=displays results, false=mute 205 } 206 s_substring(park,sizeof(park),command,sizeof(command),0,6); // checkpower 207 if (s_compare(park,"ckpower") == 0) { 208 isKnownCommand=true; 209 checkpower(); 210 } 211 s_substring(park,sizeof(park),command,sizeof(command),0,0); // ? 212 if (s_compare(park,"?") == 0) { 213 isKnownCommand=true; 214 help(); 215 } 216 for (x=0;x<10;x++) { // search processor's name for command 217 if (x!=netMyPx && netRegisterNames[x][0] != 0) { // not me, not unknown 218 s_assign(park,netRegisterNames[x],sizeof(park)); 219 s_concat(park," ",sizeof(park)); 220 ln=strlen(park); 221 s_substring(cmd,sizeof(cmd),command,sizeof(command),0,(ln-1)); 222 if (s_compare(cmd,park) == 0) { // known processor name 223 isKnownCommand=true; 224 s_substring(cmd,sizeof(cmd),command,sizeof(command),ln,strlen(command)-1); // clean command for target processor 225 askCommandOnPx(_BEL[0], x, netRegisterAddrs[x], cmd); // 7=BEL=cmdtype 226 } 227 } 228 } 229 if (isKnownCommand == false) { 230 sprintf(lineString,"%s\ ","unknown cmd"); 231 dispLine(); 232 prompt(); 233 } 234 s_clear(command,sizeof(command)); 235 } 236 if (data[0] > 31 && data[0] < 127) { // only visible ASCII chars (127=DEL) 237 s_concat(command,data,sizeof(command)); 238 } 239} // end evaluateCommands() 240 241 242void dispData() { // display single character at a time 243 if (data[0] == _CR[0] || xPos>231) { 244 xPos = 0; 245 yDraw = scroll_line(); // It takes about 13ms to scroll 16 pixel lines 246 } 247 if (data[0] > 31 && data[0] < 127) { // only visible ASCII chars (127=DEL) 248 xPos += tft.drawChar(data[0],xPos,yDraw,2); 249 blank[(18+(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT)%19]=xPos; // Keep a record of line lengths 250 } 251} // end dispData() 252 253 254void terminalData() { // send to terminal, serial and bluetooth, single char at a time 255 if (displayAlsoOnSerial==true) { // usually false 256 //Serial.print((char)data[0]); // to serial/keyboard 257 if (data[0] == _CR[0]) { // cr=cr+lf 258 //Serial.print('\n'); 259 } 260 } 261 bluetooth.print((char)data[0]); // send to bluetooth terminal 262} // end terminalData() 263 264 265void dispLine() { // prepare single char by single char 266 byte i=0; // for display and serial and bluetooth terminal (for internal generated text) 267 for (i=0; i<strlen(lineString); i++) { 268 data[0]=lineString[i]; 269 tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK); 270 dispData(); 271 terminalData(); 272 } 273 s_clear(lineString, sizeof(lineString)); 274} // end dispString() 275 276 277void help() { // provide a list of available commands 278 sprintf(lineString,"%s\ ","scan"); 279 dispLine(); 280 sprintf(lineString,"%s\ ","ckpower"); 281 dispLine(); 282 sprintf(lineString,"%s\ ","<pxname> <cmd>"); 283 dispLine(); 284 sprintf(lineString,"%s\ ","reset"); 285 dispLine(); 286 sprintf(lineString,"(free=%d)\ ",s_freemem()); 287 dispLine(); 288 prompt(); 289} // end help() 290 291void prompt() { // prompt, ready for commands 292 sprintf(lineString,"%d>\ ",netMyPx); 293 dispLine(); 294} // end prompt() 295 296 297void checkpower() { // calculate voltages onboard 3v, 5v and raw (12v) 298 float n=0; // sprintf does not work with float 299 float n1=0; // then I print separate int.dec parts of numbers 300 int v3i=0; 301 int v3d=0; 302 int v5i=0; 303 int v5d=0; 304 int vrawi=0; 305 int vrawd=0; 306 307 n = analogRead(vin3Pin); // v3 308 n1=(((6.60 * n) / 1023.00)); 309 n=(n1 + ((n1 * 1.0) /100)); // arbitrary correction in %) 310 v3i=n; 311 v3d=float((n-v3i)*100); 312 313 n = analogRead(vin5Pin); // v5 314 n1=(((6.60 * n) / 1023.00)); 315 n=(n1 + ((n1 * 1.0) /100)); // arbitrary correction in %) 316 v5i=n; 317 v5d=float((n-v5i)*100); 318 319 n = analogRead(vin12Pin); // vraw 320 n1=(((13.2 * n) / 1023.00)); 321 n=(n1 + ((n1 * 3.0) /100)); // arbitrary correction in %) 322 vrawi=n; 323 vrawd=float((n-vrawi)*100); 324 325 sprintf(lineString, "v3=%d.%02d v5=%d.%02d vraw=%d.%02d\ ",v3i,v3d,v5i,v5d,vrawi,vrawd); 326 dispLine(); 327 prompt(); 328} // end checkpower 329 330 331void reset() { // ------------------------------------------------------------ RESET 332 analogWrite(ResetPin,LOW); 333} 334 335 336void scanbus(bool isInteractive) { // scans i2c bus for active addresses 337 byte count = 0; // true=interactive and force refresh, false=mute and ask for new only 338 byte i=0; 339 int px=0; 340 if (isInteractive==true) { // shows it and clear Register -> force refresh 341 sprintf(lineString,"%s\ ","Scanning..."); 342 dispLine(); 343 for (px=0;px<10;px++) { // clear Register 344 netRegisterAddrs[px] = 0; 345 s_clear(netRegisterNames[px],sizeof(netRegisterNames[px])); 346 } 347 } 348 for (i = 8; i < 128; i++) { // addresses 0-7 are reserved (7 bits address) 349 Wire.beginTransmission (i); 350 delay (10); 351 if (Wire.endTransmission (true) == 0 || i==netMyAddress) { // it blocks here if no i2c available! put pullup resistors and check if a device is blocking to GND pins 352 sprintf(lineString,"%3d (0x%02X) ",i,i); 353 if (i==netMyAddress) { 354 netRegisterAddrs[netMyPx] = netMyAddress; 355 s_trimall(netRegisterNames[netMyPx],netMyNAME,sizeof(netRegisterNames[netMyPx])); 356 s_concat(lineString,netMyID,sizeof(lineString)); 357 s_concat(lineString," ",sizeof(lineString)); 358 s_concat(lineString,netRegisterNames[netMyPx],sizeof(lineString)); 359 s_concat(lineString," [me]",sizeof(lineString)); 360 } else { 361 px=whois(int(i)); // check address 362 if (px < 10) { // 0-9 is a "p", 99 is unknown 363 sprintf(park,"p%d %s",px,netRegisterNames[px]); 364 s_concat(lineString,park,sizeof(lineString)); 365 } 366 } 367 s_concat(lineString,_CR,sizeof(lineString)); 368 if (isInteractive==true) { dispLine(); }; // shows it 369 count++; 370 } 371 } 372 if (isInteractive==true) { // shows it 373 sprintf(lineString,"%d found\ ",count); 374 dispLine(); 375 prompt(); 376 } 377} // end scanbus() 378 379 380void askCommandOnPx(const int cmdtype, const int px, const int addrs, const char *cmd) { // ask target processor to execute a command 381 char ccommand[65]=""; 382 s_assign(ccommand, _BEL, sizeof(ccommand)); // BEL 383 s_concat(ccommand, _MYADDR, sizeof(ccommand)); // netMyAddress 384 s_concat(ccommand, cmd, sizeof(ccommand)); // cmd 385 prompt(); 386 Wire.beginTransmission (addrs); // ask 387 Wire.write(ccommand); 388 Wire.endTransmission (true); 389} // end asksd() 390 391 392int whois(int address) { // ask name+ID on i2c net, expecting char(5) ENQ as 1st byte 393 byte i=0; 394 char xtype=0; 395 int xid=0; 396 char xaddr=0; 397 char park1[65]=""; 398 s_clear(park,sizeof(park)); 399 s_clear(park1,sizeof(park1)); 400 Wire.requestFrom(address, 11); // send request, dest address + nr of bytes 401 delay(10); 402 i=0; 403 while(Wire.available()) { // read answer 404 data[0]=Wire.read(); 405 i++; 406 if (i==1) { xtype=data[0]; }; // byte 1 = answer type must be char(5) ENQ 407 if (i>1 && i<10) { // bytes 2-9 = name 408 park[i-2] = data[0]; 409 } 410 if (i==10) { xid=atoi(data); }; // byte 10=id 411 if (i==11) { xaddr=data[0]; }; // byte 11=address 412 } 413 if ((xtype == _ENQ[0]) && (int(xaddr) == address) && (xid < 10)) { // must be a "p"= 0-9, and 10th byte = address, or unknown/wrong answer 414 netRegisterAddrs[int(xid)] = xaddr; // mem id and address 415 s_trimall(park1,park,sizeof(park1)); 416 s_assign(netRegisterNames[int(xid)],park1,sizeof(netRegisterNames[int(xid)])); 417 return xid; // recognized address as "p" 418 } else { 419 return 99; // unknown address or answer 420 } 421} // end whois() 422 423 424void execNetCommand() { // executes received command 425 isBusy=true; 426 s_clear(park, sizeof(park)); 427 s_clear(command, sizeof(command)); 428 if (recType == _BEL[0]) { // char(7) = BEL command type ---------------- BEL=incoming request 429 isKnownCommand=false; 430 if (s_compare(recCommand,"")==0) { // empty command just do nothing 431 isKnownCommand=true; 432 prompt(); 433 } 434 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,0); // ? 435 if (s_compare(park,"?") == 0) { 436 isKnownCommand=true; 437 help(); 438 } 439 if (isKnownCommand == false) { 440 s_assign(lineString,"unknown cmd",sizeof(lineString)); 441 dispLine(); 442 prompt(); 443 } 444 } // endif recType=BEL 445 if (recType == _ACK[0]) { // char(6) = ACK command type ---------------- ACK=incoming answer 446 s_assign(lineString,recCommand,sizeof(lineString)); 447 dispLine(); 448 } // endif recType=ACK 449 s_clear(recCommand,sizeof(recCommand)); 450 recType=0; 451 recAddress=0; 452 isNetDataWaiting=false; 453 isBusy=false; 454} // end execNetCommand() 455 456void receiveEvent(int howMany) { // i2c event incoming requests 457 if (isBusy==false) { 458 int counter=0; 459 s_clear(recCommand, sizeof(recCommand)); // sender command 460 recAddress=0; // sender address 461 recType=0; // sender cmd type 462 if (howMany == 0) { // ignores empty requests 463 isNetDataWaiting=false; 464 } else { 465 while (Wire.available()) { 466 data[0]=Wire.read(); 467 if (counter==0) { recType = data[0]; } // 1st char = cmd type 468 if (counter==1) { recAddress = data[0]; } // 2nd char = sender address 469 if (counter >1) { // other chars = command 470 s_concat(recCommand, data, sizeof(recCommand)); 471 } 472 counter++; 473 } 474 isNetDataWaiting=true; 475 } 476 }//endisBusy 477} // end receiveEvent() 478 479 480//------------------------------------------------------------------------- CLEAR 481void s_clear(char *dest_source_string, const int dest_sizeof) { // fills-up with NUL=chr(0) 482 memset(dest_source_string, 0, dest_sizeof); 483} // end s_clear() 484 485//------------------------------------------------------------------------- ASSIGN 486bool s_assign(char *dest_string, const char *source_string, const int dest_sizeof) { // copies source to dest 487 int _LenS=strlen(source_string); // how many bytes 488 if (_LenS > (dest_sizeof - 1)) { 489 return true; // err 1 490 } else { 491 strcpy(dest_string, source_string); 492 return false; // ok 0 493 } 494} // end s_assign() 495 496//------------------------------------------------------------------------- SUBSTRING 497bool s_substring(char *dest_string, const int dest_sizeof, const char *source_string, 498 const int source_sizeof, const int source_from, const int source_to) { // copies source(from, to) to dest 499 if ((source_from < 0) || (source_to < source_from) || ((source_to - source_from + 1) > (dest_sizeof - 1)) 500 || (source_to >= (source_sizeof-1)) || ((source_to - source_from + 1) > (strlen(source_string)))) { 501 dest_string[0]=0; // NUL 502 return true; // err 1 503 } else { 504 int _Count=0; 505 for (int i=source_from;i<(source_to+1);i++) { 506 dest_string[_Count]=source_string[i]; 507 _Count++; 508 } 509 dest_string[_Count]=0; // ends with NUL 510 return false; // ok 0 511 } 512} // end s_substring() 513 514//------------------------------------------------------------------------- CONCAT 515bool s_concat(char *dest_string, const char *source_string, const int dest_sizeof) { // append source to dest 516 int _LenS=strlen(source_string); // how many bytes source 517 int _LenD=strlen(dest_string); // how many bytes dest 518 if ((_LenS + _LenD) > (dest_sizeof - 1)) { 519 return true; // err 1 520 } else { 521 strcat(dest_string, source_string); 522 return false; // ok 0 523 } 524} // end s_concat() 525 526//------------------------------------------------------------------------- COMPARE 527bool s_compare(const char *dest_string, const char *source_string) { // compares source with dest 528 int _LenS=strlen(source_string); // how many bytes source 529 int _LenD=strlen(dest_string); // how many bytes dest 530 if (_LenS != _LenD) { // different length 531 return true; // are different 1 532 } else { 533 if (strcmp(dest_string, source_string) == 0) { 534 return false; // are the same 0 535 } else { 536 return true; // are different 1 537 } 538 } 539} // end s_compare() 540 541//------------------------------------------------------------------------- TRIMALL 542bool s_trimall(char *dest_string, const char *source_string, const int dest_sizeof) { // eliminate all 'spaces' from source to dest 543 int _LenS=strlen(source_string); // how many bytes max 544 if (_LenS > (dest_sizeof - 1)) { 545 return true; // err 1 546 } else { 547 int _Count=0; 548 for (int i=0;i<(_LenS);i++) { 549 if (source_string[i] != _SPACE[0]){ 550 dest_string[_Count]=source_string[i]; 551 _Count++; 552 } 553 } 554 dest_string[_Count]=0; // ends with NUL 555 return false; // ok 0 556 } 557} // end s_trimall() 558 559//------------------------------------------------------------------------- FREEMEM 560int s_freemem() { 561 extern int __heap_start,*__brkval; 562 int v; 563 return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); 564} // end s_freemem() 565 566 567 568// ############################################################################################## 569// Call this function to scroll the display one text line 570// ############################################################################################## 571int scroll_line() { 572 int yTemp = yStart; // Store the old yStart, this is where we draw the next line 573 // Use the record of line lengths to optimise the rectangle size we need to erase the top line 574 tft.fillRect(0,yStart,blank[(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT],TEXT_HEIGHT, ILI9341_BLACK); 575 576 // Change the top of the scroll area 577 yStart+=TEXT_HEIGHT; 578 // The value must wrap around as the screen memory is a circular buffer 579 if (yStart >= 320 - BOT_FIXED_AREA) yStart = TOP_FIXED_AREA + (yStart - 320 + BOT_FIXED_AREA); 580 // Now we can scroll the display 581 scrollAddress(yStart); 582 return yTemp; 583} 584 585// ############################################################################################## 586// Setup a portion of the screen for vertical scrolling 587// ############################################################################################## 588// We are using a hardware feature of the display, so we can only scroll in portrait orientation 589void setupScrollArea(uint16_t TFA, uint16_t BFA) { 590 tft.writecommand(ILI9341_VSCRDEF); // Vertical scroll definition 591 tft.writedata(TFA >> 8); 592 tft.writedata(TFA); 593 tft.writedata((320-TFA-BFA)>>8); 594 tft.writedata(320-TFA-BFA); 595 tft.writedata(BFA >> 8); 596 tft.writedata(BFA); 597} 598 599// ############################################################################################## 600// Setup the vertical scrolling start address 601// ############################################################################################## 602void scrollAddress(uint16_t VSP) { 603 tft.writecommand(ILI9341_VSCRSADD); // Vertical scrolling start address 604 tft.writedata(VSP>>8); 605 tft.writedata(VSP); 606} 607
UPDATER Arduino code
arduino
1 2/* Arduino Net-P (i2c processors network) by Zonca Marco 2020 3 * 'netMyNAME' ("UPDATER ") max 8 char, 'netMyID' (p8) max 2 char, 4 * address='netMyAddress' (0x15) max 0xFF, 'netMyPx' (8) 5 * 6 * implemented commands (* = to do): ?, reset 7 * 8 */ 9 10#include <Wire.h> 11 12const int netMyPx = 8; // netMyPx 0-9 13const int netMyAddress = 0x15; // i2c address 0x15=HEX 21=DEC 14const char *netMyID = "p8"; // netMyID 15const char *netMyNAME = "UPDATER "; // netMyNAME 16const int ResetPin=17; // A3 put low for reset 17 18char _CR[2]=""; // CR 19char _BEL[2]=""; // BEL 20char _ENQ[2]=""; // ENQ 21char _ACK[2]=""; // ACK 22char _SPACE[2]=""; // SPACE 23char _MYADDR[2]=""; // netMyAddress 24 25char lineString[65]=""; 26char recCommand[65]=""; 27byte recAddress=0; 28byte recType=0; 29char data[2]=""; 30bool isNetDataWaiting=false; 31bool isKnownCommand = false; 32bool isBusy=false; 33 34 35void setup() { 36 //Serial.begin(38400); 37 Wire.begin(netMyAddress); 38 Wire.onReceive(receiveEvent); // i2c event 39 Wire.onRequest(netWhoIsEvent); // i2c whois 40 _CR[0] = 13; // CR 41 _BEL[0] = 7; // BEL 42 _ENQ[0] = 5; // ENQ 43 _ACK[0] = 6; // ACK 44 _SPACE[0] = 32; // SPACE 45 _MYADDR[0] = netMyAddress; 46} // end setup() 47 48 49void loop() { 50 if (isNetDataWaiting == true) { 51 execNetCommand(); 52 } 53} // end loop() 54 55 56void execNetCommand() { // executes received command 57 isBusy=true; 58 char park[65]=""; 59 if (recType == _BEL[0]) { // char(7) = BEL command type 60 isKnownCommand=false; 61 if (s_compare(recCommand,"")==0) { // empty command just do nothing 62 isKnownCommand=true; 63 prompt(); 64 } 65 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,4); // reset 66 if (s_compare(park,"reset") == 0) { 67 isKnownCommand=true; 68 reset(); 69 } 70 s_substring(park,sizeof(park),recCommand,sizeof(recCommand),0,0); // ? 71 if (s_compare(park,"?") == 0) { 72 isKnownCommand=true; 73 sdhelp(); 74 } 75 if (isKnownCommand == false) { 76 s_assign(lineString,"unknown command",sizeof(lineString)); 77 dispLine(); 78 prompt(); 79 } 80 } // endif recType 81 s_clear(recCommand,sizeof(recCommand)); 82 recType=0; 83 recAddress=0; 84 isNetDataWaiting=false; 85 isBusy=false; 86} // end execNetCommand() 87 88 89void reset() { // ------------------------------------------------------------ RESET 90 analogWrite(ResetPin,LOW); 91} 92 93 94void sdhelp() { // ---------------------------------------------- HELP 95 s_assign(lineString, "reset", sizeof(lineString)); 96 dispLine(); 97 sprintf(lineString,"(free=%d)",s_freemem()); 98 dispLine(); 99 prompt(); 100} // end help() 101 102 103void prompt() { // prompt, ready for commands 104 sprintf(lineString,"%d>",netMyPx); 105 dispLine(); 106} // end prompt() 107 108 109void dispLine() { // print information 110 s_concat(lineString, _CR, sizeof(lineString)); 111 txBackToSender(); // tx 112 s_clear(lineString, sizeof(lineString)); 113 delay(100); 114} // end dispLine() 115 116 117void receiveEvent(int howMany) { // i2c event incoming requests 118 if (isBusy==false) { 119 int counter=0; 120 s_clear(recCommand, sizeof(recCommand)); // sender command 121 recAddress=0; // sender address 122 recType=0; // sender cmd type 123 if (howMany == 0) { // ignores empty requests 124 isNetDataWaiting=false; 125 } else { 126 while (Wire.available()) { 127 data[0]=Wire.read(); 128 if (counter==0) { recType = data[0]; } // 1st char = cmd type 129 if (counter==1) { recAddress = data[0]; } // 2nd char = sender address 130 if (counter >1) { // other chars = command 131 s_concat(recCommand, data, sizeof(recCommand)); 132 } 133 counter++; 134 } 135 isNetDataWaiting=true; 136 } 137 }//endisBusy 138} // end receiveEvent() 139 140 141void netWhoIsEvent() { // i2c event: 11 bytes = 1=char(5) ENQ, 2-9=netMyNAME 10=netMyPx 11=netMyAddress 142 if (isBusy==false) { 143 char ccommand[65]=""; 144 char park[3]=""; 145 s_concat(ccommand, _ENQ, sizeof(ccommand)); // ENQ 146 s_concat(ccommand, netMyNAME, sizeof(ccommand)); // netMyNAME 147 s_concat(ccommand, itoa(netMyPx,park,10), sizeof(ccommand)); // netMyPx 148 s_concat(ccommand, _MYADDR, sizeof(ccommand)); // netMyAddress 149 Wire.write(ccommand); 150 } 151} // end netWhoIsEvent() 152 153 154void txBackToSender() { // tx back to sender 155 char ccommand[65]=""; 156 s_assign(ccommand, _ACK, sizeof(ccommand)); // ACK=answer data 157 s_concat(ccommand, _MYADDR, sizeof(ccommand)); // netMyAddress 158 s_concat(ccommand, lineString, sizeof(ccommand)); // data 159 Wire.beginTransmission (recAddress); 160 Wire.write (ccommand); 161 Wire.endTransmission (true); 162} // end txBackToSender() 163 164 165//------------------------------------------------------------------------- CLEAR 166void s_clear(char *dest_source_string, const int dest_sizeof) { // fills-up with NUL=chr(0) 167 memset(dest_source_string, 0, dest_sizeof); 168} // end s_clear() 169 170//------------------------------------------------------------------------- ASSIGN 171bool s_assign(char *dest_string, const char *source_string, const int dest_sizeof) { // copies source to dest 172 int _LenS=strlen(source_string); // how many bytes 173 if (_LenS > (dest_sizeof - 1)) { 174 return true; // err 1 175 } else { 176 strcpy(dest_string, source_string); 177 return false; // ok 0 178 } 179} // end s_assign() 180 181//------------------------------------------------------------------------- SUBSTRING 182bool s_substring(char *dest_string, const int dest_sizeof, const char *source_string, 183 const int source_sizeof, const int source_from, const int source_to) { // copies source(from, to) to dest 184 if ((source_from < 0) || (source_to < source_from) || ((source_to - source_from + 1) > (dest_sizeof - 1)) 185 || (source_to >= (source_sizeof-1)) || ((source_to - source_from + 1) > (strlen(source_string)))) { 186 dest_string[0]=0; // NUL 187 return true; // err 1 188 } else { 189 int _Count=0; 190 for (int i=source_from;i<(source_to+1);i++) { 191 dest_string[_Count]=source_string[i]; 192 _Count++; 193 } 194 dest_string[_Count]=0; // ends with NUL 195 return false; // ok 0 196 } 197} // end s_substring() 198 199//------------------------------------------------------------------------- CONCAT 200bool s_concat(char *dest_string, const char *source_string, const int dest_sizeof) { // append source to dest 201 int _LenS=strlen(source_string); // how many bytes source 202 int _LenD=strlen(dest_string); // how many bytes dest 203 if ((_LenS + _LenD) > (dest_sizeof - 1)) { 204 return true; // err 1 205 } else { 206 strcat(dest_string, source_string); 207 return false; // ok 0 208 } 209} // end s_concat() 210 211//------------------------------------------------------------------------- COMPARE 212bool s_compare(const char *dest_string, const char *source_string) { // compares source with dest 213 int _LenS=strlen(source_string); // how many bytes source 214 int _LenD=strlen(dest_string); // how many bytes dest 215 if (_LenS != _LenD) { // different length 216 return true; // are different 1 217 } else { 218 if (strcmp(dest_string, source_string) == 0) { 219 return false; // are the same 0 220 } else { 221 return true; // are different 1 222 } 223 } 224} // end s_compare() 225 226 227//------------------------------------------------------------------------- FREEMEM 228int s_freemem() { 229 extern int __heap_start,*__brkval; 230 int v; 231 return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); 232} // end s_freemem() 233
Downloadable files
Fritzing schematic diagram
Fritzing schematic diagram
Comments
Only logged in users can leave comments
marcozonca
9 Followers
•11 Projects
0
1
marcozonca
a year ago
Please check updates of this and other my projects on https://www.hackster.io/marcozonca and keep in contact with me too with your comments and questions there. Thank you.