introduce setting_param_new, setting_param_new_from_data and setting_param_free

This commit is contained in:
Tomasz Moń
2009-05-03 21:38:15 +02:00
parent 7bf55352b8
commit acb1c1e273
4 changed files with 100 additions and 34 deletions

86
gdigi.c
View File

@@ -403,6 +403,92 @@ void append_value(GString *msg, guint value)
} }
} }
/**
* \param str pointer to value to unpack
* \param len return location for how many bytes value is encoded on (length is added to current value)
*
* Unpacks value using scheme used on all newer DigiTech products.
*
* \return unpacked value
**/
guint unpack_value(gchar *str, int *len)
{
guint value;
gint tmp;
value = (unsigned char)str[0];
*len += 1;
if (value > 0x80) {
tmp = value & 0x7F;
value = 0;
gint i;
for (i = 0; i<tmp; i++) {
value |= ((unsigned char)str[1+i] << (8*(tmp-i-1)));
}
*len += tmp;
}
return value;
}
/**
* Allocates memory for SettingParam.
*
* \return SettingParam which must be freed using setting_param_free.
**/
SettingParam *setting_param_new()
{
SettingParam *param = g_slice_new(SettingParam);
param->id = -1;
param->position = -1;
param->value = -1;
return param;
}
/**
* \param str pointer to setting param in message
* \param len return location for how many bytes value is encoded on (length is added to current value)
*
* Creates SettingParam basing on data.
* This function expects str to point on:
* -Parameter ID - 2 bytes
* -Parameter position - 1 byte
* -Parameter value - var
*
* \return newly created SettingParam which must be freed using setting_param_free.
**/
SettingParam *setting_param_new_from_data(gchar *str, gint *len)
{
gint id;
gint position;
guint value;
id = ((unsigned char)str[0] << 8) | (unsigned char)str[1];
position = (unsigned char)str[2];
*len += 3;
value = unpack_value(&str[3], len);
SettingParam *param = g_slice_new(SettingParam);
param->id = id;
param->position = position;
param->value = value;
return param;
}
/**
* \param param SettingParam to be freed
*
* Frees all memory used by SettingParam.
**/
void setting_param_free(SettingParam *param)
{
g_slice_free(SettingParam, param);
}
/** /**
* \param id Parameter ID * \param id Parameter ID
* \param position Parameter position * \param position Parameter position

View File

@@ -682,9 +682,18 @@ typedef enum {
NACK = 0x7F NACK = 0x7F
} MessageID; } MessageID;
typedef struct {
int id;
int position;
int value;
} SettingParam;
void send_message(gint procedure, gchar *data, gint len); void send_message(gint procedure, gchar *data, gint len);
void append_value(GString *msg, guint value); void append_value(GString *msg, guint value);
GString *get_message_by_id(MessageID id); GString *get_message_by_id(MessageID id);
SettingParam *setting_param_new();
SettingParam *setting_param_new_from_data(gchar *str, gint *len);
void setting_param_free(SettingParam *param);
void set_option(guint id, guint position, guint value); void set_option(guint id, guint position, guint value);
void switch_preset(guint bank, guint x); void switch_preset(guint bank, guint x);
void store_preset_name(int x, const gchar *name); void store_preset_name(int x, const gchar *name);

View File

@@ -18,6 +18,7 @@
#include <expat.h> #include <expat.h>
#include <string.h> #include <string.h>
#include "preset.h" #include "preset.h"
#include "gdigi.h"
#ifndef DOXYGEN_SHOULD_SKIP_THIS #ifndef DOXYGEN_SHOULD_SKIP_THIS
@@ -53,10 +54,7 @@ static void XMLCALL start(void *data, const char *el, const char **attr) {
if (ad->preset->params != NULL) if (ad->preset->params != NULL)
g_message("Params aleady exists!"); g_message("Params aleady exists!");
} else if (g_strcmp0(el, "Param") == 0) { } else if (g_strcmp0(el, "Param") == 0) {
SettingParam *param = g_slice_new(SettingParam); SettingParam *param = setting_param_new();
param->id = -1;
param->position = -1;
param->value = -1;
ad->preset->params = g_list_prepend(ad->preset->params, param); ad->preset->params = g_list_prepend(ad->preset->params, param);
} else if (g_strcmp0(el, "ID") == 0) { } else if (g_strcmp0(el, "ID") == 0) {
ad->id = PARSER_TYPE_PARAM_ID; ad->id = PARSER_TYPE_PARAM_ID;
@@ -185,11 +183,7 @@ Preset *create_preset_from_data(GString *data)
{ {
gint total; gint total;
gint n; gint n;
gint id;
gint position;
guint value;
gint x; gint x;
gint tmp;
g_return_val_if_fail(data != NULL, NULL); g_return_val_if_fail(data != NULL, NULL);
@@ -203,27 +197,10 @@ Preset *create_preset_from_data(GString *data)
preset->params = NULL; preset->params = NULL;
do { do {
id = ((unsigned char)data->str[x] << 8) | (unsigned char)data->str[x+1]; SettingParam *param = setting_param_new_from_data(&data->str[x], &x);
position = (unsigned char)data->str[x+2];
x+=3;
value = data->str[x];
x++;
if (value > 0x80) {
tmp = value & 0x7F;
value = 0;
gint i;
for (i=0; i<tmp; i++) {
value |= ((unsigned char)data->str[x+i] << (8*(tmp-i-1)));
}
x+=tmp;
}
n++; n++;
SettingParam *param = g_slice_new(SettingParam);
param->id = id;
param->position = position;
param->value = value;
preset->params = g_list_prepend(preset->params, param); preset->params = g_list_prepend(preset->params, param);
g_message("%d ID %d Position %d Value %d", n, id, position, value); g_message("%d ID %d Position %d Value %d", n, param->id, param->position, param->value);
} while ((x < data->len) && n<total); } while ((x < data->len) && n<total);
g_message("TOTAL %d", total); g_message("TOTAL %d", total);
preset->params = g_list_reverse(preset->params); preset->params = g_list_reverse(preset->params);
@@ -243,7 +220,7 @@ void preset_free(Preset *preset)
if (preset->params != NULL) { if (preset->params != NULL) {
GList *iter; GList *iter;
for (iter = preset->params; iter; iter = iter->next) { for (iter = preset->params; iter; iter = iter->next) {
g_slice_free(SettingParam, iter->data); setting_param_free((SettingParam*)iter->data);
} }
g_list_free(preset->params); g_list_free(preset->params);
} }

View File

@@ -19,12 +19,6 @@
#include <glib.h> #include <glib.h>
typedef struct {
int id;
int position;
int value;
} SettingParam;
typedef struct { typedef struct {
gchar *name; gchar *name;
GList *params; GList *params;