Browse Source

Cleanups for thesis inclusion

tags/zoom-1.0
Jim Paris 11 years ago
parent
commit
75dfbb28b3
9 changed files with 130 additions and 175 deletions
  1. +1
    -1
      pc/Makefile
  2. +36
    -102
      pc/calibrate.c
  3. +15
    -9
      pc/dactest.c
  4. +22
    -17
      pc/dctest.c
  5. +1
    -1
      pc/gpib.c
  6. +19
    -19
      pc/mt19937ar.c
  7. +18
    -13
      pc/read.c
  8. +13
    -10
      pc/serial-util.c
  9. +5
    -3
      pc/zoom.c

+ 1
- 1
pc/Makefile View File

@@ -25,6 +25,6 @@ calibrate: calibrate.o serial-util.o gpib.o zoom.o
dctest: LDFLAGS=-lpthread
dctest: dctest.o serial-util.o gpib.o zoom.o mt19937ar.o

dactest: dactest.o serial-util.o gpib.o zoom.o mt19937ar.o
dactest: dactest.o serial-util.o gpib.o zoom.o mt19937ar.o
clean:
rm -f *.o read calibrate dctest dactest

+ 36
- 102
pc/calibrate.c View File

@@ -21,7 +21,6 @@ void single_sweep(int zoom);
void calibrate(int zoom, int gpib);
void sweep_ota(int zoom, int gpib, int ota_sweep_count);
void hold_ota(int zoom, int gpib, int ota_cmd);
void hold_ota2(int zoom, int gpib, int ota_cmd);

#define info(x...) fprintf(stderr,x)

@@ -30,9 +29,10 @@ void handle_sig(int sig) { g_quit = 1; }

int main(int argc, char *argv[])
{
// char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A6007wag-if00-port0");
char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A6007wc5-if00-port0");
char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0");
char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_FT232R_"
"USB_UART_A6007wc5-if00-port0");
char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_Prologix_"
"GPIB-USB_Controller_PXQQY20G-if00-port0");
int getopt_index;
int zoom, gpib;
int do_write = 0;
@@ -60,7 +60,7 @@ int main(int argc, char *argv[])

while ((c = getopt_long(argc, argv, "Z:G:szw:o:l:h?",
long_opts, &getopt_index)) != -1) {
switch(c)
switch(c)
{
case 'Z':
free(zoomdev);
@@ -108,18 +108,26 @@ int main(int argc, char *argv[])
break;
}
}
if (help) {
fprintf(stderr, "Zoom Nilm Calibration Tool\n");
fprintf(stderr, "usage: %s [options]\n\n", *argv);
fprintf(stderr, " -Z, --zoom-device %-14s zoom NILM serial port\n", "/dev/xxx" /*zoomdev*/);
fprintf(stderr, " -G, --gpib-device %-14s GPIB serial port\n", "/dev/xxx" /*gpibdev*/);
fprintf(stderr, " -w, --write-dac %-14d write one value to the DAC constantly\n", write_cmd);
fprintf(stderr, " -s, --single-sweep do a single sweep on the PIC\n");
fprintf(stderr, " -z, --zero set DAC value such that ADC input is centered\n");
fprintf(stderr, " -o, --ota-sweep %-14d sweep OTA\n", ota_sweep_count);
fprintf(stderr, " -l, --ota-hold %-14d hold OTA constant\n", ota_cmd);
fprintf(stderr, " -h, --help this help\n");
fprintf(stderr, " -Z, --zoom-device %-14s "
"zoom NILM serial port\n", "/dev/xxx" /*zoomdev*/);
fprintf(stderr, " -G, --gpib-device %-14s "
"GPIB serial port\n", "/dev/xxx" /*gpibdev*/);
fprintf(stderr, " -w, --write-dac %-14d "
"write one value to the DAC constantly\n", write_cmd);
fprintf(stderr, " -s, --single-sweep "
"do a single sweep on the PIC\n");
fprintf(stderr, " -z, --zero "
"set DAC value such that ADC input is centered\n");
fprintf(stderr, " -o, --ota-sweep %-14d "
"sweep OTA\n", ota_sweep_count);
fprintf(stderr, " -l, --ota-hold %-14d "
"hold OTA constant\n", ota_cmd);
fprintf(stderr, " -h, --help "
"this help\n");
return 1;
}

