commit 92d85d46e83db1ccfb2e904819e17a1f947fde16 Author: Phyks Date: Mon Sep 30 16:22:47 2013 +0200 Initial commit : 1 electrode diff --git a/main.py b/main.py new file mode 100755 index 0000000..b2ab10d --- /dev/null +++ b/main.py @@ -0,0 +1,45 @@ +#!/bin/env python + +import serial +import pygame + +ser = serial.Serial("/dev/ttyACM0", 115200) +pygame.init() + +size = width, height = 640, 480 +black = 0, 0, 0 +screen = pygame.display.set_mode(size) +background = pygame.Surface(screen.get_size()) +background = background.convert() +background.fill(black) + +try: + ser.open() +except Exception, e: + print("Error while opening serial port : "+str(e)) + +if ser.isOpen(): + ser.flushInput() + ser.flushOutput() + + maximum = -1. + minimum = -1. + print("Calibration") + screen.blit(background, (0,0)) + pygame.display.flip() + for i in range(50) : + line = float(ser.readline()) + + if line < minimum or minimum < 0: + minimum = line + if line > maximum: + maximum = line + + print("Go") + while True: + line = float(ser.readline()) + background.fill((int(255.*(line-minimum)/(maximum-minimum)), 0, 0)) + screen.blit(background, (0,0)) + pygame.display.flip() + + ser.close() diff --git a/touchless_tracking/touchless_tracking.ino b/touchless_tracking/touchless_tracking.ino new file mode 100644 index 0000000..b3e08bc --- /dev/null +++ b/touchless_tracking/touchless_tracking.ino @@ -0,0 +1,58 @@ +#define resolution 8 +#define mains 50 // 60: north america, japan; 50: most other places + +#define refresh 2 * 1000000 / mains + +// Counter for the timer +extern volatile unsigned long timer0_overflow_count; + +/* === */ +/* ??? */ +void startTimer() { + timer0_overflow_count = 0; + TCNT0 = 0; +} + +unsigned long checkTimer() { + return ((timer0_overflow_count << 8) + TCNT0) << 2; +} + +long time(int pin, byte mask) { + unsigned long count = 0, total = 0; + + while(checkTimer() < refresh) { + // pinMode is about 6 times slower than assigning + // DDRB directly, but that pause is important + pinMode(pin, OUTPUT); + PORTB = 0; + + pinMode(pin, INPUT); + + while((PINB & mask) == 0) + count++; + + total++; + } + startTimer(); + return (count << resolution) / total; +} + +/* ??? */ +/* === */ + +void setup() { + // Initialize serial communication + Serial.begin(115200); + + // INPUT on pin 8 + pinMode(8, INPUT); + + // Start timer + startTimer(); +} + +void loop() { + // Print output to serial in decimal + Serial.print(time(8, B00000001), DEC); + Serial.print(" "); +}