Ejemplo n.º 1
0
// Returns true if we have a current display message request and it's still within it
// min time period.
static boolean isActiveDisplayMessage() {
  if (current_display_message_code == display_messages::code::kNone) {
    return false;
  } 

  if (time_in_current_display_message.timeMillis() < current_display_message_min_time_millis) {
    return true;
  }
  // Display message expired, mark as done.
  current_display_message_code = display_messages::code::kNone;
  return false;
}
Ejemplo n.º 2
0
// Called periodically from loop() to update the state machine.
static inline void updateState() {
  // Meta rule: inject IFF in INJECT state.
  {
    const boolean injecting = (state == states::IGNITION_ON_INJECT);
    custom_injector::setInjectionsEnabled(injecting);
    leds::status.set(injecting);
  }
   
  // Meta rule: if ignition is known to be off, reset to IGNITION_OFF_IDLE state.
  if (custom_signals::ignition_state().isOff()) {
    if (state != states::IGNITION_OFF_IDLE) {
      changeToState(states::IGNITION_OFF_IDLE);
    }  
    return;
  }
    
  // Handle the state transitions.
  switch (state) {
    case states::IGNITION_OFF_IDLE:
      if (custom_signals::ignition_state().isOnForAtLeastMillis(1000)) {
        changeToState(states::IGNITION_ON_MAYBE_INJECT);  
      }
      break;
      
    case states::IGNITION_ON_MAYBE_INJECT:
      changeToState(custom_config::is_enabled() 
          ? states::IGNITION_ON_INJECT 
          : states::IGNITION_ON_IDLE);  
      return;
    
    case states::IGNITION_ON_INJECT:
      // NOTE: the meta rule at the begining of this method enables injection
      // as long as state is INJECT. We don't need to control injection here.
      if (time_in_state.timeMillis() > 500) {
        changeToState(states::IGNITION_ON_IDLE);
      }
      break;
    
    case states::IGNITION_ON_IDLE:
      // Nothing to do here. Stay in this state until ignition is
      // turned off.
      break;
    
    // Unknown state, set to initial.
    default:
      sio::printf(F("injection state: unknown (%d)"), state);
      changeToState(states::IGNITION_OFF_IDLE);
      break;
  } 
}
Ejemplo n.º 3
0
  // Called periodically from loop() to update the state machine.
  static inline void updateState() {
    // Valid in IGNITION_ON_COUNTING state only.
    static uint8 button_click_count;
    static uint8 button_last_state;

    // Handle the state transitions.
    switch (state) {
      case states::IGNITION_OFF_IDLE:
        if (custom_signals::ignition_state().isOn()) {
          button_click_count = 0;
          button_last_state = custom_signals::config_button().state();
          changeToState(states::IGNITION_ON_COUNTING);  
        }
        break;

      case states::IGNITION_ON_COUNTING: 
        {
          // If sequence takes too long too long or too many clicks then ignore.
          if (time_in_state.timeMillis() > kSequenceTimeoutMillis || button_click_count > kExpectedButtonClicks) {
            changeToState(states::IGNITION_ON_IDLE);  
            break;
          }
  
          // If ignition turned off, see if we have the conditions to toggle configuration.
          if (custom_signals::ignition_state().isOff()) {
            changeToState(button_click_count == kExpectedButtonClicks 
                ? states::IGNITION_OFF_TOGGLE_CONFIG 
                : states::IGNITION_OFF_IDLE);
            break;
          }
  
          const uint8 button_new_state = custom_signals::config_button().state();
          // Count change from non pressed to pressed.
          if ((button_last_state == SignalTracker::States::OFF) && (button_new_state == SignalTracker::States::ON)) {
            // This cannot overflow because we exist this state if exceeding kExpectedButtonClicks. 
            button_click_count++;
            sio::printf(F("config state: %d.%d\n"), states::IGNITION_ON_COUNTING, button_click_count);
          }
          button_last_state = button_new_state;
        }
        break;

      case states::IGNITION_ON_IDLE:
        if (custom_signals::ignition_state().isOff()) {
          changeToState(states::IGNITION_OFF_IDLE);  
        }
        break;

      case states::IGNITION_OFF_TOGGLE_CONFIG:
        toggleConfig();
        changeToState(states::IGNITION_OFF_IDLE);
        break;

      // Unknown state, set to initial.
      default:
        sio::printf(F("config state: unknown (%d)"), state);
        // Go to a default state and wait there until ignition is off.
        changeToState(states::IGNITION_ON_IDLE);
        break;
    } 
  }