Advertisement
Guest User

Arduino - RFID - LCD

a guest
Feb 20th, 2013
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1. /*    
  2. /*    This program is free software: you can redistribute it and/or modify*/
  3. /*    it under the terms of the GNU General Public License as published by*/
  4. /*    the Free Software Foundation, either version 3 of the License, or*/
  5. /*    (at your option) any later version.*/
  6.  
  7. /*    This program is distributed in the hope that it will be useful,*/
  8. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
  9. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
  10. /*    GNU General Public License for more details.*/
  11.  
  12. /*    You should have received a copy of the GNU General Public License*/
  13. /*    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
  14.    
  15. #include <SoftwareSerial.h>
  16. #include <LiquidCrystal.h>
  17.  
  18. #define LED 13
  19.  
  20. SoftwareSerial rfidSerial(2, 3);
  21. LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
  22.  
  23. unsigned char inputBuffer[255];
  24. unsigned char dataBuffer[255];
  25. unsigned char outputBuffer[255];
  26. int lenOutput = 0;
  27.  
  28. unsigned char pingQuery[3] = {0xAB, 0x00, 0x00};
  29. unsigned char pingResponse[3] = {0xAB, 0x01, 0x00};
  30. unsigned char ackResponse[3] = {0xAB, 0x07, 0x00};
  31. unsigned char negResponse[3] = {0xAB, 0x08, 0x00};
  32.  
  33. int lcdLine = 0;
  34.  
  35. void setup()
  36. {
  37.   Serial.begin(9600); // Port USB
  38.   rfidSerial.begin(9600); // Serie vers carte RFID
  39.  
  40.   pinMode(13, OUTPUT);
  41.  
  42.   lcd.begin(16, 2);
  43.   lcd.noDisplay();
  44. }
  45.  
  46. int usbReadFrame(unsigned char* str) {
  47.   int i;  
  48.   int dataLen;
  49.  
  50.   // delay(100); // Wait for all data (http://stackoverflow.com/questions/6048270)
  51.   Serial.readBytes( (char*) str, 3);
  52.  
  53.   dataLen = (int) str[2];
  54.   Serial.readBytes( (char*) str + 3, dataLen);
  55.  
  56.   return 3 + dataLen;
  57.   Serial.flush();
  58. }
  59.  
  60. int usbSendFrame(int len, unsigned char* str){
  61.   int i = 0;
  62.   for(i = 0; i < len; i++){
  63.     Serial.write(str[i]);
  64.   }
  65.   return i;
  66.   Serial.flush();
  67. }
  68.  
  69. void ledBlink(int n){
  70.   digitalWrite(LED, HIGH);
  71.   delay(300);
  72.   digitalWrite(LED, LOW);
  73.   delay(300);
  74.   for(int i = 0; i < n; i++){
  75.     digitalWrite(LED, HIGH);
  76.     delay(100);
  77.     digitalWrite(LED, LOW);
  78.     delay(100);
  79.   }
  80. }
  81.  
  82. void loop()
  83. {
  84.   int ret = 0;
  85.   int i = 0;
  86.  
  87.   if(Serial.available()){  
  88.  
  89.     ret = usbReadFrame(inputBuffer);
  90.     if(ret != -1){
  91.  
  92.       int dataLen = (int) inputBuffer[2];
  93.  
  94.       if(inputBuffer[1] == 0x00) {
  95.         // Ping Request
  96.         usbSendFrame(3, pingResponse);
  97.         //ledBlink(1);
  98.       }
  99.       else if(inputBuffer[1] == 0x02) {
  100.         int lenOutput = 0;
  101.        
  102.         for(int i = 0; i < dataLen; i++) {
  103.           rfidSerial.write(inputBuffer[i + 3]);
  104.         }
  105.        
  106.       } else if(inputBuffer[1] == 0x04) {
  107.         // LCD Request
  108.         if( dataLen == 1 && inputBuffer[3] == 0xAB){
  109.           // Shutdown screen command
  110.           lcd.clear();
  111.           lcd.noDisplay();
  112.         } else {
  113.           lcd.display();
  114.           inputBuffer[3 + (int) inputBuffer[2]] = '\0';
  115.           lcd.setCursor(0, lcdLine);
  116.           lcd.print( (char*) (inputBuffer + 3));
  117.  
  118.           //ledBlink(3);
  119.           if(lcdLine == 0)
  120.             lcdLine = 1;
  121.           else
  122.             lcdLine = 0;
  123.         }
  124.     }
  125.   }
  126. }
  127.  
  128.   // RFID Read
  129.   if (rfidSerial.available()) {
  130.     outputBuffer[lenOutput + 3] = rfidSerial.read();
  131.     lenOutput++;
  132.   }else if(rfidSerial.available() == 0 && lenOutput != 0){
  133.       // Sending RFID Response through USB
  134.       outputBuffer[0] = 0xAB;
  135.       outputBuffer[1] = 0x03;
  136.       outputBuffer[2] = (unsigned char) lenOutput;
  137.  
  138.     for(int i = 0; i < lenOutput + 3; i++){
  139.         Serial.write(outputBuffer[i]);
  140.     }
  141.     lenOutput = 0;
  142.   }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement