1 #include <twr_module_relay.h>
3 static void _twr_module_relay_task(
void *param);
5 #define TWR_MODULE_RELAY_POLARITY_F ((1 << 6) | (1 << 7))
6 #define TWR_MODULE_RELAY_POLARITY_T ((1 << 4) | (1 << 5))
7 #define TWR_MODULE_RELAY_POLARITY_NONE ((1 << 6) | (1 << 4))
31 memset(
self, 0,
sizeof(*
self));
32 self->_i2c_address = i2c_address;
33 return _twr_module_relay_hardware_init(
self);
39 if (self->_task_is_active)
45 self->_task_is_active =
false;
52 if (self->_task_is_active)
59 self->_task_is_active =
true;
94 case TWR_MODULE_RELAY_TASK_STATE_IDLE:
99 if (_twr_module_relay_hardware_init(
self))
101 self->_error =
false;
106 if (self->_command == TWR_MODULE_RELAY_COMMAND_SET)
108 self->_command = TWR_MODULE_RELAY_COMMAND_NONE;
109 self->_state = TWR_MODULE_RELAY_TASK_STATE_SET;
113 if (self->_command == TWR_MODULE_RELAY_COMMAND_PULSE)
115 self->_command = TWR_MODULE_RELAY_COMMAND_NONE;
116 self->_state = TWR_MODULE_RELAY_TASK_STATE_PULSE;
121 twr_module_relay_scheduler_unregister(
self);
127 case TWR_MODULE_RELAY_TASK_STATE_SET:
129 _twr_module_relay_set_output(
self, self->_desired_state);
130 self->_relay_state =
self->_desired_state;
132 self->_state = TWR_MODULE_RELAY_TASK_STATE_SET_DEMAGNETIZE;
133 return tick_now + 20;
135 case TWR_MODULE_RELAY_TASK_STATE_SET_DEMAGNETIZE:
137 _twr_module_relay_set_output_disable(
self);
139 self->_state = TWR_MODULE_RELAY_TASK_STATE_IDLE;
141 return tick_now + 100;
146 case TWR_MODULE_RELAY_TASK_STATE_PULSE:
148 _twr_module_relay_set_output(
self, self->_desired_state);
149 self->_relay_state =
self->_desired_state;
151 self->_state = TWR_MODULE_RELAY_TASK_STATE_PULSE_DEMAGNETIZE;
152 return tick_now + 20;
154 case TWR_MODULE_RELAY_TASK_STATE_PULSE_DEMAGNETIZE:
156 _twr_module_relay_set_output_disable(
self);
158 self->_state = TWR_MODULE_RELAY_TASK_STATE_PULSE_REVERSE;
159 return tick_now +
self->_pulse_duration;
161 case TWR_MODULE_RELAY_TASK_STATE_PULSE_REVERSE:
164 _twr_module_relay_set_output(
self, self->_relay_state);
166 self->_state = TWR_MODULE_RELAY_TASK_STATE_PULSE_DEMAGNETIZE_2;
167 return tick_now + 20;
169 case TWR_MODULE_RELAY_TASK_STATE_PULSE_DEMAGNETIZE_2:
171 _twr_module_relay_set_output_disable(
self);
173 self->_state = TWR_MODULE_RELAY_TASK_STATE_IDLE;
175 return tick_now + 100;
183 static void _twr_module_relay_task(
void *param)
193 self->_command = TWR_MODULE_RELAY_COMMAND_SET;
196 twr_module_relay_scheduler_register(
self);
214 self->_command = TWR_MODULE_RELAY_COMMAND_PULSE;
215 self->_pulse_duration = duration;
218 twr_module_relay_scheduler_register(
self);
223 return self->_relay_state;
@ TWR_I2C_I2C0
I2C channel I2C0.
twr_module_relay_state_t
Reported relay states.
void twr_module_relay_set_state(twr_module_relay_t *self, bool state)
Set relay to specified state.
struct twr_module_relay_t twr_module_relay_t
HARDWARIO Relay Module instance.
void twr_module_relay_toggle(twr_module_relay_t *self)
Toggle relay to opposite state.
void twr_module_relay_pulse(twr_module_relay_t *self, bool state, twr_tick_t duration)
Generate pulse to specified state for given duration.
bool twr_module_relay_init(twr_module_relay_t *self, uint8_t i2c_address)
Initialize HARDWARIO Relay Module.
twr_module_relay_state_t twr_module_relay_get_state(twr_module_relay_t *self)
Get current relay state.
@ TWR_MODULE_RELAY_STATE_UNKNOWN
State is unknown.
@ TWR_MODULE_RELAY_STATE_FALSE
State is false.
@ TWR_MODULE_RELAY_STATE_TRUE
State is true.
void twr_scheduler_plan_current_absolute(twr_tick_t tick)
Schedule current task to absolute tick.
void twr_scheduler_unregister(twr_scheduler_task_id_t task_id)
Unregister specified task.
twr_tick_t twr_scheduler_get_spin_tick(void)
Get current tick of spin in which task has been run.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t state)
Write state to all pins.
bool twr_tca9534a_init(twr_tca9534a_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
bool twr_tca9534a_set_port_direction(twr_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
uint64_t twr_tick_t
Timestamp data type.