Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_onewire_relay.c
1 #include <bc_gpio.h>
2 #include <bc_onewire_relay.h>
3 
4 bool _bc_onewire_relay_read_state(bc_onewire_relay_t *self);
5 
6 bool bc_onewire_relay_init(bc_onewire_relay_t *self, bc_gpio_channel_t channel, uint64_t device_number)
7 {
8  memset(self, 0, sizeof(*self));
9  self->_device_number = device_number;
10  self->_channel = channel;
11 
12  bc_onewire_init(self->_channel);
13 
14  if (!_bc_onewire_relay_read_state(self))
15  {
16  return false;
17  }
18 
19  return true;
20 }
21 
22 bool bc_onewire_relay_set_state(bc_onewire_relay_t *self, bc_onewire_relay_channel_t relay_channel, bool state)
23 {
24  if (!self->_state_valid && !_bc_onewire_relay_read_state(self))
25  {
26  return false;
27  }
28 
29  if (!bc_onewire_reset(self->_channel))
30  {
31  return false;
32  }
33 
34  uint8_t new_state = self->_state;
35 
36  if (!state)
37  {
38  new_state |= (1 << relay_channel);
39  }
40  else
41  {
42  new_state &= ~(1 << relay_channel);
43  }
44 
45  bc_onewire_select(self->_channel, &self->_device_number);
46 
47  uint8_t buffer[] = { 0x5A, new_state, ~new_state };
48 
49  bc_onewire_write(self->_channel, buffer, sizeof(buffer));
50 
51  if (bc_onewire_read_8b(self->_channel) != 0xAA)
52  {
53  return false;
54  }
55 
56  if (bc_onewire_read_8b(self->_channel) != new_state)
57  {
58  return false;
59  }
60 
61  self->_state = new_state;
62 
63  return true;
64 }
65 
66 bool bc_onewire_relay_get_state(bc_onewire_relay_t *self, bc_onewire_relay_channel_t relay_channel, bool *state)
67 {
68  if (!self->_state_valid)
69  {
70  return false;
71  }
72 
73  *state = ((self->_state >> relay_channel) & 0x01) == 0x00;
74 
75  return true;
76 }
77 
78 bool _bc_onewire_relay_read_state(bc_onewire_relay_t *self)
79 {
80  self->_state_valid = false;
81 
82  if (!bc_onewire_reset(self->_channel))
83  {
84  return false;
85  }
86 
87  bc_onewire_select(self->_channel, &self->_device_number);
88 
89  bc_onewire_write_8b(self->_channel, 0xF5);
90 
91  self->_state = bc_onewire_read_8b(self->_channel);
92 
93  self->_state_valid = true;
94 
95  return true;
96 }
bool bc_onewire_relay_init(bc_onewire_relay_t *self, bc_gpio_channel_t channel, uint64_t device_number)
Initialize relay.
void bc_onewire_write_8b(bc_gpio_channel_t channel, uint8_t data)
Select device.
Definition: bc_onewire.c:152
bool bc_onewire_reset(bc_gpio_channel_t channel)
Reset the 1-Wire bus and return the presence of any device.
Definition: bc_onewire.c:91
void bc_onewire_init(bc_gpio_channel_t channel)
Initialize 1-Wire.
Definition: bc_onewire.c:35
void bc_onewire_select(bc_gpio_channel_t channel, uint64_t *device_number)
Select device.
Definition: bc_onewire.c:104
bool bc_onewire_relay_get_state(bc_onewire_relay_t *self, bc_onewire_relay_channel_t relay_channel, bool *state)
Get current relay state.
bc_gpio_channel_t
GPIO channels.
Definition: bc_gpio.h:12
void bc_onewire_write(bc_gpio_channel_t channel, const void *buffer, size_t length)
Select device.
Definition: bc_onewire.c:132
bool bc_onewire_relay_set_state(bc_onewire_relay_t *self, bc_onewire_relay_channel_t relay_channel, bool state)
Set relay to specified state.
BigClown 1-wire relay instance.
uint8_t bc_onewire_read_8b(bc_gpio_channel_t channel)
Select device.
Definition: bc_onewire.c:159