Components and supplies
Arduino UNO
Arduino Mega 2560
Project description
Code
SKETCH FOR ARDUINO UNO + USB SHIELD
arduino
1#include <hidboot.h> 2#include <usbhub.h> 3 4String MyString = ""; 5int 6 LenString = 0; 7 8// Satisfy the IDE, which needs to see the include statment 9 in the ino too. 10#ifdef dobogusinclude 11#include <spi4teensy3.h> 12#include 13 <SPI.h> 14#endif 15 16class KbdRptParser : public KeyboardReportParser 17{ 18 19 void PrintKey(uint8_t mod, uint8_t key); 20 21 protected: 22 void OnControlKeysChanged(uint8_t 23 before, uint8_t after); 24 25 void OnKeyDown (uint8_t mod, uint8_t key); 26 27 void OnKeyUp (uint8_t mod, uint8_t key); 28 void OnKeyPressed(uint8_t key); 29}; 30 31 32void 33 KbdRptParser::PrintKey(uint8_t m, uint8_t key) 34{ 35 MODIFIERKEYS mod; 36 *((uint8_t*)&mod) 37 = m; 38}; 39 40void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) 41{ 42 43 44 PrintKey(mod, key); 45 uint8_t c = OemToAscii(mod, key); 46 47 if (c) 48 49 OnKeyPressed(c); 50 51} 52 53void KbdRptParser::OnControlKeysChanged(uint8_t 54 before, uint8_t after) { 55 56} 57 58void KbdRptParser::OnKeyUp(uint8_t mod, 59 uint8_t key) 60{ 61 62} 63 64 65void KbdRptParser::OnKeyPressed(uint8_t key) 66{ 67 68 char inChar = (char)key; 69 70 if (key == 19) { 71 MyString += inChar; 72 73 Serial.println(MyString); 74 MyString = ""; 75 } 76 else MyString 77 += inChar; 78 79}; 80 81USB Usb; 82HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb); 83 84uint32_t 85 next_time; 86 87KbdRptParser Prs; 88 89void setup() 90{ 91 Serial.begin(115200); 92#if 93 !defined(__MIPSEL__) 94 while (!Serial); // Wait for serial port to connect - 95 used on Leonardo, Teensy and other boards with built-in USB CDC serial connection 96#endif 97 98 99 if (Usb.Init() == -1) 100 101 delay( 200 ); 102 103 next_time = millis() + 5000; 104 105 106 HidKeyboard.SetReportParser(0, (HIDReportParser*)&Prs); 107 108} 109 110void loop() 111{ 112 113 Usb.Task(); 114 115} 116 117
SKETCH FOR ARDUINO MEGA + ETHERNET SHIELD
arduino
1/* 2Progetto Elimina Code SDCard e Ethernet 3 4Questo modulo deve ricevere gli elementi della coda dalla seriale e scrivere nella SDCard e mostrarli nella pagina web. 5 6 */ 7 8#include <SPI.h> 9#include <SD.h> 10#include <Ethernet.h> 11#include <EEPROM.h> 12#define maxLength 25 13 14// Enter a MAC address and IP address for your controller below. 15// The IP address will be dependent on your local network: 16byte mac[] = { 17 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 18IPAddress ip(192, 168, 1, 111); 19 20// Initialize the Ethernet server library 21// with the IP address and port you want to use 22// (port 80 is default for HTTP): 23EthernetServer server(80); 24 25String inputString = ""; // conterr i caratteri provenienti dalla seriale 26char* FileName[] = {"CodaUno.txt", "CodaDue.txt"}; // verr sostituito da CodaDue.txt a secondo se il file txt da usare 27boolean stringComplete = false, CancellaElemento = false; 28int CS_pin = 4; 29int inCharFile; 30String inputStringWEB = String(maxLength); // conterr i caratteri provenienti dal client 31int NumeroFile; // contiene il numero del file nella non volatile memory (se CodaUno o CodaDue) 32int IndirizzoMemoria = 0; 33unsigned long TempoAttesa = millis(), TempoAttesaMedio = 1; // = 1 aggiungo un minuto per rendere piu' ragionevole il tempo di attesa 34 35File myFile1, myFile2; // files di testo contenenti gli elementi della coda 36File htmlFile; // ancora non usato "da cancellare" 37 38void setup() { 39 40 pinMode(CS_pin, OUTPUT); 41 Serial.begin(115200); 42 Serial1.begin(115200); 43 44 inputString.reserve(200); 45 46 // start the Ethernet connection and the server: 47 Ethernet.begin(mac, ip); 48 server.begin(); 49 //Serial.print("server is at "); 50 //Serial.println(Ethernet.localIP()); 51 52 if (!SD.begin(CS_pin)) { 53 //Serial.println("Avvio SD Card fallito!"); 54 return; 55 } 56 57 //Serial.println("SD Card avviata."); 58 59 NumeroFile = int(EEPROM.read(IndirizzoMemoria)); // legge dalla memoria il file attivo se 0 o 1 60 if (NumeroFile > 1) { 61 NumeroFile = 0; 62 } 63 64} 65 66void loop() { 67 68 if ((Serial1.available() != 0) and (stringComplete == 0)) { 69 serialEvent(); 70 } 71 72 if ((stringComplete) and (inputString.length()) >= 8) { // controllo stringhe provenienti dalla seriale scrivo i valori di corrente dei rispettivi canali. 73 74 // Scrittura del file 75 76 //Serial.println (inputString); 77 myFile1 = SD.open(FileName[NumeroFile], FILE_WRITE); 78 79 if (myFile1) { 80 //Serial.println("Mi preparo a scrivere il file .txt ..."); 81 myFile1.println(inputString + "<br>"); 82 myFile1.close(); 83 //Serial.println("scrittura eseguita correttamente."); 84 } 85 else { 86 //Serial.println("Non stato possibile scrivere il file .txt!"); 87 } 88 89 //Serial.println(inputString); 90 inputString = ""; 91 stringComplete = false; 92 } 93 94 if (CancellaElemento) { // CancellaElemento se vero viene cancellato il primo elemento della coda 95 96 /* Cancella primo elemento dal file 97 Visto che non ho trovato una funzione capace di cancellare un elemento preciso del file ed inoltre non trovo funzioni per rinominare nomi dei file, allora 98 creo un blocco che trasferisce la coda senza l'elemento da cancellare in una nuova coda tipo CodaDue.txt, poi viene cancellato il vecchio file CodaUno.txt 99 e da questo momento la scheda lavora su CodaDue.txt, verranno invertiti i nomi dei files alla successiva richiesta di eliminazione primo elemento.*/ 100 101 //Serial.println(NumeroFile); 102 103 if (NumeroFile == 0) { 104 myFile1 = SD.open(FileName[0], FILE_READ); 105 myFile2 = SD.open(FileName[1], FILE_WRITE); 106 if (myFile1) { 107 char inCharFile = (char)myFile1.read(); 108 while (inCharFile != '>') { // legge tutti i caratteri del primo elemento della coda non fa nulla, il primo elemento si deve cancellare 109 inCharFile = (char)myFile1.read(); 110 } 111 while (myFile1.available()) { //--- continua la lettura del file scrivendo i restanti elementi della coda nel nuovo file 112 myFile2.write(myFile1.read()); // myFile2.println(myFile1.read()); 113 } 114 myFile1.close(); 115 myFile2.close(); 116 SD.remove(FileName[0]); 117 NumeroFile = 1; 118 EEPROM.write(IndirizzoMemoria, NumeroFile); 119 } 120 else { 121 //Serial.println("Non stato possibile aprire il file .txt!"); 122 } 123 } 124 else { 125 myFile1 = SD.open(FileName[1], FILE_READ); 126 myFile2 = SD.open(FileName[0], FILE_WRITE); 127 if (myFile1) { 128 char inCharFile = (char)myFile1.read(); 129 while (inCharFile != '>') { // legge tutti i caratteri del primo elemento della coda non fa nulla, il primo elemento si deve cancellare 130 inCharFile = (char)myFile1.read(); 131 } 132 while (myFile1.available()) { //--- continua la lettura del file scrivendo i restanti elementi della coda nel nuovo file 133 myFile2.write(myFile1.read()); 134 } 135 myFile1.close(); 136 myFile2.close(); 137 SD.remove(FileName[1]); 138 NumeroFile = 0; 139 EEPROM.write(IndirizzoMemoria, NumeroFile); 140 } 141 else { 142 //Serial.println("Non stato possibile aprire il file .txt!"); 143 } 144 } 145 CancellaElemento = false; 146 } 147 148 inputStringWEB = ""; 149 // listen for incoming clients 150 EthernetClient client = server.available(); 151 if (client) { 152 //Serial.println("new client"); 153 // an http request ends with a blank line 154 boolean currentLineIsBlank = true; 155 while (client.connected()) { 156 if (client.available()) { 157 char c = client.read(); 158 // Serial.write(c); //stampa in carattere proveniente dal client 159 if (inputStringWEB.length() < maxLength) { 160 //inString.append(c); 161 inputStringWEB += c; 162 } 163 // if you've gotten to the end of the line (received a newline 164 // character) and the line is blank, the http request has ended, 165 // so you can send a reply 166 if (c == '\n' && currentLineIsBlank) { 167 // send a standard http response header 168 client.println("HTTP/1.1 200 OK"); 169 client.println("Content-Type: text/html"); 170 client.println("Connection: close"); // the connection will be closed after completion of the response 171 client.println("Refresh: 5"); // refresh the page automatically every 5 sec. 172 client.println(); 173 client.println("<!DOCTYPE HTML>"); 174 client.println("<html><head><style>table, th, td { border: 2px solid black; }</style></head><body><center>"); 175 176 client.println("<img src=\\"http://1.bp.blogspot.com/-Vk2Ril41e-w/UfaZiB2YweI/AAAAAAAABAk/1qHqsUmd_w4/s1600/intestazione%2Blunga.jpg\\" style=\\"width:550px;height:250px;\\">"); 177 178 //client.println("<h1 style=\\"font-size:500%;\\">Mio Turno</h1>"); 179 client.println("<table>"); 180 client.println("<tr>"); 181 client.println("<h1 style=\\"font-size:600%;\\">"); 182 client.print("Serviamo: "); 183 //--- questo blocco legge la prima stringa dal file e la stampa in rosso 184 myFile1 = SD.open(FileName[NumeroFile], FILE_READ); 185 if (myFile1) { 186 client.print("<font color=red>"); 187 char inCharFile = (char)myFile1.read(); 188 while (inCharFile != '>') { 189 client.write(inCharFile); // 190 inCharFile = (char)myFile1.read(); 191 } 192 client.write(inCharFile); // stampa il carattere controllato '>' 193 194 client.println("</font>"); //----------------------------------------------------------------------- 195 196 if (inputStringWEB.indexOf("?") > -1) { // se vero ho ricevuto un comando get o post 197 client.println("<script>{window.open(\\"http://192.168.1.111\\", \\"_self\\");}</script>"); // carico nuovamente la pagina per cancellare inputStringWEB 198 //Serial.println("cancello il primo elemento della lista"); 199 CancellaElemento = true; 200 TempoAttesa = millis() - (TempoAttesaMedio / 2); 201 } 202 else if (myFile1.available()!=2) { 203 client.print("<form method=get>"); 204 client.println("<br><input name=\\"S\\" type=\\"submit\\" value=\\"Prossimo: \\">"); 205 client.println("</form>"); 206 } 207 208 //--- continua la lettura del file stampando i restanti elementi della coda in nero --- 209 while (myFile1.available()) { 210 if (myFile1.available() == 2) { // in pratica non serve, volevo eliminare l'ultimo carattere della pagina web 211 break; 212 } 213 else { 214 client.write(myFile1.read()); 215 } 216 } 217 myFile1.close(); 218 } 219 220 client.println("</h1>"); 221 client.println("</tr>"); 222 client.println("</table>"); 223 //--- ----------------------------------------------------------------------------- --- 224 client.println("<h1 style=\\"font-size:200%;\\">"); 225 client.println("<svg height=\\"50\\" width=\\"800\\"><line x1=\\"0\\" y1=\\"0\\" x2=\\"1000\\" y2=\\"0\\" style=\\"stroke:rgb(255,0,0);stroke-width:12\\" />"); 226 client.println("--------------------------------------------------------------------------------------</svg><br>"); 227 // calcolo il tempo d'attesa 228 client.println("Tempo di attesa medio: "); 229 TempoAttesaMedio = ((millis() - TempoAttesa) + TempoAttesaMedio) / 2; 230 client.println(((TempoAttesaMedio/999)/60) + 1); // +1 inserisco un offset di un minuto 231 client.println(" min<br>"); 232 client.println("</h1>"); 233 client.println("</center></body></html>"); 234 break; 235 } 236 if (c == '\n') { 237 // you're starting a new line 238 currentLineIsBlank = true; 239 } 240 else if (c != '\r') { 241 // you've gotten a character on the current line 242 currentLineIsBlank = false; 243 } 244 } 245 } 246 // give the web browser time to receive the data 247 delay(1); 248 // close the connection: 249 client.stop(); 250 //Serial.println("client disconnected"); 251 } 252 253 delay(1); 254} 255 256void serialEvent() { 257 while (Serial1.available()) { 258 259 char inChar = (char)Serial1.read(); 260 261 if (('0' >= inChar <= '9') or ('A' >= inChar <= 'Z') or ('a' >= inChar <= 'z')) { // questo filtro fa in modo che inputString contenga solo caratteri da '0..9', 'A..Z' e 'a..z' 262 inputString += inChar; 263 } 264 delay(1); 265 if (Serial1.available() == 0) { 266 stringComplete = true; 267 } 268 } 269} 270
SKETCH FOR ARDUINO UNO + USB SHIELD
arduino
1#include <hidboot.h> 2#include <usbhub.h> 3 4String MyString = ""; 5int LenString = 0; 6 7// Satisfy the IDE, which needs to see the include statment in the ino too. 8#ifdef dobogusinclude 9#include <spi4teensy3.h> 10#include <SPI.h> 11#endif 12 13class KbdRptParser : public KeyboardReportParser 14{ 15 void PrintKey(uint8_t mod, uint8_t key); 16 17 protected: 18 void OnControlKeysChanged(uint8_t before, uint8_t after); 19 20 void OnKeyDown (uint8_t mod, uint8_t key); 21 void OnKeyUp (uint8_t mod, uint8_t key); 22 void OnKeyPressed(uint8_t key); 23}; 24 25 26void KbdRptParser::PrintKey(uint8_t m, uint8_t key) 27{ 28 MODIFIERKEYS mod; 29 *((uint8_t*)&mod) = m; 30}; 31 32void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) 33{ 34 35 PrintKey(mod, key); 36 uint8_t c = OemToAscii(mod, key); 37 38 if (c) 39 OnKeyPressed(c); 40 41} 42 43void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) { 44 45} 46 47void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) 48{ 49 50} 51 52 53void KbdRptParser::OnKeyPressed(uint8_t key) 54{ 55 char inChar = (char)key; 56 57 if (key == 19) { 58 MyString += inChar; 59 Serial.println(MyString); 60 MyString = ""; 61 } 62 else MyString += inChar; 63 64}; 65 66USB Usb; 67HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb); 68 69uint32_t next_time; 70 71KbdRptParser Prs; 72 73void setup() 74{ 75 Serial.begin(115200); 76#if !defined(__MIPSEL__) 77 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection 78#endif 79 80 if (Usb.Init() == -1) 81 82 delay( 200 ); 83 84 next_time = millis() + 5000; 85 86 HidKeyboard.SetReportParser(0, (HIDReportParser*)&Prs); 87 88} 89 90void loop() 91{ 92 Usb.Task(); 93 94} 95 96
Downloadable files
sketchNO CODE NO PAPER
sketchNO CODE NO PAPER
sketchNO CODE NO PAPER
sketchNO CODE NO PAPER
Comments
Only logged in users can leave comments
camillo12373
0 Followers
•0 Projects
Table of contents
Intro
1
0