Skip to content

Commit

Permalink
Merge branch 'f/invert'
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbig committed Jun 25, 2016
2 parents c315d59 + b0daae2 commit 26b790a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 16 deletions.
12 changes: 9 additions & 3 deletions alertled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ uint16_t AlertLED::receptionCounter = 0;
// Current reception status
byte AlertLED::receptionStatus = 0;

// Inverted mode
bool AlertLED::inverted = false;

/**
* Register event handlers, and start timer
* @param EventManager the event manager used for events
* @param bool wether we are in inverted mode.
*/
void AlertLED::init(EventManager* eventManager) {
void AlertLED::init(EventManager* eventManager, bool isInverted) {
inverted = isInverted;

pinMode(CFG_LED_ALERT1, OUTPUT);
pinMode(CFG_LED_ALERT2, OUTPUT);
pinMode(CFG_BEEPER, OUTPUT);
digitalWrite(CFG_LED_ALERT1, 1);
digitalWrite(CFG_LED_ALERT2, 1);
digitalWrite(CFG_LED_ALERT1, 1 - inverted);
digitalWrite(CFG_LED_ALERT2, 1 - inverted);
digitalWrite(CFG_BEEPER, 1);

alertStatus = ALERT_NONE;
Expand Down
20 changes: 15 additions & 5 deletions alertled.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ class AlertLED {
/**
* Register event handlers, and start timer
* @param EventManager the event manager used for events
* @param bool wether we should work in inverted mode
*/
static void init(EventManager* eventManager);
static void init(EventManager* eventManager, bool isInverted);

/**
* Progress the current animation
Expand All @@ -39,17 +40,22 @@ class AlertLED {
if (alertStatus == ALERT_NONE) {
if (!animPtr) return;
animPtr = 0;
analogWrite(CFG_LED_ALERT1, 1023);
analogWrite(CFG_LED_ALERT2, 1023);
analogWrite(CFG_LED_ALERT1, inverted ? 0 : 1023);
analogWrite(CFG_LED_ALERT2, inverted ? 0 : 1023);
analogWrite(CFG_BEEPER, 1023);
return;
}

if (animPtr >= sizeof(anim_warning[alertStatus])/sizeof(ALED_CFG)) animPtr = 0;

memcpy_P(&anim, &anim_warning[alertStatus][animPtr], sizeof(ALED_CFG));
analogWrite(CFG_LED_ALERT1, (anim.lft - 1023) * mult + 1023);
analogWrite(CFG_LED_ALERT2, (anim.rgt - 1023) * mult + 1023);
if (inverted) {
analogWrite(CFG_LED_ALERT1, (1024 -anim.lft) * mult);
analogWrite(CFG_LED_ALERT2, (1024 - anim.rgt) * mult);
} else {
analogWrite(CFG_LED_ALERT1, (anim.lft - 1023) * mult + 1023);
analogWrite(CFG_LED_ALERT2, (anim.rgt - 1023) * mult + 1023);
}
analogWrite(CFG_BEEPER, anim.beeper);

animPtr++;
Expand All @@ -71,6 +77,10 @@ class AlertLED {
static void resetCallback(int eventCode, int eventParam);

protected:
/**
* Should we work in inverted mode?
*/
static bool inverted;
/**
* Set to the current alert status
*/
Expand Down
6 changes: 5 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define BOARD_VERSION "b1"

// Software version
#define FW_VERSION "1.4"
#define FW_VERSION "1.5"

// Enable / disabled modules
#define ENABLE_STATUSLED 1
Expand All @@ -23,6 +23,10 @@
// GPIO port of the debug button
#define CFG_BTN 0

// GPIO ports connected when in inverted mode
#define INVERT_OUT 5
#define INVERT_IN 15

// Tolerance in heading from the requiered in degress
#define CFG_HDG_TOLERANCE 45

Expand Down
12 changes: 9 additions & 3 deletions statusled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ void StatusLED::setStatus(byte state) {
void StatusLED::tick() {
if (!gpsStatus) {
// Display sweep animation whene there is no reception
analogWrite(CFG_LED_STATUS, animPulse[pos]);
analogWrite(CFG_LED_STATUS, inverted ? 1023 - animPulse[pos] : animPulse[pos]);
pos++;
if (pos >= sizeof(animPulse) / 2) pos = 0;
} else {
// Short pings when reception acquired
if (pos == 0) analogWrite(CFG_LED_STATUS, GPS::isNight() ? 900 : 1);
else analogWrite(CFG_LED_STATUS, 1023);
if (pos == 0) {
if (GPS::isNight()) {
analogWrite(CFG_LED_STATUS, inverted ? 123 : 900);
} else {
analogWrite(CFG_LED_STATUS, inverted ? 1023 : 1);
}
}
else analogWrite(CFG_LED_STATUS, inverted ? 0 : 1023);
if (++pos >= 150) pos = 0;
}
}
Expand Down
6 changes: 5 additions & 1 deletion statusled.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class StatusLED {
* Register event handlers, and start timer
* @param EventManager the event manager used for events
*/
static void init(EventManager* eventManager) {
static void init(EventManager* eventManager, bool inverted) {
instance = new StatusLED();
instance->inverted = inverted;

eventManager->addListener(GPS_STATUS_CHANGED, &StatusLED::statusChangedCallback);
};
Expand All @@ -42,6 +43,9 @@ class StatusLED {
// This is the singleton instance
static StatusLED* instance;

// Should we work in inverted mode?
bool inverted = false;

// Status of the gps
byte gpsStatus = -1;

Expand Down
46 changes: 43 additions & 3 deletions tiveda.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,26 @@ boolean inAlert = false;
// This is set true while we're waiting for wifi
boolean wifiConnecting = false;

// Wether this board uses active high output for leds
boolean inverted = false;
/**
* System initialization
*/
void setup() {
// Init serial communication
Serial.begin(9600);
//Serial.setDebugOutput(true);
serialBuffer.reserve(200);
message.reserve(200);

inverted = isBoardInverted();
#ifdef DEBUG
if (inverted) {
Serial.println("Board is inverted");
} else {
Serial.println("Board is normal");
}
#endif

// Setup PWM frequency
analogWriteFreq(880);

Expand All @@ -84,10 +94,10 @@ void setup() {

// Initialize output modules
#ifdef ENABLE_STATUSLED
StatusLED::init(&eventManager);
StatusLED::init(&eventManager, inverted);
#endif
#ifdef ENABLE_ALERTLED
AlertLED::init(&eventManager);
AlertLED::init(&eventManager, inverted);
#endif

// First events
Expand Down Expand Up @@ -344,3 +354,33 @@ void performOTA() {
}
}


/**
* Check if the current board is inverted
* D8 and D1 should be connected. We set D1 to various
* states and check if D8 does match
*
* This method blocks for ~50ms
* @return bool true if the board matches the criteria
*/
bool isBoardInverted() {
pinMode(INVERT_OUT, OUTPUT);
pinMode(INVERT_IN, INPUT);

digitalWrite(INVERT_OUT, HIGH);
delay(10);
if (digitalRead(INVERT_IN) != HIGH) return false;
digitalWrite(INVERT_OUT, LOW);
delay(10);
if (digitalRead(INVERT_IN) != LOW) return false;
digitalWrite(INVERT_OUT, HIGH);
delay(10);
if (digitalRead(INVERT_IN) != HIGH) return false;
digitalWrite(INVERT_OUT, LOW);
delay(10);
if (digitalRead(INVERT_IN) != LOW) return false;

pinMode(INVERT_OUT, INPUT);
return true;
}

0 comments on commit 26b790a

Please sign in to comment.