Debounce digital inputs

r1387 | dcl | 2007-01-09 11:15:02 +1100 (Tue, 09 Jan 2007) | 2 lines
This commit is contained in:
Douglas Clowes
2007-01-09 11:15:02 +11:00
parent d45ba66ae1
commit 9ca1b3199a
2 changed files with 64 additions and 13 deletions

View File

@@ -43,6 +43,7 @@ void device_write(DEVICE *device, int n, const char* tp)
unsigned long value; unsigned long value;
value = strtol(tp, NULL, 16); value = strtol(tp, NULL, 16);
device->out_value = value;
hware_write(device->private_data, value); hware_write(device->private_data, value);
return; return;
} }
@@ -54,6 +55,13 @@ void device_print(DEVICE* device, FILE* fd)
device->value); device->value);
fflush(fd); fflush(fd);
} }
/*
* Finalise the current sample and move on to the next
*/
void device_sample(DEVICE* device)
{
device->sample_timer = device->current_time;
}
void device_report(DEVICE* device) void device_report(DEVICE* device)
{ {
@@ -93,16 +101,18 @@ int device_init(DEVICE** cpp, char* name)
*cpp = device; *cpp = device;
memset(device, 0, sizeof(DEVICE)); memset(device, 0, sizeof(DEVICE));
strncpy(device->name, name, sizeof(device->name)); strncpy(device->name, name, sizeof(device->name));
device->params.poll_period = 1000; /* milliseconds between polls */ device->params.poll_period = 100; /* milliseconds between polls */
device->params.sample_period = 10; /* polls between sample calcs */ device->params.sample_period = 5; /* polls between sample calcs */
device->params.report_period = 3; /* samples between reports */ device->params.report_period = 10; /* samples between reports */
device->state = device_stopped; device->state = device_stopped;
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
device->current_time = now; device->current_time = now;
device->previous_time = now; device->previous_time = now;
device->sample_timer = now;
device->report_timer = now; device->report_timer = now;
HWARE_TEST(hware_ctor(name, &device->private_data)); HWARE_TEST(hware_ctor(name, &device->private_data));
device_sample(device);
return 0; return 0;
Error: Error:
hware_errmsg(errBuff, sizeof(errBuff)); hware_errmsg(errBuff, sizeof(errBuff));
@@ -152,20 +162,35 @@ int device_poll(DEVICE* device)
dbg_printf(0, "device_poll = 0x%06X @ %s\n", dbg_printf(0, "device_poll = 0x%06X @ %s\n",
current_local_value, current_local_value,
make_timestamp(&device->current_time)); make_timestamp(&device->current_time));
if (current_local_value != device->value)
if ((current_local_value != device->value)
|| (current_local_value != device->new_value))
{
if (current_local_value != device->new_value)
{
device->new_value = current_local_value;
device->new_count = device->params.sample_period;
}
else if (--device->new_count <= 0)
{ {
char str[100]; char str[100];
snprintf(str, sizeof(str), "changed 0x%06X 0x%06X", snprintf(str, sizeof(str), "changed 0x%06X 0x%06X",
current_local_value, current_local_value,
device->value); device->value);
device_event(device, str); device_event(device, str);
}
device->value = current_local_value; device->value = current_local_value;
}
}
/* check if it is time to roll the sample */
if (device_time_to_next_sample(device) <= 0)
{
/* check if it is time to roll the report */ /* check if it is time to roll the report */
if (device_time_to_next_report(device) <= 0) if (device_time_to_next_report(device) <= 0)
{ {
device_report(device); device_report(device);
} }
device_sample(device);
}
return error; return error;
Error: Error:
@@ -213,3 +238,21 @@ double device_time_to_next_report(DEVICE* device)
timeout = timeout - timeofday % timeout; timeout = timeout - timeofday % timeout;
return 0.001 * timeout; return 0.001 * timeout;
} }
double device_time_to_next_sample(DEVICE* device)
{
uint64 last_sample;
uint64 timeofday;
int timeout;
struct timeval now;
now = device->current_time;
last_sample = 1000 * (uint64) device->sample_timer.tv_sec;
last_sample += (uint64) device->sample_timer.tv_usec / 1000;
timeofday = 1000 * (uint64) now.tv_sec;
timeofday += (uint64) now.tv_usec / 1000;
timeout = device->params.poll_period * device->params.sample_period;
if ((last_sample / timeout) != (timeofday / timeout))
return 0.0;
timeout = timeout - timeofday % timeout;
return 0.001 * timeout;
}

View File

@@ -28,12 +28,20 @@ typedef struct device_t
struct timeval current_time; struct timeval current_time;
/** time of last read */ /** time of last read */
struct timeval previous_time; struct timeval previous_time;
/** time of next sample closure */
struct timeval sample_timer;
/** time of next report generation */ /** time of next report generation */
struct timeval report_timer; struct timeval report_timer;
/** number of polls */ /** number of polls */
int poll_counter; int poll_counter;
/** output value */
unsigned int out_value;
/** physical device value */ /** physical device value */
unsigned int value; unsigned int value;
/** tentative new value */
unsigned int new_value;
/** debounce counter */
int new_count;
/** Control parameters */ /** Control parameters */
PARAMETERS params; PARAMETERS params;
struct device_private_t* private_data; struct device_private_t* private_data;