2020-05-26 13:44:02 +02:00
|
|
|
#include <Arduino.h>
|
2020-06-22 11:47:25 +02:00
|
|
|
#include <TimerOne.h> // use IDE menu Tools..Manage Libraries to locate and install TimerOne
|
2020-05-26 13:44:02 +02:00
|
|
|
#include "Hardware.h"
|
|
|
|
#include "Config.h"
|
2020-05-29 16:44:57 +02:00
|
|
|
#include "DIAG.h"
|
2020-05-26 13:44:02 +02:00
|
|
|
|
2020-06-04 21:30:11 +02:00
|
|
|
#if defined(ARDUINO_ARCH_AVR)
|
2020-06-22 11:47:25 +02:00
|
|
|
#include <DIO2.h> // use IDE menu Tools..Manage Libraries to locate and install DIO2
|
2020-06-04 21:30:11 +02:00
|
|
|
#define WritePin digitalWrite2
|
|
|
|
#else
|
|
|
|
#define WritePin digitalWrite
|
|
|
|
#endif
|
|
|
|
|
2020-05-26 13:44:02 +02:00
|
|
|
void Hardware::init() {
|
|
|
|
pinMode(MAIN_POWER_PIN, OUTPUT);
|
|
|
|
pinMode(MAIN_SIGNAL_PIN, OUTPUT);
|
|
|
|
if (MAIN_SIGNAL_PIN_ALT) pinMode(MAIN_SIGNAL_PIN_ALT, OUTPUT);
|
|
|
|
pinMode(MAIN_SENSE_PIN, INPUT);
|
|
|
|
|
|
|
|
pinMode(PROG_POWER_PIN, OUTPUT);
|
|
|
|
pinMode(PROG_SIGNAL_PIN, OUTPUT);
|
|
|
|
if (PROG_SIGNAL_PIN_ALT) pinMode(PROG_SIGNAL_PIN_ALT, OUTPUT);
|
|
|
|
pinMode(PROG_SENSE_PIN, INPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Hardware::setPower(bool isMainTrack, bool on) {
|
2020-06-04 21:30:11 +02:00
|
|
|
WritePin(isMainTrack ? MAIN_POWER_PIN : PROG_POWER_PIN, on ? HIGH : LOW);
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
2020-05-26 19:34:54 +02:00
|
|
|
void Hardware::setBrake(bool isMainTrack, bool on) {
|
2020-06-04 21:30:11 +02:00
|
|
|
WritePin(isMainTrack ? MAIN_BRAKE_PIN : PROG_BRAKE_PIN, on ? LOW:HIGH);
|
2020-05-26 19:34:54 +02:00
|
|
|
}
|
2020-05-26 13:44:02 +02:00
|
|
|
|
|
|
|
void Hardware::setSignal(bool isMainTrack, bool high) {
|
|
|
|
byte pin = isMainTrack ? MAIN_SIGNAL_PIN : PROG_SIGNAL_PIN;
|
|
|
|
byte pin2 = isMainTrack ? MAIN_SIGNAL_PIN_ALT : PROG_SIGNAL_PIN_ALT;
|
2020-06-04 21:30:11 +02:00
|
|
|
WritePin(pin, high ? HIGH : LOW);
|
|
|
|
if (pin2) WritePin(pin2, high ? LOW : HIGH);
|
2020-05-26 13:44:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Hardware::getCurrentMilliamps(bool isMainTrack) {
|
|
|
|
int pin = isMainTrack ? MAIN_SENSE_PIN : PROG_SENSE_PIN;
|
|
|
|
float factor = isMainTrack ? MAIN_SENSE_FACTOR : PROG_SENSE_FACTOR;
|
|
|
|
int rawCurrent = analogRead(pin);
|
|
|
|
return (int)(rawCurrent * factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Hardware::setCallback(int duration, void (*isr)()) {
|
2020-05-26 18:06:15 +02:00
|
|
|
Timer1.initialize(duration);
|
2020-06-22 11:47:25 +02:00
|
|
|
// We don't want the timer to set pins because these often clash with motor shields etc.
|
2020-05-26 18:06:15 +02:00
|
|
|
Timer1.disablePwm(TIMER1_A_PIN);
|
|
|
|
Timer1.disablePwm(TIMER1_B_PIN);
|
|
|
|
Timer1.attachInterrupt(isr);
|
|
|
|
}
|
2020-06-04 21:30:11 +02:00
|
|
|
|
2020-06-22 11:47:25 +02:00
|
|
|
// Railcom support functions, not yet implemented
|
|
|
|
void Hardware::setSingleCallback(int duration, void (*isr)()) {
|
|
|
|
// Timer2.initialize(duration);
|
|
|
|
// Timer2.disablePwm(TIMER1_A_PIN);
|
|
|
|
// Timer2.disablePwm(TIMER1_B_PIN);
|
|
|
|
// Timer2.attachInterrupt(isr);
|
|
|
|
}
|
|
|
|
|
2020-05-26 18:06:15 +02:00
|
|
|
void Hardware::resetSingleCallback(int duration) {
|
2020-06-22 11:47:25 +02:00
|
|
|
// if (duration==0) Timer2.stop();
|
|
|
|
// else Timer2.initialize(duration);
|
2020-05-26 18:06:15 +02:00
|
|
|
}
|