Commit 90e3c8f6 authored by Antoine Lima's avatar Antoine Lima

Merge upstream

parents 1dfd2b54 cef35859
......@@ -10,13 +10,18 @@
#define RST_PIN 9
#define SS_PIN 10
static const byte buttonPins[] = {A0, A1, A2, A3, A4, A5};
static const byte IRPin = 2;
#define DELAY_BETWEEN_RFID_MS 6000
static const byte buttonPins[] = {A0, A1, A2, A3, A4};
static const byte goalPin = 2;
static const byte RFID_RST_PIN = 9;
static const byte RFID_SS_PIN = 10;
volatile static byte currentStates;
volatile static byte previousStates;
volatile static byte triggered;
volatile static uint32_t lastRFID;
static uint32_t currentRFID;
static uint32_t previousRFID;
static uint32_t lastRFIDTime;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
Com com;
......@@ -49,14 +54,14 @@ void updateButtons(void) {
previousStates = currentStates;
}
void checkIR(void) {
triggered++;
// Temporarly either 0 or 1
void checkGoal(void) {
// Later it will measure time difference to check if a ball got in
// the way of one or another goal and send that goal
com.sendGoal(triggered%2);
if(digitalRead(goalPin)==1)
{
com.sendGoal();
}
}
void pciSetup(byte pin)
......@@ -68,32 +73,37 @@ void pciSetup(byte pin)
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
for(byte i=0; i<sizeof(buttonPins); i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pciSetup(buttonPins[i]);
}
pinMode(IRPin, INPUT_PULLUP);
attachInterrupt(0, checkIR, CHANGE);
pinMode(goalPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(goalPin), checkGoal, CHANGE);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}
void loop() {
// Look for new cards, and select one if present
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
lastRFID = 0;
currentRFID = 0;
for (byte i = 0; i < mfrc522.uid.size; i++) {
lastRFID |= ((uint32_t)mfrc522.uid.uidByte[i]) << 8*(mfrc522.uid.size-i-1);
currentRFID |= ((uint32_t)mfrc522.uid.uidByte[i]) << 8*(mfrc522.uid.size-i-1);
}
if((currentRFID!=previousRFID) || ((millis()-lastRFIDTime)>DELAY_BETWEEN_RFID_MS)) {
com.sendRFID(currentRFID);
previousRFID = currentRFID;
lastRFIDTime = millis();
}
com.sendRFID(lastRFID);
}
if(com.emitMessage()) {
delay(20);
} else {
delay(1000);
delay(100);
}
}
......@@ -2,40 +2,40 @@
#include <Arduino.h>
bool Com::addMessage(Message::Type type, uint32_t content) {
Message msg = {.type = type, .content=content};
return _buffer.put(msg);
Message msg = {.type = type, .content = content};
return _buffer.put(msg);
}
bool Com::sendRFID(uint32_t rfid) {
return addMessage(Message::Type::RFID, rfid);
return addMessage(Message::Type::RFID, rfid);
}
bool Com::sendButton(uint8_t buttonID) {
return addMessage(Message::Type::Butn, buttonID);
return addMessage(Message::Type::Butn, buttonID);
}
bool Com::sendGoal(uint8_t side) {
return addMessage(Message::Type::Goal, side);
bool Com::sendGoal() {
return addMessage(Message::Type::Goal);
}
bool Com::emitMessage() {
Message msgToSend = {.type = Message::Type::None, .content=0};
// Warning &?
if(_buffer.get(msgToSend)) {
switch(msgToSend.type)
{
case Message::Type::RDID: Serial.print("rfid"); break;
case Message::Type::Butn: Serial.print("butn"); break;
case Message::Type::Goal: Serial.print("goal"); break;
}
Serial.print(":");
Serial.println(msgToSend.content, HEX);
return true;
}
return false;
Message msgToSend = {.type = Message::Type::None, .content = 0};
// Warning &?
if (_buffer.get(msgToSend)) {
switch(msgToSend.type)
{
case Message::Type::RFID: Serial.print("rfid"); break;
case Message::Type::Butn: Serial.print("butn"); break;
case Message::Type::Goal: Serial.print("goal"); break;
}
Serial.print(":");
Serial.println(msgToSend.content, HEX);
return true;
}
return false;
}
......@@ -19,12 +19,12 @@ class Com
} Message;
private:
CircularBuffer<Message> _buffer;
bool addMessage(Message::Type type, uint32_t content);
bool addMessage(Message::Type type, uint32_t content = 0);
public:
bool sendRFID(uint32_t rfid);
bool sendButton(uint8_t buttonID);
bool sendGoal(uint8_t side);
bool sendGoal();
bool emitMessage();
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment