Changeset 12


Ignore:
Timestamp:
Jul 31, 2019, 5:44:55 PM (5 years ago)
Author:
bouyer
Message:

Host software for the v2 firmware

Location:
trunk/software/tty
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/software/tty/readtty_power.c

    r1 r12  
    22#include <stdlib.h>
    33#include <unistd.h>
     4#include <string.h>
    45#include <errno.h>
     6#include <err.h>
    57#include <fcntl.h>
    68#include <signal.h>
    79#include <termios.h>
    810#include <sys/ioctl.h>
     11#include <sys/select.h>
    912
    1013struct scale {
     
    1316};
    1417
    15 const struct scale *current_scale = NULL;
    16 const struct scale *voltage_scale = NULL;
     18struct scale current_scale;
     19struct scale voltage_scale;
    1720float current_r = 0;
    1821
    19 const struct scale current_scale_1 = {3851, 0.5367};
    20 const float current_r_1 = 0.2;
    21 const struct scale current_scale_2 = {3651, 0.9761};
    22 const float current_r_2 = 0.1;
    23 // const float current_r_3 = 0.05;
    24 const struct scale voltage_scale_1 = {4041.5, 5.996};
    25 const struct scale voltage_scale_2 = {4063.5, 13.547};
    26 const struct scale voltage_scale_3 = {4038, 23.672};
     22const float current_ra[] = {0.2, 0.1, 0.05, 0.02};
    2723
    2824volatile int quit;
     
    3834usage()
    3935{
    40         errx(1, "usage: %s -c [1|2] -v [1|2|3] <device>", prog);
    41 }
     36        errx(1, "usage: %s -c[1|2|3|4] -v[1|2|3] [-n] <device>", prog);
     37}
     38
     39static void
     40get_cal_data(const char *b, int ci, int vi)
     41{
     42        int cr[4], vr[3];
     43        float ca[4], vv[3];
     44        int ret;
     45
     46        ret = sscanf(b, "cal_data %d %f %d %f %d %f %d %f %d %f %d %f %d %f",
     47            &cr[0], &ca[0], &cr[1], &ca[1], &cr[2], &ca[2], &cr[3], &ca[3],
     48            &vr[0], &vv[0], &vr[1], &vv[1], &vr[2], &vv[2]);
     49        if (ret != 14) {
     50                errx(1, "cal_data: converted only %d\n", ret);
     51        }
     52        for (int i = 0; i < 4; i++) {
     53                printf("cr %d ca %f\n", cr[i], ca[i]);
     54        }
     55        for (int i = 0; i < 3; i++) {
     56                printf("vr %d vv %f\n", vr[i], vv[i]);
     57        }
     58        current_scale.read = cr[ci - 1];
     59        current_scale.result = ca[ci - 1];
     60        voltage_scale.read = vr[vi - 1];
     61        voltage_scale.result = vv[vi - 1];
     62}
     63
     64static void
     65readline(int d, char *buf, int l)
     66{
     67        int i;
     68        int bytes;
     69        do {
     70                for (i = 0; i < l; i++) {
     71                        do {
     72                                bytes = read(d, &buf[i], 1);
     73                                if (quit)
     74                                        return;
     75                        } while ((bytes < 0 && errno == EAGAIN) || buf[i] == '\r');
     76                        if (bytes != 1) {
     77                                fprintf(stderr, "readline: read %d\n", bytes);
     78                                err(1, "read line");
     79                        }
     80                        if (buf[i] == '\n')
     81                                break;
     82                }
     83        } while (i == 0);
     84        if (buf[i] != '\n') {
     85                warnx("realine: end of line not found");
     86        }
     87        buf[i] = '\0';
     88}
     89
    4290int
    4391main(int argc, char * const argv[])
     
    4694        int bytes;
    4795        int nfds;
     96        int lineno = 0;
     97        int nopt = 0;
    4898        char buf[256];
    4999        int i;
    50100        fd_set rfds;
    51         char gpio[2], current[4], tension[4];
     101        char gpio[3], current[4], tension[4];
    52102        char *endp;
    53         int currenti, tensioni;
     103        int gpioi, currenti, tensioni;
    54104        float current_a, tension_v;
    55         int val;
     105        int val, gpio_val;
     106        int c_opt = 0, v_opt = 0;
    56107        extern char *optarg;
    57108        extern int optind;
     
    69120        prog = argv[0];
    70121        state = INIT;
    71         currenti = tensioni = 0;
    72 
    73         while ((ch = getopt(argc, argv, "c:v:")) != -1) {
     122        gpioi = currenti = tensioni = 0;
     123
     124        while ((ch = getopt(argc, argv, "np:c:v:")) != -1) {
    74125                switch (ch) {
     126                case 'n':
     127                        nopt = 1;
     128                        break;
    75129                case 'c':
    76                         val = atoi(optarg);
    77                         if (val == 1) {
    78                                 current_scale = &current_scale_1;
    79                                 current_r = current_r_1;
    80                         } else if (val == 2) {
    81                                 current_scale = &current_scale_2;
    82                                 current_r = current_r_2;
    83                         }
     130                        c_opt = atoi(optarg);
     131                        if (c_opt < 1 || c_opt > 4)
     132                                usage();
     133                        current_r = current_ra[c_opt  -1];
    84134                        break;
    85135                case 'v':
    86                         val = atoi(optarg);
    87                         if (val == 1) {
    88                                 voltage_scale = &voltage_scale_1;
    89                         } else if (val == 2) {
    90                                 voltage_scale = &voltage_scale_2;
    91                         } else if (val == 3) {
    92                                 voltage_scale = &voltage_scale_3;
    93                         }
     136                        v_opt = atoi(optarg);
     137                        if (v_opt < 1 || v_opt > 3)
     138                                usage();
    94139                        break;
    95140                default:
     
    100145        argv += optind;
    101146
    102         if (argc != 1 || current_scale == NULL || voltage_scale == NULL)
     147        if (argc != 1 || c_opt == 0 || v_opt == 0)
    103148                usage();
    104149
     
    139184        }
    140185        fprintf(stderr, "tty ready\n");
     186        do {
     187                write(d, "M0\n", 3);
     188                readline(d, buf, sizeof(buf));
     189                if (quit)
     190                        goto quit;
     191        } while (memcmp(buf, "OK", 2) != 0);
     192        write(d, "C\n", 2);
     193        readline(d, buf, sizeof(buf));
     194        if (quit)
     195                goto quit;
     196        printf("cal_data: %s\n", buf);
     197        get_cal_data(buf, c_opt, v_opt);
     198        printf("cal_data selected: %f %f, %f %f\n",
     199            current_scale.read, current_scale.result,
     200            voltage_scale.read, voltage_scale.result);
     201
     202        /* start measures */
     203        write(d, "M1\n", 3);
    141204        FD_ZERO(&rfds);
    142205        while (quit == 0) {
     
    152215                                case INIT:
    153216                                        /* look for an X mark */
    154                                         if (buf[i] == 'X')
     217                                        if (buf[i] == 'X') {
    155218                                                state = GPIO;
     219                                                gpioi = 0;
     220                                        }
    156221                                        break;
    157222                                case GPIO:
    158                                         gpio[0] = buf[i];
    159                                         gpio[1] = '\0';
    160                                         val = strtol(gpio, &endp, 16);
     223                                        gpio[gpioi] = buf[i];
     224                                        gpioi++;
     225                                        if (gpioi < 2)
     226                                                break;
     227                                        gpio[gpioi] = '\0';
     228                                        gpio_val = strtol(gpio, &endp, 16);
    161229                                        if (*endp != '\0') {
    162230                                                fprintf(stderr,
     
    167235                                                state = TENSION;
    168236                                                tensioni = 0;
    169                                                 printf("%2d ", val);
    170237                                        }
    171238                                        break;
     
    189256                                                }
    190257                                                state = MARK;
    191                                                 current_a = val / current_scale->read * current_scale->result;
    192                                                 printf("%8f ", current_a);
     258                                                current_a = val / current_scale.read * current_scale.result;
    193259                                                /* correct for in our resistor */
    194260                                                tension_v -= current_a * current_r;
    195                                                 printf("%8f ", tension_v);
    196                                                 printf("%8f\n", current_a * tension_v);
     261                                                if (nopt) {
     262                                                        printf("%8f ", (float)lineno / 5000);
     263                                                        lineno++;
     264                                                }
     265                                                printf("%3d %8f %8f %8f\n",
     266                                                    gpio_val,
     267                                                    current_a,
     268                                                    tension_v,
     269                                                    current_a * tension_v);
    197270                                        }
    198271                                        break;
     
    217290                                                state = CURRENT;
    218291                                                currenti = 0;
    219                                                 tension_v = val / voltage_scale->read * voltage_scale->result;
     292                                                tension_v = val / voltage_scale.read * voltage_scale.result;
    220293                                        }
    221294                                        break;
     
    228301                                        } else {
    229302                                                state = GPIO;
     303                                                gpioi = 0;
    230304                                        }
    231305                                        break;
     
    236310                }
    237311        }
     312quit:
     313        /* stop measures */
     314        write(d, "M0\n", 3);
     315        if (fflush(stdout) == EOF) {
     316                warn("fflush");
     317        }
    238318        if (tcsetattr(d, TCSANOW, &ot) < 0) {
    239319                err(1, "restore tcsetattr");
     
    241321        exit(0);
    242322}
    243 
    244 
  • trunk/software/tty/readtty_timer.c

    r1 r12  
    33#include <unistd.h>
    44#include <errno.h>
     5#include <err.h>
    56#include <fcntl.h>
    67#include <signal.h>
     8#include <string.h>
    79#include <termios.h>
    810#include <sys/ioctl.h>
     11#include <sys/select.h>
    912
    1013volatile int quit;
     
    1417{
    1518        quit = 1;
     19}
     20
     21static void
     22readline(int d, char *buf, int l)
     23{
     24        int i;
     25        int bytes;
     26        do {
     27                for (i = 0; i < l; i++) {
     28                        do {
     29                                bytes = read(d, &buf[i], 1);
     30                                if (quit)
     31                                        return;
     32                        } while ((bytes < 0 && errno == EAGAIN) || buf[i] == '\r');
     33                        if (bytes != 1) {
     34                                fprintf(stderr, "readline: read %d\n", bytes);
     35                                err(1, "read line");
     36                        }
     37                        if (buf[i] == '\n')
     38                                break;
     39                }
     40        } while (i == 0);
     41        if (buf[i] != '\n') {
     42                errx(1, "realine: end of line not found");
     43        }
     44        buf[i] = '\0';
    1645}
    1746
     
    2554        int i;
    2655        fd_set rfds;
    27         char gpio[2], current[4], tension[4];
     56        char gpio[3], current[4], tension[4];
    2857        char *endp;
    29         int currenti, tensioni;
    30         int val;
     58        int gpioi, currenti, tensioni;
     59        int gpioval, currentval, tensionval;
     60        int exitst = 0;
    3161
    3262        enum {
     
    4171
    4272        state = INIT;
    43         currenti = tensioni = 0;
     73        gpioi = currenti = tensioni = 0;
    4474
    4575        if (argc != 3) {
     
    87117        }
    88118        fprintf(stderr, "tty ready\n");
     119        write(d, "M0\n", 3);
     120        write(d, "M0\n", 3);
     121        do {
     122                readline(d, buf, sizeof(buf));
     123                if (quit)
     124                        goto quit;
     125        } while (memcmp(buf, "OK", 2) != 0);
     126        fprintf(stderr, "starting\n");
     127        write(d, "M1\n", 3);
     128
    89129        alarm(atoi(argv[2]));
    90130        FD_ZERO(&rfds);
     
    94134                if (nfds > 0 && FD_ISSET(d, &rfds)) {
    95135                        bytes = read(d, buf, sizeof(buf));
     136                        if (bytes > sizeof(buf)) {
     137                                errx(1, "read %d/0x%x bytes", bytes, bytes);
     138                        }
    96139                        if (bytes < 0 && errno != EAGAIN) {
    97140                                warn("read");
     141                                continue;
    98142                        }
    99143                        for (i = 0; i < bytes; i++) {
     144                                if (i > sizeof(buf))
     145                                        abort();
     146                                if (bytes > sizeof(buf))
     147                                        abort();
     148                                if (gpioi > sizeof(buf))
     149                                        abort();
     150                                if (tensioni > sizeof(buf))
     151                                        abort();
     152                                if (currenti > sizeof(buf))
     153                                        abort();
    100154                                switch(state) {
    101155                                case INIT:
    102156                                        /* look for an X mark */
    103                                         if (buf[i] == 'X')
     157                                        if (buf[i] == 'X') {
    104158                                                state = GPIO;
     159                                                gpioi = 0;
     160                                        }
    105161                                        break;
    106162                                case GPIO:
    107                                         gpio[0] = buf[i];
    108                                         gpio[1] = '\0';
    109                                         val = strtol(gpio, &endp, 16);
     163                                        gpio[gpioi] = buf[i];
     164                                        gpioi++;
     165                                        if (gpioi < 2)
     166                                                break;
     167                                        gpio[gpioi] = '\0';
     168                                        gpioval = strtol(gpio, &endp, 16);
    110169                                        if (*endp != '\0') {
    111170                                                fprintf(stderr,
    112171                                                    "gpio error: %c\n",
    113172                                                    *endp);
    114                                                 state = INIT;
    115                                         } else {
    116                                                 state = CURRENT;
    117                                                 currenti = 0;
    118                                                 printf("%2d ", val);
     173                                                exitst = 1;
     174                                                state = INIT;
     175                                        } else {
     176                                                state = TENSION;
     177                                                tensioni = 0;
    119178                                        }
    120179                                        break;
     
    125184                                                break;
    126185                                        current[currenti] = '\0';
    127                                         val = strtol(current, &endp, 16);
     186                                        currentval = strtol(current, &endp, 16);
    128187                                        if (*endp != '\0') {
    129188                                                fprintf(stderr,
    130189                                                    "current error: %c\n",
    131190                                                    *endp);
    132                                                 state = INIT;
    133                                         } else {
    134                                                 state = TENSION;
     191                                                exitst = 1;
     192                                                state = INIT;
     193                                        } else {
     194                                                state = MARK;
    135195                                                tensioni = 0;
    136                                                 printf("%4d ", val);
    137196                                        }
    138197                                        break;
     
    143202                                                break;
    144203                                        tension[tensioni] = '\0';
    145                                         val = strtol(tension, &endp, 16);
     204                                        tensionval = strtol(tension, &endp, 16);
    146205                                        if (*endp != '\0') {
    147206                                                fprintf(stderr,
    148207                                                    "tension error: %c\n",
    149208                                                    *endp);
    150                                                 state = INIT;
    151                                         } else {
    152                                                 state = MARK;
    153                                                 printf("%4d\n", val);
     209                                                exitst = 1;
     210                                                state = INIT;
     211                                        } else {
     212                                                state = CURRENT;
     213                                                currenti = 0;
    154214                                        }
    155215                                        break;
     
    159219                                                    "mark error: %c\n",
    160220                                                    buf[i]);
    161                                                 state = INIT;
    162                                         } else {
     221                                                exitst = 1;
     222                                                state = INIT;
     223                                        } else {
     224                                                printf("%3d ", gpioval);
     225                                                printf("%4d ", tensionval);
     226                                                printf("%4d\n", currentval);
    163227                                                state = GPIO;
     228                                                gpioi = 0;
    164229                                        }
    165230                                        break;
     
    170235                }
    171236        }
     237quit:
     238        /* stop measures */
     239        write(d, "M0\n", 3);
     240        if (fflush(stdout) == EOF) { 
     241                warn("fflush");       
     242        }
     243
    172244        if (tcsetattr(d, TCSANOW, &ot) < 0) {
    173245                err(1, "restore tcsetattr");
    174246        }
    175         exit(0);
     247        exit(exitst);
    176248}
    177249
Note: See TracChangeset for help on using the changeset viewer.