@@ -151,7 +159,7 @@ int main(int argc, char *argv[])

switch (ota_opt) {
case 1:
sweep_ota(zoom, gpib, ota_sweep_count);
sweep_ota(zoom, gpib, ota_sweep_count);
break;
case 2:
hold_ota(zoom, gpib, ota_cmd);
@@ -173,7 +181,7 @@ void calibrate(int zoom, int gpib)
int zero;
int r = 0;
int dac[ZOOM_SWEEP_COUNT];
int adc[ZOOM_SWEEP_COUNT];
int adc[ZOOM_SWEEP_COUNT];

info("Initializing Zoom NILM\n");
if (zoom_init(zoom) < 0) goto fail;
@@ -198,7 +206,7 @@ void calibrate(int zoom, int gpib)
// for (idesired = -0.2; idesired <= 0.2 && !g_quit; idesired += 0.02) {
info("Zeroing\n");
if (zoom_zero_start(zoom) < 0) goto fail;
info("Setting current: %.8f\n", idesired);
keithley_current(gpib, idesired);
usleep(100000);
@@ -229,7 +237,7 @@ fail:
info("Failed (code %d)\n", r);
goto safecleanup;
}
void write_dac(int zoom, int dac)
{
char buf[128];
@@ -237,7 +245,7 @@ void write_dac(int zoom, int dac)
if (zoom_init(zoom) < 0)
errx(1, "init failed");

if (dac < 0)
if (dac < 0)
dac = 0;
if (dac > 65535)
dac = 65535;
@@ -256,7 +264,7 @@ void single_sweep(int zoom)
{
int i, r;
int dac[ZOOM_SWEEP_COUNT];
int adc[ZOOM_SWEEP_COUNT];
int adc[ZOOM_SWEEP_COUNT];

info("Initializing Zoom NILM\n");
if (zoom_init_nozero(zoom) < 0) goto fail;
@@ -311,7 +319,7 @@ void sweep_ota(int zoom, int gpib, int ota_sweep_count)
char zcmd[128];
int mycmd = 0;
int cmd_inc = 4096;
info("Initializing Zoom NILM\n");
if (zoom_init(zoom) < 0) goto fail;

@@ -346,14 +354,14 @@ void sweep_ota(int zoom, int gpib, int ota_sweep_count)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");
usleep(100);
iactual = keithley2002_read(gpib);
printf("%d %.8f\n",(int)(mycmd + j), iactual);
}
mycmd += cmd_inc;

}
@@ -384,7 +392,7 @@ void hold_ota(int zoom, int gpib, int ota_cmd)
int r = 0;
char buf[128];
char zcmd[128];
info("Initializing Zoom NILM\n");
if (zoom_init(zoom) < 0) goto fail;

@@ -413,90 +421,16 @@ void hold_ota(int zoom, int gpib, int ota_cmd)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");
usleep(100);

info("Holding OTA\n");
for (i = 0; i <= 500 && !g_quit; i++){

iactual = keithley2002_read2(gpib, &tx);
printf("%.12f %.12f\n", tx, iactual);
}

// return to zero
buf[0] = '0';
if (safewrite(zoom, buf, 1) != 1)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");



safecleanup:
return;

fail:
info("Failed (code %d)\n", r);
goto safecleanup;
}

