static char *maybe_get_subst (char *name, int *n, int quoted, int *freeit) { char *s = NULL; int k = *n - 1; char *ret = NULL; while (k >= 0) { s = (char *) get_string_by_name(name); if (s != NULL) { *n = k + 1; ret = s; break; } name[k--] = '\0'; } if (ret != NULL) { if (quoted && strchr(ret, '\\')) { ret = mod_strdup(ret); *freeit = 1; } } return ret; }
static void look_up_string_variable (const char *s, parser *p) { char *val = get_string_by_name(s + 1); if (val != NULL) { p->idstr = gretl_strdup(s + 1); if (p->idstr == NULL) { p->err = E_ALLOC; } else { p->uval = val; p->sym = USTR; } } else { undefined_symbol_error(s, p); } }
static int maybe_delete_bundle_value (const char *s, PRN *prn) { char bname[VNAMELEN]; char key[VNAMELEN]; char fmt[16]; int brackets = 0; int err = 0; if (strchr(s, '[')) { sprintf(fmt, "%%%d[^[][%%%d[^]]", VNAMELEN-1, VNAMELEN-1); brackets = 1; } else { sprintf(fmt, "%%%d[^.].%%%ds", VNAMELEN-1, VNAMELEN-1); } if (sscanf(s, fmt, bname, key) == 2) { gretl_bundle *bundle; const char *s; bundle = get_bundle_by_name(bname); if (bundle == NULL) { err = E_UNKVAR; } else if (brackets) { if (*key == '"') { s = gretl_unquote(key, &err); } else if (gretl_is_string(key)) { s = get_string_by_name(key); } else { err = E_UNKVAR; } } else { s = key; } if (!err) { err = gretl_bundle_delete_data(bundle, s); } } else { err = E_UNKVAR; } return err; }
/* * Get datapoint data according to datapoint properties. * Here we fake the data using random numbers. */ void * get_datapoint_data(void *props) { float temperature = 0.0; void *ret = NULL; const char *name = get_string_by_name(props, "name"); ret = malloc(sizeof(double)); if (ret == NULL) { perror("No memory available.\n"); exit(-1); } if (strcmp(name, "grove_temperature") == 0) { /* the temperature sensor's output connect to a0 pin of galileo */ int a0v = galileo_analog_read(0); /* the value of a0v should within [0,4096], that use 12bit to * moderize 0~5v voltage, which means 0 stands for 0v while * 4096 stands for 5v. */ printf("Readed a0 pin voltage: %1.2f\n", ((double)a0v * 5) / 4096); /* then next we'll need to calculate temperature based on * the design of temperature sensor. */ int val = a0v / 4; int B = 3975; float resistance; if (val != 0) { resistance = (float)(1023 - val) * 10000 / val; temperature = 1 / (log(resistance / 10000) / B + 1 / 298.15) - 273.15; } printf("The temperature is: %2.2f c\n", temperature); /* return the temperature to libsensor */ *(double *)ret = (double)temperature; } else if (strcmp(name, "grove_light") == 0) { int a1v = galileo_analog_read(1); printf("Readed a1 pin voltage: %1.2f\n", ((double)a1v * 5) / 4096); int val = a1v / 4; printf("The light number is: %2.2f\n", (double)val); *(double *)ret = (double)val; } else if (strcmp(name, "grove_sound") == 0) { int a2v = galileo_analog_read(2); printf("Readed a2 pin voltage: %1.2f\n", ((double)a2v * 5) / 4096); int val = a2v / 4; printf("The sound number is: %2.2f\n", (double)val); *(double *)ret = (double)val; } else if (strcmp(name, "lm35-temperature") == 0) { int a0v = galileo_analog_read(0); /* get voltage */ printf("Readed a0 pin voltage: %1.2f\n", ((double)a0v * 5) / 4096); double val = a0v / 4; /* the lm35 output voltage is 10mV per degree, from 0 to 100 C */ double temperature = (val * 5 / 1024) * 100.00; printf("The lm35 temperature is: %2.2f C\n", temperature); *(double *)ret = temperature; } else if (strcmp(name, "oc_image") == 0) { struct timeb t; ftime(&t); /* prepare a image file then return its name to libsensor */ char *file = NULL; char *cmd = NULL; /* note, according to asprintf, the 'file' need be freed later, which will be done by libsensor */ asprintf(&file, "image_%lld%s", 1000 * (long long)t.time + t.millitm, ".jpg"); asprintf(&cmd, "capture %s 2>/dev/null", file); system(cmd); free(cmd); cmd = NULL; ret = (void*)file; } else if (strcmp(name, "image") == 0) { struct timeb t; ftime(&t); /* prepare a image file then return its name to libsensor */ char *file = NULL; char *cmd = NULL; /* note, according to asprintf, the 'file' need be freed later, which will be done by libsensor */ asprintf(&file, "image_%lld%s", 1000 * (long long)t.time + t.millitm, ".jpg"); asprintf(&cmd, "fswebcam -r 1280x720 --save %s 2>/dev/null", file); system(cmd); free(cmd); cmd = NULL; ret = (void*)file; } else if (strcmp(name, "serial_test") == 0) { unsigned char buf[32] = {}; int fd = init_serial_port(SERISL_DEVICE); if (fd < 0) { *(double *)ret = 0; } else { int len = do_read_from_serial(buf, fd); dump_str(buf, len); *(double *)ret = 1; } clean_serial_port(fd); } return ret; }
int do_modprint (const char *line, gretlopt opt, PRN *prn) { gretl_matrix *coef_se = NULL; gretl_matrix *addstats = NULL; char *parnames = NULL; char *litstr = NULL; const char *s; char name[VNAMELEN]; int err = 0; err = incompatible_options(opt, OPT_C | OPT_R | OPT_T); if (err) { return err; } /* skip the command word, if present */ s = line; s += strspn(s, " "); if (!strncmp(s, "modprint ", 9)) { s += 9; } /* first up, name of k x 2 matrix */ if (gretl_scan_varname(s, name) == 1) { coef_se = get_matrix_by_name(name); if (coef_se == NULL) { err = E_UNKVAR; } else if (gretl_matrix_cols(coef_se) != 2) { gretl_errmsg_set(_("modprint: the first matrix argument must have 2 columns")); err = E_DATA; } } else { err = E_PARSE; } if (!err) { /* second up, string containing names */ s += strspn(s, " "); s += strlen(name); s += strspn(s, " "); if (*s == '"') { /* got a string literal */ litstr = gretl_quoted_string_strdup(s, &s); if (litstr == NULL) { err = E_PARSE; } else { parnames = litstr; s += strspn(s, " "); } } else if (gretl_scan_varname(s, name) == 1) { parnames = (char *) get_string_by_name(name); if (parnames == NULL) { err = E_UNKVAR; } else { /* advance past string name */ s += strspn(s, " "); s += strlen(name); s += strspn(s, " "); } } else { err = E_PARSE; } } if (!err) { /* optional third field: extra matrix */ if (*s != '\0') { gretl_scan_varname(s, name); addstats = get_matrix_by_name(name); if (addstats == NULL) { err = E_UNKVAR; } } } if (!err) { PrnFormat fmt = GRETL_FORMAT_TXT; if (opt & OPT_C) { fmt = GRETL_FORMAT_CSV; } else if (opt & OPT_R) { fmt = GRETL_FORMAT_RTF; } else if (opt & OPT_T) { fmt = GRETL_FORMAT_TEX; if (opt & OPT_O) { fmt |= GRETL_FORMAT_DOC; } } gretl_print_set_format(prn, fmt); err = print_model_from_matrices(coef_se, addstats, parnames, prn); } free(litstr); return err; }