View Single Post
Flandry's Avatar
Posts: 1,559 | Thanked: 1,786 times | Joined on Oct 2009 @ Boston
#57
It's a mess of commented-out code right now but if it doesn't work, that's a dead end.

It takes an initial time, polls the device file in a tight loop 60 times, takes the end time, and then divides the detected presses by the difference in time and prints it. Then it does it all over again. If it's missing presses in a tight loop, i don't think we can do any better using that resource. Back to the audio approach?

I'll copy and paste the snippet of code that actually does the relevant stuff when i get home from the lab.

Edit: Looking through that, i think i accidentally uploaded a version that calls nanosleep, so it might not be optimal. Try the version attached to see if it's any better.



Code:
#include <sys/time.h>
/* Headset button (AKA sensor) device file */
static const char *button_filename = "/sys/devices/platform/nokia-av/detect";

int read_button()
{
  FILE *fd;
  char button = 0;

  fd = fopen(button_filename, "r");
  button = fgetc(fd); // 4 is open, 2 (or 0 for hacked wiring) is closed
  fclose(fd);
  if (button == '4') return 0;
  else if (button == '2' || button == '0') return 1;
  else return -1;
}

int do_poll(int samples)
{
    int presses = 0, button_status, old_button_status = 0; // WARNING: Assuming button is open at start could lead to overcounting presses by 1/frame
    for (int i=samples; i > 0; i--)
    {
      button_status = read_button();
      if (!old_button_status && button_status > 0)  presses++; // No error checking
      old_button_status = button_status;
    }
    return presses;

int main(int argc, char **argv)
{
  struct timeval start_poll, end_poll;
  float elapsed_time;
  while(1) { // Begin main loop. This only exits if a trigger file that was specified is deleted.
    gettimeofday( &start_poll, NULL );
    presses = do_poll( 60 );
    gettimeofday( &end_poll, NULL );
    elapsed_time = (float)(end_poll.tv_usec - start_poll.tv_usec)/1000000.0;
    elapsed_time += end_poll.tv_sec - start_poll.tv_sec;
    printf("Revolutions: %i Elapsed: %0.2fs Hertz: %0.2f Approx. speed: %0.1fm/s\n", presses, elapsed_time, (float)presses/elapsed_time, presses*2.0/elapsed_time);
  } // End main loop
  return 0;
}
Attached Files
File Type: deb maebike_0.0.1_armel.deb (6.4 KB, 80 views)
__________________

Unofficial PR1.3/Meego 1.1 FAQ

***
Classic example of arbitrary Nokia decision making. Couldn't just fallback to the no brainer of tagging with lat/lon if network isn't accessible, could you Nokia?
MAME: an arcade in your pocket
Accelemymote: make your accelerometer more joy-ful

Last edited by Flandry; 2010-06-12 at 21:01. Reason: Snippet added