Beispiel #1
0
int pwm_input_main(int argc, char *argv[])
{
	configgpio();
	
	attach_isr();
	
	set_timer(0);//timer14
	set_timer(1);//timer12
	set_timer(2);//timer3
	set_timer(3);//timer8
	
	enable_irq();
	
}
// Class instance to configure the MCP23017 and prepare arduino interrupts
void MCP23017::begin()
{
  configure();    // Configure MCP23017
  attach_isr();   // Configure Interrupt on Arduino
}
void MCP23017::handleClicks() {

  uint32_t currentMillis = millis();    // Save current millis()

  for (uint8_t x = 0; x < last_button; x++) {  // loop through buttons

    // Single/Double click handling
    if (currentMillis - buttonReleaseTime[x] >= 225) {
      if (buttonReleaseCount[x] >= 2) {
        buttonNeedHandling[x] = true;
        buttonClickType[x] = MCP_DOUBLE_CLICK;
        buttonReleaseCount[x] = 0;
#ifdef _MCP_SERIAL_DEBUG
        Serial.print(F("Double click on button "));
        Serial.println(x);
        Serial.println();
#endif
      }
      else if (buttonReleaseCount[x] == 1) {
        buttonNeedHandling[x] = true;
        buttonClickType[x] = MCP_CLICK;
        buttonReleaseCount[x] = 0;
#ifdef _MCP_SERIAL_DEBUG
        Serial.print(F("Single click on button "));
        Serial.println(x);
        Serial.println();

      }

      if (buttonNeedHandling[x] == true) {
#ifdef _MCP_SERIAL_DEBUG3
        Serial.print(F("Button handling (handle) Required for Button ID: "));
        Serial.println(x);
#endif
#endif
      }

    } // end single/double click handling

    // Button hold handling
    if (currentMillis - buttonPressTime[x] > MCP_LONG_CLICK_TIMEOUT && buttonState[x] && buttonClickType[x] != MCP_HELD_DOWN) {
      buttonNeedHandling[x] = true;
      buttonClickType[x] = MCP_HELD_DOWN;
#ifdef _MCP_SERIAL_DEBUG
      Serial.print(F("Button "));
      Serial.print(x);
      Serial.println(F(" is held down"));
      Serial.println();
#endif
    }
  } // end button loop

  if (ISR_flag) {      // If MCP23017 Interrupt flag is set....

    uint8_t _buttonID = getLastIntPin();  // Get which pin caused the interrupt
    uint32_t isr_time_diff = (uint32_t)(currentMillis - prevISRMillis); // Time difference between the time the ISR fired and now

    if (isr_time_diff >= MCP_ISR_DEBOUNCE) {  // If we have a difference of MCP_ISR_DEBOUCE...

      uint8_t _buttonState = readReg(MCP_INTCAPA);          // Read port state at the time of interrupt (INTCAP register)

      ISR_flag = false;         // Reset interrupt flag

      if (_buttonID != MCP_ERR) {   // If the function didn't return ERR...
        if (_buttonState != MCP_ERR) {

          buttonState[_buttonID] = bitRead(_buttonState, _buttonID);  // Set the button state accordingly

          if (buttonState[_buttonID]) { // Pressed
            buttonNeedHandling[_buttonID] = true;
            buttonClickType[_buttonID] = MCP_PRESS;
#ifdef _MCP_SERIAL_DEBUG
            Serial.print(F("Button press on button: "));
            Serial.println(_buttonID);
            Serial.println();
#endif
#ifdef _MCP_SERIAL_DEBUG2
            Serial.print(F("Press Time for button:  "));
            Serial.print(_buttonID);
            Serial.print(F(" : "));
            Serial.println(currentMillis - buttonPressTime[_buttonID]);
            Serial.println();
#endif
            buttonPressTime[_buttonID] = currentMillis;  // Save the time at which the button was pressed (approximately...)

          }
          else {                  // Released
#ifdef _MCP_SERIAL_DEBUG2
            Serial.print(F("Release count: "));
            Serial.print(buttonReleaseCount[_buttonID]);
            Serial.print(F("  Time: "));
            Serial.println(currentMillis - buttonReleaseTime[_buttonID]);
            Serial.println();
#endif
            if (isr_time_diff < MCP_LONG_CLICK_LENGTH) {
              buttonReleaseCount[_buttonID]++;
            }
            else if (isr_time_diff > MCP_LONG_CLICK_LENGTH && (currentMillis - buttonPressTime[_buttonID]) <= MCP_LONG_CLICK_TIMEOUT) {
              buttonNeedHandling[_buttonID] = true;
              buttonClickType[_buttonID] = MCP_LONG_CLICK;
#ifdef _MCP_SERIAL_DEBUG
              Serial.print(F("Long click on button "));
              Serial.println(_buttonID);
              Serial.println();
#endif
            }
            if (currentMillis - buttonPressTime[_buttonID] > MCP_LONG_CLICK_TIMEOUT) {

              if (buttonClickType[_buttonID] == MCP_HELD_DOWN) {
                buttonNeedHandling[_buttonID] = true;
                buttonClickType[_buttonID] = MCP_HELD_RELEASE;
#ifdef _MCP_SERIAL_DEBUG
                Serial.print(F("Button "));
                Serial.print(_buttonID);
                Serial.println(F(" was held down, now is released"));
                Serial.println();
#endif
              }
            }
            buttonPressTime[_buttonID] = 0;
            buttonReleaseTime[_buttonID] = currentMillis;
          }
        }
      } // end if _buttonID

      prevISRMillis = currentMillis;  // Save when we last checked for interrupt (approximately...)
      attach_isr();         // Re-enable interrupts on Arduino
    } // end debounce
  }
}