Firmware SDK
twr_onewire_relay.c
1 #include <twr_gpio.h>
2 #include <twr_onewire_relay.h>
3 
4 bool _twr_onewire_relay_read_state(twr_onewire_relay_t *self);
5 
6 bool twr_onewire_relay_init(twr_onewire_relay_t *self, twr_onewire_t *onewire, uint64_t device_number)
7 {
8  memset(self, 0, sizeof(*self));
9  self->_device_number = device_number;
10  self->_onewire = onewire;
11 
12  if (!_twr_onewire_relay_read_state(self))
13  {
14  return false;
15  }
16 
17  return true;
18 }
19 
20 bool twr_onewire_relay_set_state(twr_onewire_relay_t *self, twr_onewire_relay_channel_t relay_channel, bool state)
21 {
22  if (!self->_state_valid && !_twr_onewire_relay_read_state(self))
23  {
24  return false;
25  }
26 
27  if (!twr_onewire_reset(self->_onewire))
28  {
29  return false;
30  }
31 
32  uint8_t new_state = self->_state;
33 
34  if (!state)
35  {
36  new_state |= (1 << relay_channel);
37  }
38  else
39  {
40  new_state &= ~(1 << relay_channel);
41  }
42 
43  twr_onewire_select(self->_onewire, &self->_device_number);
44 
45  uint8_t buffer[] = { 0x5A, new_state, ~new_state };
46 
47  twr_onewire_write(self->_onewire, buffer, sizeof(buffer));
48 
49  if (twr_onewire_read_byte(self->_onewire) != 0xAA)
50  {
51  return false;
52  }
53 
54  if (twr_onewire_read_byte(self->_onewire) != new_state)
55  {
56  return false;
57  }
58 
59  self->_state = new_state;
60 
61  return true;
62 }
63 
64 bool twr_onewire_relay_get_state(twr_onewire_relay_t *self, twr_onewire_relay_channel_t relay_channel, bool *state)
65 {
66  if (!self->_state_valid)
67  {
68  return false;
69  }
70 
71  *state = ((self->_state >> relay_channel) & 0x01) == 0x00;
72 
73  return true;
74 }
75 
76 bool _twr_onewire_relay_read_state(twr_onewire_relay_t *self)
77 {
78  self->_state_valid = false;
79 
80  if (!twr_onewire_reset(self->_onewire))
81  {
82  return false;
83  }
84 
85  twr_onewire_select(self->_onewire, &self->_device_number);
86 
87  twr_onewire_write_byte(self->_onewire, 0xF5);
88 
89  self->_state = twr_onewire_read_byte(self->_onewire);
90 
91  self->_state_valid = true;
92 
93  return true;
94 }
bool twr_onewire_relay_init(twr_onewire_relay_t *self, twr_onewire_t *onewire, uint64_t device_number)
Initialize relay.
uint8_t twr_onewire_read_byte(twr_onewire_t *self)
Select device.
Definition: twr_onewire.c:123
HARDWARIO 1-wire relay instance.
void twr_onewire_write_byte(twr_onewire_t *self, uint8_t data)
Select device.
Definition: twr_onewire.c:116
bool twr_onewire_relay_get_state(twr_onewire_relay_t *self, twr_onewire_relay_channel_t relay_channel, bool *state)
Get current relay state.
void twr_onewire_select(twr_onewire_t *self, uint64_t *device_number)
Select device.
Definition: twr_onewire.c:68
struct twr_onewire_t twr_onewire_t
1-Wire instance
Definition: twr_onewire.h:14
bool twr_onewire_relay_set_state(twr_onewire_relay_t *self, twr_onewire_relay_channel_t relay_channel, bool state)
Set relay to specified state.
bool twr_onewire_reset(twr_onewire_t *self)
Reset the 1-Wire bus and return the presence of any device.
Definition: twr_onewire.c:55
void twr_onewire_write(twr_onewire_t *self, const void *buffer, size_t length)
Select device.
Definition: twr_onewire.c:96