ArcheryClock-Arduino/ArcheryClockMEGA02_1.ino

281 lines
9.4 KiB
C++

//Copyright (C) 2010-2014 Henk Jegers
//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/>.
//for questions email to info@archeryclock.com
// version MEGA01 Initial version
// version MEGA01_1 Adding robustness for receiving ABCD detailes which is implemented in AC 232. Using the ABCD details information is not yet implemented in MEGA01_1 Needs archeryclock version 2320 or newer.
// version MEGA01_2 Added end number information. Needs Archeryclock2401 or newer. (older archeryclock versions don't sent the end number information to arduine)
// version MEGA02_1 Changes because of a change in protocoll for AC 241 (nr of archers and E and F shooters) and added functionality: emergency stop, add archers E and F. Changes for signal robustness. (0 (0000) is sent as 12 (1100) to prevent decoding issues when 3 times 0 is sent (000000000000)
String inputString;
int serialvalue;
int statevalue; //received data for state details.
int trafficvalue; //received data for traffic lights, and ABCD lights
int leftcountvalue; //received data for left countdown.
int rightcountvalue; //received data for rightcountdown.
int abcdvalue; //received data for abcd details
int endnrvalue; //received data for end nr
int buttonvalue;
int remember1;
int loop1;
int blinkk;
int blinkkk;
int blinkl;
int blinkr;
int rightdigit; //value of left digit (0..10 (10 means dashes))
int middigit; //value of middle digit (0..10 (10 means dashes))
int leftdigit; //value of light digit (0..10 (10 means dashes))
int lefend; //value of left digit to indicate end nr (0..9 or 15 (15 means P to indicate practise end))
int midend; //value of middle digit to indicate end nr (0..9)
int rigend; //value of middle digit to indicate end nr (0..9)
int comend; //value of combined left and middle digit. In case of practise end the P from the left digit should be projected. else the middle digit (0..9 or 15 (15 means P to indicate practise end))
#define CHAR_P 15
#define CHAR_DASH 10
const uint8_t segment[16] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x40, 0x00, 0x3f, 0x00, 0x00, 0x73}; //0,1,2,3,4,5,6,7,8,9,-, , , , ,P
enum PanelSide_t
{
LEFT = 0,
RIGHT = 1
};
enum BuzzerStatus_t
{
BUZZER_OFF,
BUZZER_ON
};
struct cfg_t
{
PanelSide_t PanelSide : 1;
BuzzerStatus_t BuzzerStatus : 1;
uint8_t abcd : 4; // unclear meaning
uint8_t competition : 3; // unclear meaning
uint8_t GroupsNumber : 6;
uint8_t : 0;
} cfg;
struct trafficvalue_t
{
uint8_t cmd : 3;
BuzzerStatus_t buzzer : 1;
uint8_t TrafficLightLeft : 3;
uint8_t TrafficLightRight : 3;
uint8_t ArcherGroups : 6;
//uint8_t : 0;
};
struct statevalue_t
{
uint8_t cmd : 4;
uint8_t abcd : 4; // unclear meaning
uint8_t competition : 3; // unclear meaning
uint8_t : 3;
uint8_t GroupsNumber : 6;
//uint8_t : 0;
};
struct digitsvalue_t
{
uint8_t cmd : 1;
PanelSide_t side : 1;
uint8_t sideOverride : 1;
uint8_t minutes : 1;
uint8_t rDigit : 4;
uint8_t mDigit : 4;
uint8_t lDigit : 4;
};
union ReceivedCommand_t {
int raw;
trafficvalue_t t;
statevalue_t s;
digitsvalue_t d;
} ReceivedCommand;
void setup()
{
// All following port banks are outputs for transistors array
DDRF = 0xFF; // Analog 0~7
DDRK = 0xFF; // Analog 8~15
DDRL = 0xFF;
DDRA = 0xFF;
DDRC = 0xFF;
// Load system configuration
cfg.BuzzerStatus = digitalRead(A4) ? BUZZER_ON : BUZZER_OFF;
cfg.PanelSide = digitalRead(A5) ? RIGHT : LEFT;
remember1 = 0;
loop1 = 0;
blinkk = 0;
blinkkk = 0;
blinkl = 0;
blinkr = 0;
rightcountvalue = 0xAAA0;
leftcountvalue = 0xAAA0;
serialvalue = 0x0000;
Serial.begin(9600);
Serial.println("Arduino Serial Link");
Serial.println("connected!");
}
void loop()
{
if (Serial.available())
{
/* ** Buffer to store received char **
* Data is an integer string terminated with newline '\n'
* */
static String inputString = "";
char digit = Serial.read(); // Read serial buffer
// Check if string terminator
if (digit == '\n')
{
// Make string conversion
ReceivedCommand.raw = inputString.toInt();
inputString = "";
// Decode command
//16 bits data received.
//first bit is telling if the data is about traffic lights and ABCD lights (true). or about the digits of the countdown (false).
//second bit is telling:
// -in case of digits (first bit is 0)if the digits are from the right side countdown (true) or left side countdown (false).
// -in case of trafic/ABCD lights: not implemented jet. Proposal is to use this for ABCD light depending on sequence (For example CDAB is also possible to show)
// Traffic light infos
// status info
// Right
// Left
// TODO: uint8_t PORTUPPERCASE will be replaced by according PORTx
// Parsing of DIGITS command
if (ReceivedCommand.d.cmd == 0b0)
{
// Parse command only if is same side as this panel (or comamnd says to override)
if (ReceivedCommand.d.sideOverride || ReceivedCommand.d.side == cfg.PanelSide)
{
uint8_t PORTLEFT = segment[ReceivedCommand.d.lDigit];
uint8_t PORTMID = segment[ReceivedCommand.d.mDigit];
uint8_t PORTRIGHT = segment[ReceivedCommand.d.rDigit];
}
// Turn on : between minutes and seconds only if count is in minutes and its digit is a representable value (not dash)
if (ReceivedCommand.d.lDigit != CHAR_DASH && ReceivedCommand.d.minutes)
uint8_t PORTCOMMA |= _BV(7);
}
// Parsing of TRAFFIC command
else if (ReceivedCommand.t.cmd < 0b111)
{
if (cfg.BuzzerStatus == BUZZER_ON)
{
uint8_t PORTBUZZER |= ReceivedCommand.t.buzzer;
}
if (cfg.PanelSide == RIGHT)
uint8_t PORTSEMAPHORE |= ReceivedCommand.t.TrafficLightRight;
else
uint8_t PORTSEMAPHORE |= ReceivedCommand.t.TrafficLightLeft;
uint8_t PORTABCD |= ReceivedCommand.t.ArcherGroups;
// at the moment ignore EF, not in this panel
/*
digitalWrite(6, (((statevalue >> 7) & B00000111) != 2) and (trafficvalue & 0x4000)); //E
digitalWrite(7, (((statevalue >> 7) & B00000111) != 2) and (trafficvalue & 0x8000)); //F
digitalWrite(A15, (((statevalue >> 7) & B00000111) == 2) and (trafficvalue & 0x4000)); //green right arrow for fita finals
digitalWrite(39, (((statevalue >> 7) & B00000111) == 2) and (trafficvalue & 0x8000)); //green left arrow for fita finals
*/
}
// Parsing of STATE command
else if (ReceivedCommand.s.cmd == 0b1111)
{
cfg.abcd = ReceivedCommand.s.abcd;
cfg.competition = ReceivedCommand.s.competition;
cfg.GroupsNumber = ReceivedCommand.s.GroupsNumber;
}
// not managed endnr value
/*
lefend = ((endnrvalue >> 4) & B00001111);
midend = ((endnrvalue >> 8) & B00001111);
rigend = ((endnrvalue >> 12) & B00001111);
if (lefend == 15)
{
comend = lefend;
}
else
{
comend = midend;
};
digitalWrite(24, ((segment[comend] & 0x001)) ? HIGH : LOW); //left combined end digit segment A
digitalWrite(26, ((segment[comend] & 0x002)) ? HIGH : LOW); //left combined end digit segment B
digitalWrite(28, ((segment[comend] & 0x004)) ? HIGH : LOW); //left combined end digit segment C
digitalWrite(30, ((segment[comend] & 0x008)) ? HIGH : LOW); //left combined end digit segment D
digitalWrite(32, ((segment[comend] & 0x010)) ? HIGH : LOW); //left combined end digit segment E
digitalWrite(34, ((segment[comend] & 0x020)) ? HIGH : LOW); //left combined end digit segment F
digitalWrite(36, ((segment[comend] & 0x040)) ? HIGH : LOW); //left combined end digit segment G
digitalWrite(40, ((segment[rigend] & 0x001)) ? HIGH : LOW); //right end digit segment A
digitalWrite(42, ((segment[rigend] & 0x002)) ? HIGH : LOW); //right end digit segment B
digitalWrite(44, ((segment[rigend] & 0x004)) ? HIGH : LOW); //right end digit segment C
digitalWrite(46, ((segment[rigend] & 0x008)) ? HIGH : LOW); //right end digit segment D
digitalWrite(48, ((segment[rigend] & 0x010)) ? HIGH : LOW); //right end digit segment E
digitalWrite(50, ((segment[rigend] & 0x020)) ? HIGH : LOW); //right end digit segment F
digitalWrite(52, ((segment[rigend] & 0x040)) ? HIGH : LOW); //right end digit segment G
*/
}
else // if (char) digit != '\n'
{
inputString += digit;
}
}
//last 2 bits of trafic value show the number of archers. In this way we can determine notA, notB, notC and notD
/*
if (blinkk == 0)
{
blinkk = 300;
}
else
{
blinkk--;
};
if (blinkkk == 0)
{
blinkkk = 2400;
}
else
{
blinkkk--;
};
if (blinkkk > 1200)
{
blinkr = 0;
blinkl = (blinkk > 200);
}
else
{
blinkr = (blinkk > 200);
blinkl = 0;
};
*/
}