From 4a8fe2911f535b4024decf9e8cde341fb74bc057 Mon Sep 17 00:00:00 2001 From: Phyks Date: Mon, 30 Sep 2013 22:16:17 +0200 Subject: [PATCH] Bug correction in color.py Computed value could be outside range [0, 255]. Now, it won't. --- color.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 45 --------------------------------------------- 2 files changed, 51 insertions(+), 45 deletions(-) create mode 100755 color.py delete mode 100755 main.py diff --git a/color.py b/color.py new file mode 100755 index 0000000..932cf81 --- /dev/null +++ b/color.py @@ -0,0 +1,51 @@ +#!/bin/env python + +import serial +import pygame + +ser = serial.Serial("/dev/ttyACM0", 115200) +pygame.init() + +size = width, height = 640, 480 +screen = pygame.display.set_mode(size) +screen.fill((0, 0, 0)) + +try: + ser.open() +except Exception: + print("Error while opening serial port.") + +if ser.isOpen(): + try: + ser.flushInput() + ser.flushOutput() + + maximum = -1 + minimum = -1 + + print("Calibration :") + 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()) + valueR = int(255*(line-minimum)/(maximum-minimum)) + if valueR < 0: + valueR = 0 + if valueR > 255: + valueR = 255 + + screen.fill((valueR, 0, 0)) + pygame.display.flip() + + ser.close() + except Exception: + print("Error while fetching data from serial.") diff --git a/main.py b/main.py deleted file mode 100755 index b2ab10d..0000000 --- a/main.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/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()