void hold_ota2(int zoom, int gpib, int ota_cmd)
{
int i;
int r = 0;
char buf[128];
char zcmd[128];
int dac1, dac2, adc1, adc2;
info("Initializing Zoom NILM\n");
if (zoom_init(zoom) < 0) goto fail;

//info("Initializing GPIB\n");
//if (gpib_init(gpib) < 0) goto fail;

//info("Initializing Keithley 2002 Multimeter\n");
//if (gpib_addr(gpib, 23) < 0) goto fail;
//if (keithley2002_init2(gpib) < 0) goto fail;
//if (isnan(keithley2002_read(gpib))) goto fail;

buf[0] = '0';
if (safewrite(zoom, buf, 1) != 1)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");
drain(zoom);

if(ota_cmd > 65535 || ota_cmd < 0)
errx(1, "ota command out-of-range");

if(sprintf(zcmd,"v%.4x",(ota_cmd)) < 5)
errx(1, "fail hex conversion");

if (safewrite(zoom, zcmd, 5) != 5)
errx(1, "write failed");
if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");
usleep(100);

zcmd[0] = ' ';

info("Holding OTA\n");
for (i = 0; i <= 1000 && !g_quit; i++){

if(safewrite(zoom,zcmd,1) != 1)
errx(1, "write failed");

if (fdgets(buf, 128, zoom, 1000) == NULL)
errx(1, "read timeout");
printf("%.12f %.12f\n", tx, iactual);

if (sscanf(buf,"%x %d %x %d", &dac1, &dac2, &adc1, &adc2) != 4)
errx(1, "read error");
printf("%d %d\n", dac2, adc2);
}

// return to zero


+ 15
- 9
pc/dactest.c View File

@@ -28,8 +28,10 @@ void handle_sig(int sig) { g_quit = 1; }

int main(int argc, char *argv[])
{
char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A6007wc5-if00-port0");
char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0");
char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_"
"FT232R_USB_UART_A6007wc5-if00-port0");
char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_"
"Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0");
int getopt_index;
int zoom, gpib;
unsigned long seed = 1337;
@@ -46,7 +48,7 @@ int main(int argc, char *argv[])

while ((c = getopt_long(argc, argv, "Z:G:s:h?",
long_opts, &getopt_index)) != -1) {
switch(c)
switch(c)
{
case 'Z':
free(zoomdev);
@@ -68,14 +70,17 @@ int main(int argc, char *argv[])
break;
}
}
if (help) {
fprintf(stderr, "Zoom Nilm DC Test\n");
fprintf(stderr, "usage: %s [options]\n\n", *argv);
fprintf(stderr, " -Z, --zoom-device %-14s zoom NILM serial port\n", "/dev/xxx" /*zoomdev*/);
fprintf(stderr, " -G, --gpib-device %-14s GPIB serial port\n", "/dev/xxx" /*gpibdev*/);
fprintf(stderr, " -Z, --zoom-device %-14s "
"zoom NILM serial port\n", "/dev/xxx" /*zoomdev*/);
fprintf(stderr, " -G, --gpib-device %-14s "
"GPIB serial port\n", "/dev/xxx" /*gpibdev*/);
fprintf(stderr, " -s, --seed %-16ld random seed\n", seed);
fprintf(stderr, " -h, --help this help\n");
fprintf(stderr, " -h, --help "
"this help\n");
return 1;
}

@@ -123,7 +128,8 @@ void write_random(int zoom, int gpib)
zoom_write_dac(zoom, dac);
gettimeofday(&now, NULL);
meas = keithley2002_read(gpib);
printf("%ld.%06ld %d %.12f\n", now.tv_sec, now.tv_usec, dac, meas);
printf("%ld.%06ld %d %.12f\n", now.tv_sec, now.tv_usec,
dac, meas);
fflush(stdout);
}

@@ -134,4 +140,4 @@ fail:
info("Failed\n");
goto safecleanup;
}

+ 22
- 17
pc/dctest.c View File

@@ -27,8 +27,10 @@ static void handle_sig(int sig) { g_quit = 1; }

