aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hvac-demo-bindings.c127
1 files changed, 67 insertions, 60 deletions
diff --git a/hvac-demo-bindings.c b/hvac-demo-bindings.c
index ea09204..a341d05 100644
--- a/hvac-demo-bindings.c
+++ b/hvac-demo-bindings.c
@@ -37,27 +37,14 @@
static const struct afb_binding_interface *interface;
// Initialize CAN hvac array that will be sent trough the socket
-static char *can_hvac_components[8] = {
- "LeftTemperature",
- "RightTemperature",
- "Temperature",
- NULL,
- "FanSpeed",
- NULL,
- NULL,
- NULL
-};
-
-// Initialize CAN hvac array that will be sent trough the socket
-static uint8_t can_hvac_values[8] = {
- 21, // LeftTemperature
- 21, // RightTemperature
- 21, // AverageTemperature
- 240, // Don't know why 240 but it was 0xF0 in the original amb hvacplugin
- 0, // FanSpeed
- 1, // Don't know why 1 but it was 0x01 in the original amb hvacplugin
- 0, // Don't know why 0 but it was 0x00 in the original amb hvacplugin
- 0 // Don't know why 0 but it was 0x00 in the original amb hvacplugin
+static struct {
+ const char *name;
+ uint8_t value;
+} hvac_values[] = {
+ { "LeftTemperature", 21 },
+ { "RightTemperature", 21 },
+ { "Temperature", 21 },
+ { "FanSpeed", 0 }
};
struct can_handler {
@@ -121,7 +108,7 @@ static int open_can_dev()
}
// Get original get temperature function from cpp hvacplugin code
-static uint8_t get_temperature(uint8_t value)
+static uint8_t to_can_temp(uint8_t value)
{
int result = ((0xF0 - 0x10) / 15) * value - 16;
if (result < 0x10)
@@ -132,6 +119,26 @@ static uint8_t get_temperature(uint8_t value)
return (uint8_t)result;
}
+static uint8_t read_temp_left_zone()
+{
+ return hvac_values[0].value;
+}
+
+static uint8_t read_temp_right_zone()
+{
+ return hvac_values[1].value;
+}
+
+static uint8_t read_temp()
+{
+ return (uint8_t)(((int)read_temp_left_zone() + (int)read_temp_right_zone()) >> 1);
+}
+
+static uint8_t read_fanspeed()
+{
+ return hvac_values[3].value;
+}
+
static int write_can()
{
struct can_frame txCanFrame;
@@ -143,14 +150,14 @@ static int write_can()
// Hardcoded can_id and dlc (data lenght code)
txCanFrame.can_id = 0x30;
txCanFrame.can_dlc = 8;
- txCanFrame.data[0] = get_temperature(can_hvac_values[0]);
- txCanFrame.data[1] = get_temperature(can_hvac_values[1]);
- txCanFrame.data[2] = get_temperature((can_hvac_values[0] + can_hvac_values[1]) / 2);
- txCanFrame.data[3] = can_hvac_values[3];
- txCanFrame.data[4] = can_hvac_values[4];
- txCanFrame.data[5] = can_hvac_values[5];
- txCanFrame.data[6] = can_hvac_values[6];
- txCanFrame.data[7] = can_hvac_values[7];
+ txCanFrame.data[0] = to_can_temp(read_temp_left_zone());
+ txCanFrame.data[1] = to_can_temp(read_temp_right_zone());
+ txCanFrame.data[2] = to_can_temp(read_temp());
+ txCanFrame.data[3] = 0xf0;
+ txCanFrame.data[4] = read_fanspeed();
+ txCanFrame.data[5] = 1;
+ txCanFrame.data[6] = 0;
+ txCanFrame.data[7] = 0;
#if defined(SIMULATE_HVAC)
DEBUG(interface, "WRITING CAN: %d %d [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
@@ -173,21 +180,6 @@ static int write_can()
return rc;
}
-static uint8_t read_temp_left_zone()
-{
- return can_hvac_values[0];
-}
-
-static uint8_t read_temp_right_zone()
-{
- return can_hvac_values[1];
-}
-
-static uint8_t read_fanspeed()
-{
- return can_hvac_values[4];
-}
-
/*****************************************************************************************/
/*****************************************************************************************/
/** **/
@@ -276,31 +268,35 @@ static void get(struct afb_req request)
*/
static void set(struct afb_req request)
{
- int i, rc, x;
+ int i, rc, x, changed;
struct json_object *query, *val;
- uint8_t values[sizeof can_hvac_components / sizeof *can_hvac_components];
- uint8_t saves[sizeof can_hvac_components / sizeof *can_hvac_components];
+ uint8_t values[sizeof hvac_values / sizeof *hvac_values];
+ uint8_t saves[sizeof hvac_values / sizeof *hvac_values];
/* records initial values */
DEBUG(interface, "Records initial values");
- memcpy(values, can_hvac_values, sizeof values);
- memcpy(saves, can_hvac_values, sizeof saves);
+ i = (int)(sizeof hvac_values / sizeof *hvac_values);
+ while (i) {
+ i--;
+ values[i] = saves[i] = hvac_values[i].value;
+ }
/* Loop getting arguments */
query = afb_req_json(request);
- i = (int)(sizeof can_hvac_components / sizeof *can_hvac_components);
+ changed = 0;
+ i = (int)(sizeof hvac_values / sizeof *hvac_values);
DEBUG(interface, "Looping for args. i: %d", i);
while (i)
{
i--;
- DEBUG(interface, "Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, can_hvac_components[i]);
- if (can_hvac_components[i] != NULL && json_object_object_get_ex(query, can_hvac_components[i], &val))
+ DEBUG(interface, "Searching... query: %s, i: %d, comp: %s", json_object_to_json_string(query), i, hvac_values[i].name);
+ if (json_object_object_get_ex(query, hvac_values[i].name, &val))
{
DEBUG(interface, "We got it. Tests if it is an int or not.");
if (!json_object_is_type(val, json_type_int))
{
afb_req_fail_f(request, "bad-request",
- "argument '%s' isn't integer", can_hvac_components[i]);
+ "argument '%s' isn't integer", hvac_values[i].name);
return;
}
DEBUG(interface, "We get an 'int'. Hail for the int: %d", x);
@@ -308,25 +304,36 @@ static void set(struct afb_req request)
if (x < 0 || x > 255)
{
afb_req_fail_f(request, "bad-request",
- "argument '%s' is out of bounds", can_hvac_components[i]);
+ "argument '%s' is out of bounds", hvac_values[i].name);
return;
}
- values[i] = (uint8_t)x;
+ if (values[i] != x) {
+ values[i] = (uint8_t)x;
+ changed = 1;
+ }
}
DEBUG(interface, "Not found !");
}
/* attemps to set new values */
- DEBUG(interface, "Diff: %d", memcmp(can_hvac_values, values, sizeof values));
- if (memcmp(can_hvac_values, values, sizeof values) != 0)
+ DEBUG(interface, "Diff: %d", changed);
+ if (changed)
{
- memcpy(can_hvac_values, values, sizeof values);
+ i = (int)(sizeof hvac_values / sizeof *hvac_values);
+ while (i) {
+ i--;
+ hvac_values[i].value = values[i];
+ }
rc = write_can();
if (rc >= 0)
afb_req_success(request, NULL, NULL);
else {
/* restore initial values */
- memcpy(can_hvac_values, saves, sizeof saves);
+ i = (int)(sizeof hvac_values / sizeof *hvac_values);
+ while (i) {
+ i--;
+ hvac_values[i].value = saves[i];
+ }
afb_req_fail(request, "error", "CAN error");
}
}