Browse Source

update stuff

git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@7584 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
jim 15 years ago
parent
commit
06e8c490ba
1 changed files with 60 additions and 4 deletions
  1. +60
    -4
      pc/read.c

+ 60
- 4
pc/read.c View File

@@ -16,6 +16,7 @@
int hex = 0;
int screen = 0;
int unprocessed = 0;
int total = 0;

int process(const uint8_t *buf, int len);

@@ -74,6 +75,7 @@ int main(int argc, char *argv[])
{ "device", required_argument, NULL, 'd' },
{ "rate", required_argument, NULL, 'r' },
{ "hex", no_argument, NULL, 'x' },
{ "total", no_argument, NULL, 't' },
{ "unprocessed", no_argument, NULL, 'u' },
{ "screen", no_argument, NULL, 's' },
{ "help", no_argument, NULL, 'h' },
@@ -82,7 +84,7 @@ int main(int argc, char *argv[])
int help=0;
char c;

while ((c = getopt_long(argc, argv, "d:r:xush?",
while ((c = getopt_long(argc, argv, "d:r:xutsh?",
long_opts, &getopt_index)) != -1) {
switch(c)
{
@@ -98,6 +100,9 @@ int main(int argc, char *argv[])
case 'x':
hex = 1;
break;
case 't':
total = 1;
break;
case 'u':
unprocessed = 1;
break;
@@ -120,6 +125,7 @@ int main(int argc, char *argv[])
fprintf(stderr, " -x, --hex hex out\n");
fprintf(stderr, " -u, --unprocessed dump raw unprocessed data\n");
fprintf(stderr, " -s, --screen send \\r instead of \\n\n");
fprintf(stderr, " -t, --total just display total amps\n");
fprintf(stderr, " -h, --help this cruft\n");
return 1;
}
@@ -142,10 +148,33 @@ int main(int argc, char *argv[])
return 1;
}

float dac_to_current(uint16_t dacv)
{
float amps;
amps = dacv * (20.0 / 32768.0) - 20.0;
return amps;
}

float adc12_to_current(int16_t picv)
{
float amps;

if (picv < 0)
picv = 0;
if (picv > 2047)
picv = 2047;
amps = (1024.0 - picv) / 3522.6;
return amps;
}

int process(const uint8_t *buf, int len)
{
int n = 0;
uint16_t dac, adc;
uint16_t dac, tmp;
int16_t adc;
float idac = dac_to_current(dac);
float iadc = adc12_to_current(adc);

if (unprocessed) {
n = write(fileno(stdout), buf, len);
@@ -167,12 +196,39 @@ int process(const uint8_t *buf, int len)
goto retry;
}

adc = ((buf[0] & 0x7f) << 5) |
tmp = ((buf[0] & 0x7f) << 5) |
((buf[1] & 0x7c) >> 2);
dac = ((buf[1] & 0x03) << 14) |
(buf[2] << 7) | buf[3];
printf(hex ? "%04x %04x%c" : "%d %d%c", dac, adc, screen ? '\r' : '\n');
/* sign-extend ADC value */
if (tmp & 0x0800)
tmp |= 0xF000;
else
tmp &= ~0xF000;
adc = (int16_t)tmp;

/* compute floating-point currents */
idac = dac_to_current(dac);
iadc = adc12_to_current(adc);

/* send it out */
if (hex) {
printf("%04x %03x", dac, adc & 0x0FFF);
} else if (total) {
printf("%.6f", idac + iadc);
} else {
printf("DAC: %5d (% f) ADC: % 5d (% f) Total: % 10.6f amps",
dac, idac, (int16_t)adc, iadc, idac + iadc);

if (adc < 0 || adc >= 2047)
printf(" **** adc limit");
}

if (screen)
printf("\033[K\r");
else
printf("\n");
}
return n;
}

Loading…
Cancel
Save