int main(int argc, char *argv[])
{
char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A6007wc5-if00-port0");
char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0");
char *zoomdev=strdup("/dev/serial/by-id/usb-FTDI_"
"FT232R_USB_UART_A6007wc5-if00-port0");
char *gpibdev=strdup("/dev/serial/by-id/usb-Prologix_"
"Prologix_GPIB-USB_Controller_PXQQY20G-if00-port0");
int rate=500000;
unsigned long seed = 1337;
int getopt_index;
@@ -47,7 +49,7 @@ int main(int argc, char *argv[])

while ((c = getopt_long(argc, argv, "Z:G:r:s:h?",
long_opts, &getopt_index)) != -1) {
switch(c)
switch(c)
{
case 'Z':
free(zoomdev);
@@ -74,15 +76,17 @@ int main(int argc, char *argv[])
break;
}
}
if (help) {
fprintf(stderr, "Zoom Nilm DC Test Tool\n");
fprintf(stderr, "usage: %s [options]\n\n", *argv);
fprintf(stderr, " -Z, --zoom-device %-9s zoom NILM serial port\n", "/dev/xxx");
fprintf(stderr, " -G, --gpib-device %-9s GPIB serial port\n", "/dev/xxx");
fprintf(stderr, " -Z, --zoom-device %-9s "
"zoom NILM serial port\n", "/dev/xxx");
fprintf(stderr, " -G, --gpib-device %-9s "
"GPIB serial port\n", "/dev/xxx");
fprintf(stderr, " -r, --rate %-16d baud rate\n", rate);
fprintf(stderr, " -s, --seed %-16ld random seed\n", seed);
fprintf(stderr, " -h, --help this help\n");
fprintf(stderr, " -h, --help this help\n");
return 1;
}

@@ -102,7 +106,7 @@ int main(int argc, char *argv[])

/* do the dc test */
dctest(zoom, gpib);
close(zoom);
close(gpib);
return 0;
@@ -139,7 +143,7 @@ int process_adc_dac(const uint8_t *buf, struct threadinfo_t *ti)
tmp = ((buf[0] & 0x0F) << 8) | buf[1];

/* sign-extend ADC value */
if (tmp & 0x0800)
if (tmp & 0x0800)
tmp |= 0xF000;
else
tmp &= ~0xF000;
@@ -156,7 +160,7 @@ int process_adc_dac(const uint8_t *buf, struct threadinfo_t *ti)
pthread_mutex_unlock(&ti->mutex);

/* write it out */
printf("%d %.12f %.12f %.8f %5d %5d %d\n",
printf("%d %.12f %.12f %.8f %5d %5d %d\n",
stable,
idesired,
iactual,
@@ -198,7 +202,8 @@ static int process(const uint8_t *buf, int len, struct threadinfo_t *ti)
}
if (!ok) {
/* badly formed data; eat one byte and retry */
info("throwing away 0x%02x '%c'\n", buf[0], isprint(buf[0]) ? buf[0] : '.');
info("throwing away 0x%02x '%c'\n", buf[0],
isprint(buf[0]) ? buf[0] : '.');
buf++;
n++;
goto retry;
@@ -219,11 +224,11 @@ static void *read_data(void *arg)
len = 0;
while (!ti->quit_flag) {
int processed, n;
n = saferead_timeout(ti->fd,
n = saferead_timeout(ti->fd,
buf + len,
sizeof(buf) - len,
1000);
if (n < 0)
if (n < 0)
err(1, "read");
if (n == 0)
continue;
@@ -284,7 +289,7 @@ static void dctest(int zoom, int gpib)
/* Init Keithley */
info("Initializing GPIB\n");
if (gpib_init(gpib) < 0) { info("failed\n"); goto out1; }
info("Initializing Keithley\n");
if (gpib_addr(gpib, 24) < 0) { info("failed\n"); goto out1; }
if (keithley_init(gpib) < 0) { info("failed\n"); goto out2; }
@@ -339,7 +344,7 @@ static void dctest(int zoom, int gpib)
#define K_MIN -0.5 /* keithley min, amps */
#define K_MAX 0.5 /* keithley max, amps */
#define STEPSIZE 0.03 /* max size of small step, in amps */
/* Choose any value within the Keithley range */
double desired = genrand(K_MIN, K_MAX);
info("Big step: %.12f\n", desired);
@@ -352,7 +357,7 @@ static void dctest(int zoom, int gpib)
/* Choose a few more values nearby */
for (i = 0; i < STEPS && !g_quit; i++) {
desired += genrand(-STEPSIZE/2, STEPSIZE/2);
if (desired < -1.0)
if (desired < -1.0)
desired = -1.0;
if (desired > 1.0)
desired = 1.0;
@@ -376,4 +381,4 @@ out2:
out1:
return;
}

+ 1
- 1
pc/gpib.c View File

@@ -12,7 +12,7 @@
if (safewrite(fd, string, ____l) != ____l) return rv; \
if (safewrite(fd, "\r", 1) != 1) return rv; } while(0)
#define gput(string) gputv(string, -1)
int gpib_init(int fd)
{
gput("++mode 1");


+ 19
- 19
pc/mt19937ar.c View File

@@ -1,12 +1,12 @@
/*
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.

Before using, initialize the state by using init_genrand(seed)
Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).

Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@@ -19,8 +19,8 @@
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
@@ -43,7 +43,7 @@

#include "mt19937ar.h"

/* Period parameters */
/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
@@ -58,8 +58,8 @@ void init_genrand(unsigned long s)
{
mt[0]= s & 0xffffffffUL;
for (mti=1; mti<N; mti++) {
mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt[]. */
@@ -95,7 +95,7 @@ void init_by_array(unsigned long init_key[], int key_length)
if (i>=N) { mt[0] = mt[N-1]; i=1; }
}

mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
}

/* generates a random number on [0,0xffffffff]-interval */
@@ -124,7 +124,7 @@ unsigned long genrand_int32(void)

mti = 0;
}
y = mt[mti++];

/* Tempering */
@@ -145,29 +145,29 @@ long genrand_int31(void)
/* generates a random number on [0,1]-real-interval */
double genrand_real1(void)
{
return genrand_int32()*(1.0/4294967295.0);
/* divided by 2^32-1 */
return genrand_int32()*(1.0/4294967295.0);
/* divided by 2^32-1 */
}

/* generates a random number on [0,1)-real-interval */
double genrand_real2(void)
{
return genrand_int32()*(1.0/4294967296.0);
return genrand_int32()*(1.0/4294967296.0);
/* divided by 2^32 */
}

/* generates a random number on (0,1)-real-interval */
double genrand_real3(void)
{
return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
/* divided by 2^32 */
}

/* generates a random number on [0,1) with 53-bit resolution*/
double genrand_res53(void)
{
unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
return(a*67108864.0+b)*(1.0/9007199254740992.0);
}
double genrand_res53(void)
{
unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
return(a*67108864.0+b)*(1.0/9007199254740992.0);
}
/* These real versions are due to Isaku Wada, 2002/01/09 added */


+ 18
- 13
pc/read.c View File

@@ -21,7 +21,8 @@ int process(const uint8_t *buf, int len);

int main(int argc, char *argv[])
{
char *device=strdup("/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A6007wc5-if00-port0");
char *device=strdup("/dev/serial/by-id/usb-FTDI_FT232R_"
"USB_UART_A6007wc5-if00-port0");
int rate=500000;
int fd;
int getopt_index;
@@ -45,7 +46,7 @@ int main(int argc, char *argv[])

while ((c = getopt_long(argc, argv, "d:r:xDunsh?",
long_opts, &getopt_index)) != -1) {
switch(c)
switch(c)
{
case 'd':
free(device);
@@ -78,7 +79,7 @@ int main(int argc, char *argv[])
break;
}
}
if (help) {
fprintf(stderr, "Zoom Nilm Client\n");
fprintf(stderr, "usage: %s [options]\n\n", *argv);
@@ -86,9 +87,12 @@ int main(int argc, char *argv[])
fprintf(stderr, " -r, --rate %-16d baud rate\n", rate);
fprintf(stderr, " -x, --hex hex out\n");
fprintf(stderr, " -D, --dec dec out\n");
fprintf(stderr, " -u, --unprocessed dump raw unprocessed data\n");
fprintf(stderr, " -n, --no-calibrate skip calibration routine\n");
fprintf(stderr, " -s, --screen send \\r instead of \\n\n");
fprintf(stderr, " -u, --unprocessed "
"dump raw unprocessed data\n");
fprintf(stderr, " -n, --no-calibrate "
"skip calibration routine\n");
fprintf(stderr, " -s, --screen "
"send \\r instead of \\n\n");
fprintf(stderr, " -h, --help this cruft\n");
return 1;
}
@@ -107,14 +111,14 @@ int main(int argc, char *argv[])
while (1) {
int processed, n;
n = read(fd, buf + len, sizeof(buf) - len);
if (n <= 0)
if (n <= 0)
err(1, "read");
len += n;
processed = process((uint8_t *) buf, len);
memmove(buf, buf + processed, len - processed);
len -= processed;
}
return 1;
}

@@ -132,12 +136,12 @@ int process_adc_dac(const uint8_t *buf)

if (buf[0] & 0x10)
overflow = 1;
else
else
overflow = 0;

tmp = ((buf[0] & 0x0F) << 8) | buf[1];
/* sign-extend ADC value */
if (tmp & 0x0800)
if (tmp & 0x0800)
tmp |= 0xF000;
else
tmp &= ~0xF000;
@@ -155,8 +159,8 @@ int process_adc_dac(const uint8_t *buf)
if (overflow)
printf(" **** adc may have clamped");
}
if (screen)
if (screen)
printf("\033[K\r");
else
printf("\n");
@@ -200,7 +204,8 @@ int process(const uint8_t *buf, int len)
}
if (!ok) {
/* badly formed data; eat one byte and retry */
fprintf(stderr,"throwing away 0x%02x '%c'\n", buf[0], isprint(buf[0]) ? buf[0] : '.');
fprintf(stderr,"throwing away 0x%02x '%c'\n",
buf[0], isprint(buf[0]) ? buf[0] : '.');
buf++;
n++;
goto retry;


+ 13
- 10
pc/serial-util.c View File

@@ -26,12 +26,12 @@ static int rate_to_constant(int baudrate) {
B(50); B(75); B(110); B(134); B(150);
B(200); B(300); B(600); B(1200); B(1800);
B(2400); B(4800); B(9600); B(19200); B(38400);
B(57600); B(115200); B(230400); B(460800); B(500000);
B(576000); B(921600); B(1000000);B(1152000);B(1500000);
B(57600); B(115200); B(230400); B(460800); B(500000);
B(576000); B(921600); B(1000000);B(1152000);B(1500000);
default: return 0;
}
#undef B
}
}

/* Open serial port in raw mode, with custom baudrate if necessary */
int serial_open(const char *device, int rate)
@@ -54,7 +54,7 @@ int serial_open(const char *device, int rate)
serinfo.flags &= ~ASYNC_SPD_MASK;
serinfo.flags |= ASYNC_SPD_CUST;
serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate;
if (serinfo.custom_divisor < 1)
if (serinfo.custom_divisor < 1)
serinfo.custom_divisor = 1;
if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
return -1;
@@ -121,7 +121,7 @@ ssize_t safewrite(int fd, const void *buf, size_t count)
size_t nwritten = 0;
while (count > 0) {
ssize_t r = write(fd, buf, count);
if (r < 0 && errno == EINTR)
continue;
if (r < 0)
@@ -142,9 +142,9 @@ int drain_timeout(int fd, int msec)
int ret;
while (1) {
ret = saferead_timeout(fd, buf, sizeof(buf), msec);
if (ret <= 0)
if (ret <= 0)
return ret;
}
}
}

