make set_option wrapper to send_message
This commit is contained in:
99
gdigi.c
99
gdigi.c
@@ -240,6 +240,33 @@ static gint get_message_id(GString *msg)
|
||||
return -1;
|
||||
}
|
||||
|
||||
void append_value(GString *msg, guint value)
|
||||
{
|
||||
/* check how many bytes long the value is */
|
||||
guint temp = value;
|
||||
gint n = 0;
|
||||
do {
|
||||
n++;
|
||||
temp = temp >> 8;
|
||||
} while (temp);
|
||||
|
||||
if (n == 1) {
|
||||
if (value & 0x80)
|
||||
n = 2;
|
||||
else
|
||||
g_string_append_printf(msg, "%c", value);
|
||||
}
|
||||
|
||||
if (n > 1) {
|
||||
gint x;
|
||||
g_string_append_printf(msg, "%c", (n | 0x80));
|
||||
for (x=0; x<n; x++) {
|
||||
g_string_append_printf(msg, "%c",
|
||||
((value >> (8*(n-x-1))) & 0xFF));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
id - ID as found in preset file
|
||||
position - Position as found in preset file
|
||||
@@ -247,71 +274,13 @@ static gint get_message_id(GString *msg)
|
||||
*/
|
||||
void set_option(guint id, guint position, guint value)
|
||||
{
|
||||
static char option[] = {0xF0, 0x00, 0x00, 0x10, 0x00, 0x5E, 0x02, 0x41,
|
||||
0x00, 0x00, 0x00, /* ID */
|
||||
0x00, /* position */
|
||||
0x00, /* value length (not necesarry) */
|
||||
0x00, 0x00, 0x00, /* value (can be either 1, 2 or 3 bytes) */
|
||||
0x00, /* checksum */ 0xF7};
|
||||
|
||||
option[8] = ((id & 0x80) >> 2);
|
||||
|
||||
option[9] = ((id & 0xFF00) >> 8);
|
||||
option[10] = ((id & 0x007F));
|
||||
|
||||
option[11] = position;
|
||||
|
||||
if (value < 0x80) {
|
||||
// [12] = value, [13] - checksum, [14] = 0xF7
|
||||
option[12] = value;
|
||||
|
||||
option[14] = 0xF7;
|
||||
option[13] = calculate_checksum(option, 15, 13);
|
||||
|
||||
send_data(option, 15);
|
||||
} else if (value <= 0xFF) {
|
||||
option[8] |= 0x08; // there'll be length before value
|
||||
if (((value & 0x80) >> 7) == 1)
|
||||
option[8] |= 0x04;
|
||||
|
||||
option[12] = 0x01;
|
||||
// [13] = value, [14] - checksum, [15] = 0xF7
|
||||
option[13] = (value & 0x007F);
|
||||
|
||||
option[15] = 0xF7;
|
||||
option[14] = calculate_checksum(option, 16, 14);
|
||||
|
||||
send_data(option, 16);
|
||||
} else if (value <= 0xFFFF) {
|
||||
option[8] |= 0x08; // there'll be length before value
|
||||
if (((value & 0x80) >> 7) == 1)
|
||||
option[8] |= 0x02;
|
||||
|
||||
option[12] = 0x02;
|
||||
|
||||
option[13] = ((value & 0xFF00) >> 8);
|
||||
option[14] = ((value & 0x007F));
|
||||
|
||||
option[16] = 0xF7;
|
||||
option[15] = calculate_checksum(option, 17, 15);
|
||||
|
||||
send_data(option, 17);
|
||||
} else if (value <= 0xFFFFFF) {
|
||||
option[8] |= 0x08; // there'll be length before value
|
||||
if (((value & 0x80) >> 7) == 1)
|
||||
option[8] |= 0x01;
|
||||
|
||||
option[12] = 0x03;
|
||||
|
||||
option[13] = ((value & 0xFF0000) >> 16);
|
||||
option[14] = ((value & 0x00FF00) >> 8);
|
||||
option[15] = ((value & 0x00007F));
|
||||
|
||||
option[17] = 0xF7;
|
||||
option[16] = calculate_checksum(option, 18, 16);
|
||||
|
||||
send_data(option, 18);
|
||||
}
|
||||
GString *msg = g_string_sized_new(9);
|
||||
g_string_append_printf(msg, "%c%c%c",
|
||||
((id & 0xFF00) >> 8), (id & 0xFF),
|
||||
position);
|
||||
append_value(msg, value);
|
||||
send_message(RECEIVE_PARAMETER_VALUE, msg->str, msg->len);
|
||||
g_string_free(msg, TRUE);
|
||||
}
|
||||
|
||||
/* x = 0 to 60 */
|
||||
|
||||
Reference in New Issue
Block a user