From e78e1109cd29040ae57ff25dff897801c7026e25 Mon Sep 17 00:00:00 2001 From: heck Date: Sun, 24 Jan 2021 01:12:31 +0100 Subject: [PATCH] lib_synth_shed - Add **arg, struct field[3] --- examples/lib/lib_synth_shed/main.c | 10 +- examples/lib/lib_synth_shed/synth_shed.c | 122 +++++++++++++++++------ examples/lib/lib_synth_shed/synth_shed.h | 18 ++-- 3 files changed, 109 insertions(+), 41 deletions(-) diff --git a/examples/lib/lib_synth_shed/main.c b/examples/lib/lib_synth_shed/main.c index 3036031..bb24aa3 100644 --- a/examples/lib/lib_synth_shed/main.c +++ b/examples/lib/lib_synth_shed/main.c @@ -11,15 +11,17 @@ int main() { // synth_t* newsynth = NULL; // synth_init(newsynth); - synth_t* my_synth = synth_create("Korg", "MS20", 2); + synth_t *my_synth = NULL; + SYNTH_STATUS status = synth_create(&my_synth,"Korg", "MS20", 2); + if(status != SYNTH_STATUS_OK) { + printf("%s", status_to_string(status)); + } printf("brand %s\n", my_synth->brand); printf("model %s\n", my_synth->model); printf("%s", play_synth(my_synth)); - synth_set_osc_count(my_synth, 33); + synth_set_osc_count(my_synth, 34); printf("%s", play_synth(my_synth)); - - return 0; } diff --git a/examples/lib/lib_synth_shed/synth_shed.c b/examples/lib/lib_synth_shed/synth_shed.c index 7c058d6..1fa5760 100644 --- a/examples/lib/lib_synth_shed/synth_shed.c +++ b/examples/lib/lib_synth_shed/synth_shed.c @@ -8,22 +8,43 @@ void init_synth_shed() { printf("init_synth_shed() - called\n"); } -synth_t* synth_create(const char* brand, const char* model, int osc_count) { - printf("%s::%s brand:'%s, model: %s'\n",__FILE__, __FUNCTION__, brand, model); - synth_t* new_synth = (synth_t*) malloc(sizeof(synth_t)); - assert(new_synth); - if (new_synth != NULL) { - new_synth->brand = brand; - new_synth->model = model; - new_synth->osc_count = osc_count; - new_synth->technology = ANALOG; - new_synth->filter.technology = ANALOG; - new_synth->filter.type = LPF; +SYNTH_STATUS synth_create(synth_t **new_synth, const char *brand, const char *model, int osc_count) { + assert(brand); + assert(model); + + if (brand == NULL || brand[0] == '\0') { + return SYNTH_STATUS_ARG_ERR; + } + + if (model == NULL || brand[0] == '\0') { + return SYNTH_STATUS_ARG_ERR; + } + + printf("%s::%s brand:'%s, model: %s'\n", __FILE__, __FUNCTION__, brand, model); + + synth_t *_new_synth = (synth_t *) malloc(sizeof(synth_t)); + assert(_new_synth); + if (_new_synth == NULL) { + return SYNTH_STATUS_ERR; } - return new_synth; + + _new_synth->brand = brand; + _new_synth->model = model; + _new_synth->osc_count = osc_count; + _new_synth->technology = ANALOG; + _new_synth->filter[0].technology = ANALOG; + _new_synth->filter[0].type = LPF; + _new_synth->filter[1].technology = ANALOG; + _new_synth->filter[1].type = HPF; + _new_synth->filter[2].technology = ANALOG; + _new_synth->filter[2].type = BPF; + + *new_synth = _new_synth; + + return SYNTH_STATUS_OK; } -SYNTH_STATUS synth_set_osc_count(synth_t* synth, int osc_count) { +SYNTH_STATUS synth_set_osc_count(synth_t *synth, int osc_count) { assert(synth); SYNTH_STATUS status = 0; if (osc_count < 0) { @@ -39,9 +60,23 @@ SYNTH_STATUS synth_set_osc_count(synth_t* synth, int osc_count) { //SYNTH_STATUS synth_set_tech(synth_t* synth, tech_t tech); -const char* tech_to_string(const enum _tech* tech) { +const char *status_to_string(SYNTH_STATUS status) { + const char *ret = "unknown status"; + if (status == SYNTH_STATUS_OK) { + ret = "SYNTH_STATUS_OK"; + } + if (status == SYNTH_STATUS_ERR) { + ret = "SYNTH_STATUS_ERR"; + } + if (status == SYNTH_STATUS_ARG_ERR) { + ret = "SYNTH_STATUS_ARG_ERR"; + } + return ret; +} + +const char *tech_to_string(const enum _tech *tech) { assert(tech); - const char* ret = "unknown tech"; + const char *ret = "unknown tech"; if (*tech == ANALOG) { return "ANALOG"; } @@ -52,36 +87,65 @@ const char* tech_to_string(const enum _tech* tech) { } -const char* filtertype_to_string(const filtertype_t* const filt) { +SYNTH_STATUS filtertype_to_string(const filtertype_t *const filt, char **ftstr) { assert(filt); - const char* ret = "unknown filtertype"; + char *_ftstr = "unknown filtertype"; + if (filt == NULL) { + return SYNTH_STATUS_ARG_ERR; + } + if (*filt == LPF) { - ret = "LPF"; + _ftstr = "LPF"; } if (*filt == HPF) { - ret = "HPF"; + _ftstr = "HPF"; } if (*filt == BPF) { - ret = "BPF"; + _ftstr = "BPF"; } - return ret; + + *ftstr = _ftstr; + return SYNTH_STATUS_OK; } -const char* play_synth(synth_t* synth) { +const char *play_synth(synth_t *synth) { assert(synth); char ret[9999]; - const char* greet = "Playing synth:"; - const char* brand = synth->brand; - const char* model = synth->model; + const char *greet = "PLAYING SYNTH:"; + const char *brand = synth->brand; + const char *model = synth->model; char osc[4]; sprintf(osc, "%i", synth->osc_count); - const char* tech = tech_to_string(&synth->technology); - const char* filt_tech = tech_to_string(&synth->filter.technology); - const char* filt_type = filtertype_to_string(&synth->filter.type); + const char *tech = tech_to_string(&synth->technology); + + + const char *filt_tech[3] = {"", "", ""}; + const char *filtertype_str[3] = {"", "", ""}; + + for (int i = 0; i < 3; i++) { + filt_tech[i] = tech_to_string(&synth->filter[i].technology); + + SYNTH_STATUS status = filtertype_to_string(&synth->filter[i].type, &filtertype_str[i]); + if (status != SYNTH_STATUS_OK) { + printf("%s", status_to_string(status)); + } + } - sprintf(ret, "%s\nbrand: %s\nmodel: %s\nosc: %s\ntech:%s\nfilt:%s / %s\n\n", greet, brand,model, osc, tech, filt_tech, filt_type); + sprintf(ret, "%s\n" + "brand: %s\n" + "model: %s\n" + "osc: %s\n" + "tech:%s\n" + "filt[0]:%s / %s\n" + "filt[1]:%s / %s\n" + "filt[2]:%s / %s\n" + "\n", + greet, brand, model, osc, tech, + filt_tech[0], filtertype_str[0], + filt_tech[1], filtertype_str[1], + filt_tech[2], filtertype_str[2]); return strdup(ret); } diff --git a/examples/lib/lib_synth_shed/synth_shed.h b/examples/lib/lib_synth_shed/synth_shed.h index fe432cb..1033bd5 100644 --- a/examples/lib/lib_synth_shed/synth_shed.h +++ b/examples/lib/lib_synth_shed/synth_shed.h @@ -7,12 +7,16 @@ extern "C" { // Types (as forward declarations) -// != 0 means ERR -typedef int SYNTH_STATUS; typedef struct _synth synth_t; typedef synth_t synth; // Enums +typedef enum _SYNTH_STATUS { + SYNTH_STATUS_OK, + SYNTH_STATUS_ERR, + SYNTH_STATUS_ARG_ERR +} SYNTH_STATUS; + typedef enum _filtertype { HPF, LPF, @@ -42,10 +46,7 @@ struct _synth { // Functions void init_synth_shed(); -synth_t* synth_create(const char* brand, const char* model, int osc_count); - -// This function covers the problem of a pointer to pointer arg -SYNTH_STATUS synth_create_pp(synth_t** new_synth); +SYNTH_STATUS synth_create(synth_t **new_synth, const char *brand, const char *model, int osc_count); SYNTH_STATUS synth_set_osc_count(synth_t* synth, int osc_count); @@ -53,10 +54,11 @@ SYNTH_STATUS synth_set_osc_count(synth_t* synth, int osc_count); //SYNTH_STATUS synth_set_tech(synth_t* synth, tech_t tech); -const char* tech_to_string(const enum _tech* tech); +const char* status_to_string(SYNTH_STATUS status); -const char* filtertype_to_string(const filtertype_t* filt); +const char* tech_to_string(const enum _tech* tech); +SYNTH_STATUS filtertype_to_string(const filtertype_t* const filt, char** ftstr); const char* play_synth(synth_t* synth);