/* Like fprintf, but to a fd, using safewrite. */
@@ -170,7 +170,8 @@ char *fdgets(char *s, int size, int fd, int timeout_ms)
int ret;
int nread = 0;

/* Not very efficient, needs to read one char at a time to avoid buffering */
/* Not very efficient; needs to read one char at a time to
* avoid buffering */
while (nread < (size - 1)) {
ret = saferead_timeout(fd, &s[nread], 1, timeout_ms);
if (ret <= 0) {
@@ -194,6 +195,8 @@ void chomp(char *s)
{
int len = strlen(s);
/* do it twice to remove \r\n as well */
if (len > 1 && (s[len - 1] == '\r' || s[len - 1] == '\n')) s[--len] = '\0';
if (len > 1 && (s[len - 1] == '\r' || s[len - 1] == '\n')) s[--len] = '\0';
if (len > 1 && (s[len - 1] == '\r' || s[len - 1] == '\n'))
s[--len] = '\0';
if (len > 1 && (s[len - 1] == '\r' || s[len - 1] == '\n'))
s[--len] = '\0';
}

+ 5
- 3
pc/zoom.c View File

@@ -7,8 +7,10 @@
#include "zoom.h"
#include "serial-util.h"

#define zputs(s) do { if (safewrite(fd, s, strlen(s)) != strlen(s)) return -1; } while(0)
#define zputc(ch) do { const char ____c = ch; if (safewrite(fd, &____c, 1) != 1) return -1; } while(0)
#define zputs(s) do { if (safewrite(fd, s, strlen(s)) != strlen(s)) \
return -1; } while(0)
#define zputc(ch) do { const char ____c = ch; \
if (safewrite(fd, &____c, 1) != 1) return -1; } while(0)

static int last_dac, last_adc;

@@ -84,7 +86,7 @@ int zoom_sweep(int fd, int dac[ZOOM_SWEEP_COUNT], int adc[ZOOM_SWEEP_COUNT])
int zoom_write_dac(int fd, int dac)
{
char s[128];
sprintf(s, "v%04x", dac);
zputs(s);
if (verify_prompt(fd) < 0)


Loading…
Cancel
Save