40 #include "lib/sensors.h" 45 #ifndef REED_CHECK_PERIOD 46 #define REED_CHECK_PERIOD CLOCK_SECOND 49 static int current_status = -1;
50 static struct ctimer change_timer;
51 process_event_t reed_sensor_event_changed;
59 return ~(REED_PORT_DIR & REED_READ_PIN);
61 return REED_SENSOR_ERROR;
67 if((!status(SENSORS_ACTIVE)) || (type != REED_SENSOR_VAL)) {
68 return REED_SENSOR_ERROR;
70 return (REED_PORT_READ & REED_READ_PIN) ? REED_CLOSED : REED_OPEN;
74 check_callback(
void *data)
76 static int new_status;
77 if(current_status == -1) {
82 new_status = value(REED_SENSOR_VAL);
83 if(new_status != current_status) {
84 current_status = new_status;
85 process_post(PROCESS_BROADCAST, reed_sensor_event_changed, ¤t_status);
91 configure(
int type,
int c)
96 if(!status(SENSORS_ACTIVE)) {
97 REED_PORT_SEL |= REED_READ_PIN;
98 REED_PORT_DIR &= ~REED_READ_PIN;
99 REED_PORT_REN |= REED_READ_PIN;
100 REED_PORT_PRES |= REED_READ_PIN;
103 REED_PORT_DIR |= REED_READ_PIN;
104 REED_PORT_REN &= ~REED_READ_PIN;
106 return REED_SENSOR_SUCCESS;
107 case REED_SENSOR_MODE:
108 if(c == REED_SENSOR_EVENT_MODE) {
109 current_status = value(REED_SENSOR_VAL);
110 ctimer_set(&change_timer, REED_CHECK_PERIOD, check_callback, NULL);
111 }
else if(c == REED_SENSOR_EVENT_POLL) {
115 return REED_SENSOR_ERROR;
117 return REED_SENSOR_SUCCESS;
119 return REED_SENSOR_ERROR;
122 SENSORS_SENSOR(reed_sensor, REED_SENSOR, value, configure, status);
Header file for the reed sensor.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Header file for the callback timer
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Header file for the Contiki process interface.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.