Initial commit : 1 electrode

This commit is contained in:
Phyks 2013-09-30 16:22:47 +02:00
commit 92d85d46e8
2 changed files with 103 additions and 0 deletions

45
main.py Executable file
View File

@ -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()

View File

@ -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(" ");
}