Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- /* This program is free software: you can redistribute it and/or modify*/
- /* it under the terms of the GNU General Public License as published by*/
- /* the Free Software Foundation, either version 3 of the License, or*/
- /* (at your option) any later version.*/
- /* This program is distributed in the hope that it will be useful,*/
- /* but WITHOUT ANY WARRANTY; without even the implied warranty of*/
- /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the*/
- /* GNU General Public License for more details.*/
- /* You should have received a copy of the GNU General Public License*/
- /* along with this program. If not, see <http://www.gnu.org/licenses/>.*/
- #include <SoftwareSerial.h>
- #include <LiquidCrystal.h>
- #define LED 13
- SoftwareSerial rfidSerial(2, 3);
- LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
- unsigned char inputBuffer[255];
- unsigned char dataBuffer[255];
- unsigned char outputBuffer[255];
- int lenOutput = 0;
- unsigned char pingQuery[3] = {0xAB, 0x00, 0x00};
- unsigned char pingResponse[3] = {0xAB, 0x01, 0x00};
- unsigned char ackResponse[3] = {0xAB, 0x07, 0x00};
- unsigned char negResponse[3] = {0xAB, 0x08, 0x00};
- int lcdLine = 0;
- void setup()
- {
- Serial.begin(9600); // Port USB
- rfidSerial.begin(9600); // Serie vers carte RFID
- pinMode(13, OUTPUT);
- lcd.begin(16, 2);
- lcd.noDisplay();
- }
- int usbReadFrame(unsigned char* str) {
- int i;
- int dataLen;
- // delay(100); // Wait for all data (http://stackoverflow.com/questions/6048270)
- Serial.readBytes( (char*) str, 3);
- dataLen = (int) str[2];
- Serial.readBytes( (char*) str + 3, dataLen);
- return 3 + dataLen;
- Serial.flush();
- }
- int usbSendFrame(int len, unsigned char* str){
- int i = 0;
- for(i = 0; i < len; i++){
- Serial.write(str[i]);
- }
- return i;
- Serial.flush();
- }
- void ledBlink(int n){
- digitalWrite(LED, HIGH);
- delay(300);
- digitalWrite(LED, LOW);
- delay(300);
- for(int i = 0; i < n; i++){
- digitalWrite(LED, HIGH);
- delay(100);
- digitalWrite(LED, LOW);
- delay(100);
- }
- }
- void loop()
- {
- int ret = 0;
- int i = 0;
- if(Serial.available()){
- ret = usbReadFrame(inputBuffer);
- if(ret != -1){
- int dataLen = (int) inputBuffer[2];
- if(inputBuffer[1] == 0x00) {
- // Ping Request
- usbSendFrame(3, pingResponse);
- //ledBlink(1);
- }
- else if(inputBuffer[1] == 0x02) {
- int lenOutput = 0;
- for(int i = 0; i < dataLen; i++) {
- rfidSerial.write(inputBuffer[i + 3]);
- }
- } else if(inputBuffer[1] == 0x04) {
- // LCD Request
- if( dataLen == 1 && inputBuffer[3] == 0xAB){
- // Shutdown screen command
- lcd.clear();
- lcd.noDisplay();
- } else {
- lcd.display();
- inputBuffer[3 + (int) inputBuffer[2]] = '\0';
- lcd.setCursor(0, lcdLine);
- lcd.print( (char*) (inputBuffer + 3));
- //ledBlink(3);
- if(lcdLine == 0)
- lcdLine = 1;
- else
- lcdLine = 0;
- }
- }
- }
- }
- // RFID Read
- if (rfidSerial.available()) {
- outputBuffer[lenOutput + 3] = rfidSerial.read();
- lenOutput++;
- }else if(rfidSerial.available() == 0 && lenOutput != 0){
- // Sending RFID Response through USB
- outputBuffer[0] = 0xAB;
- outputBuffer[1] = 0x03;
- outputBuffer[2] = (unsigned char) lenOutput;
- for(int i = 0; i < lenOutput + 3; i++){
- Serial.write(outputBuffer[i]);
- }
- lenOutput = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement