ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
irrigation.c
Go to the documentation of this file.
1#include "irrigation.h"
2
3#include "../../proto/transcoder.h"
4#include "../../sensors/solenoid.h"
5#include "../communication.h"
6
7extern unsigned int g_controller_i2c_timeout;
8
9ControllerStatus IrrigationCommandTransaction(const IrrigationCommand* input,
10 IrrigationCommand* output);
11
12ControllerStatus IrrigationCommandTransaction(const IrrigationCommand* input,
13 IrrigationCommand* output) {
14 // get reference to tx and rx buffers
15 Buffer* tx = ControllerTx();
16 Buffer* rx = ControllerRx();
17
18 // encode command
19 tx->len = EncodeIrrigationCommand(input, tx->data, tx->size);
20
21 // send transaction
23 status = ControllerTransaction();
24 if (status != CONTROLLER_SUCCESS) {
25 return status;
26 }
27
28 // check for errors
29 if (rx->len == 0) {
30 return CONTROLLER_ERROR;
31 }
32
33 // decode command
34 Esp32Command cmd = Esp32Command_init_default;
35 cmd = DecodeEsp32Command(rx->data, rx->len);
36
37 // copy output data
38 memcpy(output, &cmd.command.irrigation_command, sizeof(IrrigationCommand));
39
40 return CONTROLLER_SUCCESS;
41}
42
43IrrigationCommand_State ControllerIrrigationCheck(void) {
44 IrrigationCommand irrigation_cmd = IrrigationCommand_init_zero;
45 irrigation_cmd.type = IrrigationCommand_Type_CHECK;
46
47 IrrigationCommand resp = IrrigationCommand_init_zero;
48
49 if (IrrigationCommandTransaction(&irrigation_cmd, &resp) !=
51 return IrrigationCommand_State_OPEN;
52 }
53
54 // Add verbose output
55 // const char* state_name = "UNKNOWN";
56 // switch (resp.state) {
57 // case IrrigationCommand_State_OPEN:
58 // state_name = "OPEN";
59 // break;
60 // case IrrigationCommand_State_CLOSE:
61 // state_name = "CLOSE";
62 // break;
63 // default:
64 // state_name = "ERROR";
65 // break;
66 //}
67
68 // Control solenoid based on the received state
69 switch (resp.state) {
70 case IrrigationCommand_State_OPEN:
72 break;
73 case IrrigationCommand_State_CLOSE:
75 break;
76 default:
77 break;
78 }
79
80 return resp.state;
81}
Buffer * ControllerTx(void)
Get reference to transmit buffer.
Buffer * ControllerRx(void)
Get reference to receive buffer.
ControllerStatus ControllerTransaction(void)
Send message and receive response from esp32.
ControllerStatus
@ CONTROLLER_ERROR
@ CONTROLLER_SUCCESS
IrrigationCommand_State ControllerIrrigationCheck(void)
Definition irrigation.c:43
size_t EncodeIrrigationCommand(const IrrigationCommand *irrigation_cmd, uint8_t *buffer, size_t size)
Encodes an IrrigationCommand.
Definition transcoder.c:322
Esp32Command DecodeEsp32Command(const uint8_t *data, const size_t len)
Decodes an Esp32Command message.
Definition transcoder.c:246
unsigned int g_controller_i2c_timeout
ControllerStatus IrrigationCommandTransaction(const IrrigationCommand *input, IrrigationCommand *output)
Definition irrigation.c:12
Irrigation interface implementation with the esp32.
void SolenoidOpen(void)
Returns both the raw voltage value and a calibrated measurement from a water Flow sensor.
Definition solenoid.c:35
void SolenoidClose(void)
Read water Flow sensor and serialize measurement.
Definition solenoid.c:43
size_t len
size_t size
uint8_t * data