ESR-2025/src/door_sensor.c

56 lines
1.6 KiB
C

#include <stdint.h>
#include <msp430.h>
#include <driverlib.h>
#include <stdbool.h>
#include "door_sensor.h"
#include "constants.h"
#define SENSOR_PORT GPIO_PORT_P2
#define SENSOR_PIN GPIO_PIN0
#define SENSOR_VECTOR PORT2_VECTOR
/** User callback invoked on confirmed key press */
static DoorOpenedCallback_t doorOpenedCallback = NULL;
static DoorClosedCallback_t doorClosedCallback = NULL;
void door_init(DoorOpenedCallback_t ocb, DoorClosedCallback_t ccb) {
doorOpenedCallback = ocb;
doorClosedCallback = ccb;
/* Configure port as pull up with interrupt */
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P2, GPIO_PIN0);
GPIO_clearInterrupt(GPIO_PORT_P2, GPIO_PIN0);
GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN0, GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_enableInterrupt(GPIO_PORT_P2, GPIO_PIN0);
/* Enable global interrupts */
__enable_interrupt();
}
volatile bool door_last_open = false;
/**
* @brief Interrupt Service Routine for PORT2
*/
#pragma vector=SENSOR_VECTOR
__interrupt void SENSOR_ISR(void)
{
uint16_t status = GPIO_getInterruptStatus(SENSOR_PORT, SENSOR_PIN);
if (status) {
if (door_last_open) {
if (doorClosedCallback)
doorClosedCallback();
GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN0, GPIO_LOW_TO_HIGH_TRANSITION);
} else {
if (doorOpenedCallback)
doorOpenedCallback();
GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN0, GPIO_HIGH_TO_LOW_TRANSITION);
}
}
GPIO_clearInterrupt(SENSOR_PORT, SENSOR_PIN);
door_last_open = !door_last_open;
}