Debounce digital inputs
r1387 | dcl | 2007-01-09 11:15:02 +1100 (Tue, 09 Jan 2007) | 2 lines
This commit is contained in:
@@ -43,6 +43,7 @@ void device_write(DEVICE *device, int n, const char* tp)
|
||||
unsigned long value;
|
||||
|
||||
value = strtol(tp, NULL, 16);
|
||||
device->out_value = value;
|
||||
hware_write(device->private_data, value);
|
||||
return;
|
||||
}
|
||||
@@ -54,6 +55,13 @@ void device_print(DEVICE* device, FILE* fd)
|
||||
device->value);
|
||||
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)
|
||||
{
|
||||
@@ -93,16 +101,18 @@ int device_init(DEVICE** cpp, char* name)
|
||||
*cpp = device;
|
||||
memset(device, 0, sizeof(DEVICE));
|
||||
strncpy(device->name, name, sizeof(device->name));
|
||||
device->params.poll_period = 1000; /* milliseconds between polls */
|
||||
device->params.sample_period = 10; /* polls between sample calcs */
|
||||
device->params.report_period = 3; /* samples between reports */
|
||||
device->params.poll_period = 100; /* milliseconds between polls */
|
||||
device->params.sample_period = 5; /* polls between sample calcs */
|
||||
device->params.report_period = 10; /* samples between reports */
|
||||
device->state = device_stopped;
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
device->current_time = now;
|
||||
device->previous_time = now;
|
||||
device->sample_timer = now;
|
||||
device->report_timer = now;
|
||||
HWARE_TEST(hware_ctor(name, &device->private_data));
|
||||
device_sample(device);
|
||||
return 0;
|
||||
Error:
|
||||
hware_errmsg(errBuff, sizeof(errBuff));
|
||||
@@ -152,19 +162,34 @@ int device_poll(DEVICE* device)
|
||||
dbg_printf(0, "device_poll = 0x%06X @ %s\n",
|
||||
current_local_value,
|
||||
make_timestamp(&device->current_time));
|
||||
if (current_local_value != device->value)
|
||||
|
||||
if ((current_local_value != device->value)
|
||||
|| (current_local_value != device->new_value))
|
||||
{
|
||||
char str[100];
|
||||
snprintf(str, sizeof(str), "changed 0x%06X 0x%06X",
|
||||
current_local_value,
|
||||
device->value);
|
||||
device_event(device, str);
|
||||
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];
|
||||
snprintf(str, sizeof(str), "changed 0x%06X 0x%06X",
|
||||
current_local_value,
|
||||
device->value);
|
||||
device_event(device, str);
|
||||
device->value = current_local_value;
|
||||
}
|
||||
}
|
||||
device->value = current_local_value;
|
||||
/* check if it is time to roll the report */
|
||||
if (device_time_to_next_report(device) <= 0)
|
||||
/* check if it is time to roll the sample */
|
||||
if (device_time_to_next_sample(device) <= 0)
|
||||
{
|
||||
device_report(device);
|
||||
/* check if it is time to roll the report */
|
||||
if (device_time_to_next_report(device) <= 0)
|
||||
{
|
||||
device_report(device);
|
||||
}
|
||||
device_sample(device);
|
||||
}
|
||||
|
||||
return error;
|
||||
@@ -213,3 +238,21 @@ double device_time_to_next_report(DEVICE* device)
|
||||
timeout = timeout - timeofday % 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user