Skip to content
Snippets Groups Projects
Commit c6e37217 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Cleanup code, fix underflow error

parent 6ca5f79f
Branches
No related tags found
No related merge requests found
...@@ -133,12 +133,13 @@ static struct moberg_status analog_out_write( ...@@ -133,12 +133,13 @@ static struct moberg_status analog_out_write(
struct moberg_channel_context *channel = &analog_out->channel_context; struct moberg_channel_context *channel = &analog_out->channel_context;
struct moberg_device_context *device = channel->device; struct moberg_device_context *device = channel->device;
struct analog_map map = device->analog_out.map[channel->index]; struct analog_map map = device->analog_out.map[channel->index];
long as_long = value - map.min / map.delta; long as_long = (value - map.min) / map.delta;
if (as_long < 0) { if (as_long < 0) {
value = 0; as_long = 0;
} else if (as_long > map.maxdata) { } else if (as_long > map.maxdata) {
as_long = map.maxdata; as_long = map.maxdata;
} }
struct serial2002_data data = { is_channel, map.index, as_long }; struct serial2002_data data = { is_channel, map.index, as_long };
return serial2002_write(device->port.fd, data); return serial2002_write(device->port.fd, data);
} }
...@@ -204,7 +205,6 @@ return_result: ...@@ -204,7 +205,6 @@ return_result:
return result; return result;
err_einval: err_einval:
return MOBERG_ERRNO(EINVAL); return MOBERG_ERRNO(EINVAL);
fprintf(stderr, "%s\n", __FUNCTION__);
return MOBERG_OK; return MOBERG_OK;
} }
...@@ -218,6 +218,7 @@ static struct moberg_device_context *new_context(struct moberg *moberg, ...@@ -218,6 +218,7 @@ static struct moberg_device_context *new_context(struct moberg *moberg,
result->moberg = moberg; result->moberg = moberg;
result->dlclose = dlclose; result->dlclose = dlclose;
result->dlhandle = dlhandle; result->dlhandle = dlhandle;
result->port.timeout = 100000;
} }
return result; return result;
} }
...@@ -258,9 +259,10 @@ static void remap_analog( ...@@ -258,9 +259,10 @@ static void remap_analog(
remap->map[remap->count].delta = remap->map[remap->count].delta =
(remap->map[remap->count].max - remap->map[remap->count].min) / (remap->map[remap->count].max - remap->map[remap->count].min) /
remap->map[remap->count].maxdata; remap->map[remap->count].maxdata;
} } else {
remap->map[remap->count].delta = 1.0;
fprintf(stderr, "%d -> %d\n", remap->count, i); }
remap->count++; remap->count++;
} }
} }
...@@ -276,7 +278,6 @@ static void remap_digital( ...@@ -276,7 +278,6 @@ static void remap_digital(
for (int i = 0 ; i < count ; i++) { for (int i = 0 ; i < count ; i++) {
if (channel[i].kind == kind) { if (channel[i].kind == kind) {
remap->map[remap->count].index = i; remap->map[remap->count].index = i;
fprintf(stderr, "%d -> %d\n", remap->count, i);
remap->count++; remap->count++;
} }
} }
...@@ -287,7 +288,6 @@ static struct moberg_status device_open(struct moberg_device_context *device) ...@@ -287,7 +288,6 @@ static struct moberg_status device_open(struct moberg_device_context *device)
struct moberg_status result; struct moberg_status result;
int fd = -1; int fd = -1;
fprintf(stderr, "%s\n", __FUNCTION__);
if (device->port.count == 0) { if (device->port.count == 0) {
fd = open(device->port.name, O_RDWR); fd = open(device->port.name, O_RDWR);
if (fd < 0) { goto err_errno; } if (fd < 0) { goto err_errno; }
...@@ -323,10 +323,8 @@ static struct moberg_status device_open(struct moberg_device_context *device) ...@@ -323,10 +323,8 @@ static struct moberg_status device_open(struct moberg_device_context *device)
device->port.fd = fd; device->port.fd = fd;
} }
device->port.count++; device->port.count++;
fprintf(stderr, "OPENED %d\n", device->port.count);
return MOBERG_OK; return MOBERG_OK;
err_errno: err_errno:
fprintf(stderr, "ERRNO %d\n", errno);
result = MOBERG_ERRNO(errno); result = MOBERG_ERRNO(errno);
err_result: err_result:
if (fd >= 0) { if (fd >= 0) {
...@@ -337,16 +335,13 @@ err_result: ...@@ -337,16 +335,13 @@ err_result:
static struct moberg_status device_close(struct moberg_device_context *device) static struct moberg_status device_close(struct moberg_device_context *device)
{ {
fprintf(stderr, "%s\n", __FUNCTION__);
if (device->port.count < 0) { errno = ENODEV; goto err_errno; } if (device->port.count < 0) { errno = ENODEV; goto err_errno; }
device->port.count--; device->port.count--;
if (device->port.count == 0) { if (device->port.count == 0) {
fprintf(stderr, "CLOSE\n");
if (close(device->port.fd) < 0) { goto err_errno; } if (close(device->port.fd) < 0) { goto err_errno; }
} }
return MOBERG_OK; return MOBERG_OK;
err_errno: err_errno:
fprintf(stderr, "CLOSE %d\n", errno);
return MOBERG_ERRNO(errno); return MOBERG_ERRNO(errno);
} }
...@@ -371,14 +366,12 @@ static int channel_down(struct moberg_channel *channel) ...@@ -371,14 +366,12 @@ static int channel_down(struct moberg_channel *channel)
static struct moberg_status channel_open(struct moberg_channel *channel) static struct moberg_status channel_open(struct moberg_channel *channel)
{ {
fprintf(stderr, "%s\n", __FUNCTION__);
struct moberg_status result = device_open(channel->context->device); struct moberg_status result = device_open(channel->context->device);
return result; return result;
} }
static struct moberg_status channel_close(struct moberg_channel *channel) static struct moberg_status channel_close(struct moberg_channel *channel)
{ {
fprintf(stderr, "%s\n", __FUNCTION__);
struct moberg_status result = device_close(channel->context->device); struct moberg_status result = device_close(channel->context->device);
return result; return result;
} }
......
...@@ -33,7 +33,6 @@ static struct moberg_status tty_write(int fd, unsigned char *buf, int count) ...@@ -33,7 +33,6 @@ static struct moberg_status tty_write(int fd, unsigned char *buf, int count)
while (n < count) { while (n < count) {
int written = write(fd, &buf[n], count - n); int written = write(fd, &buf[n], count - n);
if (written == 0) { if (written == 0) {
fprintf(stderr, "Failed to write\n");
return MOBERG_ERRNO(ENODATA); return MOBERG_ERRNO(ENODATA);
} else if (written < 0) { } else if (written < 0) {
return MOBERG_ERRNO(errno); return MOBERG_ERRNO(errno);
...@@ -50,7 +49,7 @@ static struct moberg_status tty_read(int fd, long timeout, unsigned char *value) ...@@ -50,7 +49,7 @@ static struct moberg_status tty_read(int fd, long timeout, unsigned char *value)
while (1) { while (1) {
pollfd.fd = fd; pollfd.fd = fd;
pollfd.events = POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR; pollfd.events = POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR;
int err = poll(&pollfd, 1, timeout); int err = poll(&pollfd, 1, timeout / 1000);
if (err >= 1) { if (err >= 1) {
break; break;
} else if (err == 0) { } else if (err == 0) {
...@@ -100,14 +99,11 @@ struct moberg_status serial2002_read(int f, long timeout, ...@@ -100,14 +99,11 @@ struct moberg_status serial2002_read(int f, long timeout,
} }
length++; length++;
if (data < 0) { if (length < 6 && data & 0x80) {
fprintf(stderr, "serial2002 error\n");
break;
} else if (length < 6 && data & 0x80) {
value->value = (value->value << 7) | (data & 0x7f); value->value = (value->value << 7) | (data & 0x7f);
} else if (length == 6 && data & 0x80) { } else if (length == 6 && data & 0x80) {
fprintf(stderr, "Too long\n");
value->kind = is_invalid; value->kind = is_invalid;
return MOBERG_ERRNO(EFBIG);
break; break;
} else { } else {
if (length == 1) { if (length == 1) {
...@@ -180,7 +176,6 @@ static double interpret_value(int value) ...@@ -180,7 +176,6 @@ static double interpret_value(int value)
result *= 1e-6; result *= 1e-6;
break; break;
} }
fprintf(stderr, "%f\n", result);
return result; return result;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment