Skip to content

Commit

Permalink
timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
dlyubimov committed Jul 5, 2017
1 parent 268956e commit bee360a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
41 changes: 29 additions & 12 deletions Hydra_EVSE/Hydra_EVSE.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define ___HYDRA_EVSE_H___

// Standard Arduino types like boolean
#include <Arduino.h>
#include <Arduino.h>

#include <avr/wdt.h>
#include <Wire.h>
Expand Down Expand Up @@ -341,10 +341,23 @@ extern char p_buffer[];
#include "units.h"
#endif

struct timeouts_struct {
unsigned long sequential_pilot_timeout;
unsigned long button_press_time, button_debounce_time;

timeout_struct() {
clear();
}

void clear() {
memset(this, 0, sizeof(*this));
}
};

struct car_struct {
// CAR_A or CAR_B
unsigned char car;
car_struct& them;
car_struct& them;
unsigned int relay_pin, pilot_out_pin, pilot_sense_pin, current_pin;
volatile unsigned int relay_state;
unsigned int last_state;
Expand All @@ -356,9 +369,9 @@ struct car_struct {
unsigned int pilot_state;
EWASumD ammSum;

car_struct(unsigned int car, int themOffset, unsigned int relay_pin,
unsigned int pilot_out_pin, unsigned int pilot_sense_pin, unsigned int current_pin) :
car(car),
car_struct(unsigned int car, int themOffset, unsigned int relay_pin,
unsigned int pilot_out_pin, unsigned int pilot_sense_pin, unsigned int current_pin) :
car(car),
them(*(this + themOffset)),
relay_pin(relay_pin),
pilot_out_pin(pilot_out_pin),
Expand Down Expand Up @@ -387,11 +400,15 @@ struct car_struct {
void loopCurrentMonitor();
void loopCheckDelayedTransition();
void loopSeqHandover(unsigned long nowMs);
// Inlines
char carLetter() { return 'A' + car - CAR_A; }
// Inlines
char carLetter() {
return 'A' + car - CAR_A;
}
// Returns 0 for car A and 8 for car B. Typically, to print display status or current.
unsigned int dispCol() { return 8 * ( car - CAR_A ); }

unsigned int dispCol() {
return 8 * ( car - CAR_A );
}

};

#define EVENT_COUNT 4
Expand Down Expand Up @@ -421,10 +438,10 @@ typedef struct event_struct {
// Calibration menu items

// this is in 0.1A units
#define CALIB_AMM_MAX 5
#define CALIB_AMM_MAX 5

// this is in -% units. Can derate pilots up to 5%.
#define CALIB_PILOT_MAX 10
#define CALIB_PILOT_MAX 10

typedef struct calib_struct {
char amm_a, amm_b, pilot_a, pilot_b;
Expand Down Expand Up @@ -514,7 +531,7 @@ extern DSTRule dstRules[2];
static inline time_t localTime()
{
time_t t = now();
return persisted.enable_dst && isSummer(dstRules, t)? t + SECS_PER_HOUR : t;
return persisted.enable_dst && isSummer(dstRules, t) ? t + SECS_PER_HOUR : t;
}


Expand Down
50 changes: 26 additions & 24 deletions Hydra_EVSE/Hydra_EVSE.ino
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ US_DST_RULES(dstRules);
// strings that are then used in snprintf statements that themselves use this macro.
char p_buffer[96];

// "display" global variable definition
// "display" global variable definition -- this may be a different device however it must support
// all methods we use here. This macro is defined based on hardware configuration (different for
// my unit tests and 2.3.1 hardware).
DISPLAY_DEF(display);


Expand All @@ -93,11 +95,10 @@ car_struct cars[] =
};
// TODO: in theory once we remove symmetry duplication in the code, we won't need this.
car_struct &car_a(cars[0]), &car_b(cars[1]);
timeouts_struct timeouts;

unsigned long last_state_log;
unsigned long sequential_pilot_timeout;
unsigned char &operatingMode(persisted.operatingMode), sequential_mode_tiebreak;
unsigned long button_press_time, button_debounce_time;
#ifdef GROUND_TEST
unsigned char current_ground_status;
#endif
Expand Down Expand Up @@ -428,7 +429,7 @@ void error(unsigned int status)
// (that is, turn off the oscillator) whenever we want.
// Stop flipping, one way or another
unsigned int car = status & CAR_MASK;
sequential_pilot_timeout = 0;
timeouts.sequential_pilot_timeout = 0;
if (car == BOTH || car == CAR_A)
{
car_a.setPilot(HIGH);
Expand Down Expand Up @@ -713,7 +714,7 @@ void car_struct::sequential_mode_transition(unsigned int car_state)
setRelay(LOW);
setPilot(HIGH);
// We're not both in state B anymore.
sequential_pilot_timeout = 0;
timeouts.sequential_pilot_timeout = 0;
// We don't exist. If they're waiting, they can have it.
if (their_state == STATE_B)
{
Expand Down Expand Up @@ -759,7 +760,7 @@ void car_struct::sequential_mode_transition(unsigned int car_state)
// display.print(P(": done ")); // differentiated from "wait" because a C/D->B transition has occurred.
// Disable future charges for this car until re-unpaused or re-plugged.
seq_done = true;
sequential_pilot_timeout = millis(); // We're both now in B. Start flipping.
timeouts.sequential_pilot_timeout = millis(); // We're both now in B. Start flipping.
}
else
{
Expand All @@ -769,7 +770,7 @@ void car_struct::sequential_mode_transition(unsigned int car_state)
// display.print((us == CAR_A) ? "A" : "B");
// display.print(P(": off "));
// their state is not B, so we're not "flipping"
sequential_pilot_timeout = 0;
timeouts.sequential_pilot_timeout = 0;
}
}
else
Expand All @@ -780,7 +781,7 @@ void car_struct::sequential_mode_transition(unsigned int car_state)
setPilot(FULL);
seq_done = false;
them.seq_done = false;
sequential_pilot_timeout = 0;
timeouts.sequential_pilot_timeout = 0;

displayStatus(car | STATUS_OFF );
// display.setCursor((us == CAR_A) ? 0 : 8, 1);
Expand All @@ -803,7 +804,7 @@ void car_struct::sequential_mode_transition(unsigned int car_state)
if (!seq_done && (sequential_mode_tiebreak == car ))
{
setPilot(FULL);
sequential_pilot_timeout = millis();
timeouts.sequential_pilot_timeout = millis();

displayStatus (car | STATUS_OFF);
// display.setCursor((us == CAR_A) ? 0 : 8, 1);
Expand Down Expand Up @@ -835,7 +836,7 @@ void car_struct::sequential_mode_transition(unsigned int car_state)
return;
}
// We're not both in state B anymore
sequential_pilot_timeout = 0;
timeouts.sequential_pilot_timeout = 0;
displayStatus(car | STATUS_ON );
// display.setCursor((us == CAR_A) ? 0 : 8, 1);
// display.print((us == CAR_A) ? "A" : "B");
Expand Down Expand Up @@ -976,37 +977,37 @@ unsigned int checkTimer()
unsigned int checkEvent()
{
logTrace(P("Checking for button event"));
if (button_debounce_time != 0 && millis() - button_debounce_time < BUTTON_DEBOUNCE_INTERVAL)
if (timeouts.button_debounce_time != 0 && millis() - timeouts.button_debounce_time < BUTTON_DEBOUNCE_INTERVAL)
{
// debounce is in progress
return EVENT_NONE;
}
else
{
// debounce is over
button_debounce_time = 0;
timeouts.button_debounce_time = 0;
}
unsigned int buttons = display.readButtons();
logTrace(P("Buttons %d"), buttons);
if ((buttons & BUTTON) != 0)
{
logTrace(P("Button is down"));
// Button is down
if (button_press_time == 0) // this is the start of a press.
if (timeouts.button_press_time == 0) // this is the start of a press.
{
button_debounce_time = button_press_time = millis();
timeouts.button_debounce_time = timeouts.button_press_time = millis();
}
return EVENT_NONE; // We don't know what this button-push is going to be yet
}
else
{
logTrace(P("Button is up"));
// Button released
if (button_press_time == 0) return EVENT_NONE; // It wasn't down anyway.
if (timeouts.button_press_time == 0) return EVENT_NONE; // It wasn't down anyway.
// We are now ending a button-push. First, start debuncing.
button_debounce_time = millis();
unsigned long button_pushed_time = button_debounce_time - button_press_time;
button_press_time = 0;
timeouts.button_debounce_time = millis();
unsigned long button_pushed_time = timeouts.button_debounce_time - timeouts.button_press_time;
timeouts.button_press_time = 0;
if (button_pushed_time > BUTTON_LONG_START)
{
logDebug(P("Button long-push event"));
Expand Down Expand Up @@ -1756,9 +1757,10 @@ void setup()
car_a.setRelay(LOW);
car_b.setRelay(LOW);

button_debounce_time = 0;
button_press_time = 0;
sequential_pilot_timeout = 0;
timeouts.clear();
// button_debounce_time = 0;
// button_press_time = 0;
// sequential_pilot_timeout = 0;
last_minute = 99;
relay_change_time = 0;
#ifdef QUICK_CYCLING_WORKAROUND
Expand Down Expand Up @@ -1999,7 +2001,7 @@ void car_struct::loopSeqHandover(unsigned long nowMs) {
// move the pilot offer.
setPilot(HIGH);
them.setPilot(FULL);
sequential_pilot_timeout = nowMs;
timeouts.sequential_pilot_timeout = nowMs;
displayStatus(car | (seq_done ? STATUS_DONE : STATUS_WAIT));
displayStatus(them.car | STATUS_OFF );
}
Expand Down Expand Up @@ -2190,10 +2192,10 @@ void loop()
car_b.loopCheckPilot(car_b_state);


if (sequential_pilot_timeout != 0)
if (timeouts.sequential_pilot_timeout != 0)
{
unsigned long nowMs = millis();
if (nowMs - sequential_pilot_timeout > SEQ_MODE_OFFER_TIMEOUT)
if (nowMs - timeouts.sequential_pilot_timeout > SEQ_MODE_OFFER_TIMEOUT)
{
if (car_a.pilot_state == FULL)
car_a.loopSeqHandover(nowMs);
Expand Down

0 comments on commit bee360a

Please sign in to comment.