void DHT22Controller_handleI2CWrite(uint8_t package[], uint8_t packageLength) { // Example request bytes: // 0 = ACTION // 1 = PIN if (packageLength != 2) return; uint8_t action = package[0]; switch (action) { case ACTION_REGISTER_SENSOR: { uint8_t pin = package[1]; if (getPinIndex(pin) == -1) { _pins[_pinsIndex] = pin; _cache[_pinsIndex][0] = 0; // Set default to "no success". _pinsIndex++; } _pinForRead = pin; break; } } #if (DEBUG) Serial.println("Set sensor PIN to " + String(_pinForRead)); #endif }
void attachInterrupt(uint8_t pin, void (*function)(void), int mode) { volatile uint32_t *config; uint32_t cfg, mask; if (pin >= CORE_NUM_DIGITAL) return; switch (mode) { case CHANGE: mask = 0x0B; break; case RISING: mask = 0x09; break; case FALLING: mask = 0x0A; break; case LOW: mask = 0x08; break; case HIGH: mask = 0x0C; break; default: return; } mask = (mask << 16) | 0x01000000; config = portConfigRegister(pin); #if defined(KINETISK) attachInterruptVector(IRQ_PORTA, port_A_isr); attachInterruptVector(IRQ_PORTB, port_B_isr); attachInterruptVector(IRQ_PORTC, port_C_isr); attachInterruptVector(IRQ_PORTD, port_D_isr); attachInterruptVector(IRQ_PORTE, port_E_isr); voidFuncPtr* isr_table = getIsrTable(config); if(!isr_table) return; uint32_t pin_index = getPinIndex(config); __disable_irq(); cfg = *config; cfg &= ~0x000F0000; // disable any previous interrupt *config = cfg; isr_table[pin_index] = function; // set the function pointer cfg |= mask; *config = cfg; // enable the new interrupt __enable_irq(); #elif defined(KINETISL) attachInterruptVector(IRQ_PORTA, porta_interrupt); attachInterruptVector(IRQ_PORTCD, portcd_interrupt); __disable_irq(); cfg = *config; cfg &= ~0x000F0000; // disable any previous interrupt *config = cfg; intFunc[pin] = function; // set the function pointer cfg |= mask; *config = cfg; // enable the new interrupt __enable_irq(); #endif }
void detachInterrupt(uint8_t pin) { volatile uint32_t *config; config = portConfigRegister(pin); #if defined(KINETISK) voidFuncPtr* isr_table = getIsrTable(config); if(!isr_table) return; uint32_t pin_index = getPinIndex(config); __disable_irq(); *config = ((*config & ~0x000F0000) | 0x01000000); isr_table[pin_index] = dummy_isr; __enable_irq(); #elif defined(KINETISL) __disable_irq(); *config = ((*config & ~0x000F0000) | 0x01000000); intFunc[pin] = dummy_isr; __enable_irq(); #endif }
uint8_t DHT22Controller_handleI2CRead(uint8_t response[]) { int pinIndex = getPinIndex(_pinForRead); if (pinIndex == -1) { #if (DEBUG) Serial.println("Cache value for " + String(_pinForRead) + " not found"); #endif return 0; } #if (DEBUG) Serial.println("Fetching cache value from index " + String(pinIndex)); #endif for (int i = 0; i < RESPONSE_SIZE; i++) { response[i] = _cache[pinIndex][i]; } return RESPONSE_SIZE; }