Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1393 Location: Loughborough
|
Posted: Sun Jun 26, 2011 11:07 pm Post subject: A Kids Level Python Project I Am Working On. |
|
|
Hi all...
Here is the preliminary of a Data-Logger/Transient-Recorder project for
kids that I am working on. It uses the Arduino Diecimila Dev Board and
works in Linux, Windows, Classic AMIGA and WinUAE from Python
1.4.0 to Python 2.6.6.
I haven't got Python 2.7.x so if anyone is able to test for that then I
would be grateful.
It runs in DEMO mode but to enable the Arduino access just change
the line demo=1 to demo=0 and ensure that the correct serial port
devices are changed too. The device numbers are for my setups only.
Watch for any wordwrapping etc...
| Code: |
# DataLogger.py
#
# (C)2011, B.Walker, G0LCU.
#
# A kids level project using the Arduino Diecimila and other related boards as an
# analogue input for use as a Data Logger. The accuarcy is 8 bit depth but the
# display is only 4 bit depth due to the limitations of text mode.
#
# This is a feeder upload only, it is no where near completion.
#
# This code defaults to DEMO mode but the crucial Arduino code is added and just
# requires the line "demo=0" to access the board and the correct device number to
# be changed. Later versions of Arduino now use /dev/ttyACMx in Linux and I have
# not got one to test with, so if any of you guys are willing to experiment by
# changing the Linux device type and number and let me know how you get on with
# it, then thanks.
#
# The test *.pde file for the Arduino Diecimila Board is here:-
#
# http://code.activestate.com/recipes/577625-arduino-diecimila-board-access-inside-winuae-demo/?in=user-4177147
#
# Tested on Windows XP using Python 2.4.2, Vista using Python 2.6.1, PCLinuxOS 2009 using
# Python 2.5.2, Debian Linux using Python 2.6.6, stock AMIGA A1200 using Python 1.4.0 and
# WinUAE with standard AMIGA OS 3.1 using Python 2.0.1.
# DEMO mode ONLY works in E-UAE with standard AMIGA OS 3.1 and Python 1.4.0 or Python 2.0.1.
#
# NOTE:- The Linux platforms assume running Python from a root default Terminal.
import sys
import os
import random
import time
def main():
global LoggerScreen
global LoggerWindow
global savefile
global plot
global position
global horiz
global demo
global pause
global pausestring
global serialport
global pointer
global mybyte
global grab
# global n
LoggerScreen="(C)2011, B.Walker, G0LCU."
LoggerWindow="Data_Logger-Transient_Recorder."
savefile="/tmp/LoggerStartup.txt"
demo=1
plot=0
horiz=1
position=79
pause=1
pausestring="1"
# The latest device name for current Arduino variants, (01-01-2011).
serialport="/dev/ttyACM0"
pointer=" "
mybyte=" "
grab=255
# n=0
if sys.platform=="amiga":
# The AMIGA serial port may need to be changed to 1200 baud, no parity,
# 8 bit data and 1 stop bit, this applies to WinUAE too.
serialport="SER:"
if sys.platform=="linux2":
# Assumed running from root for the time being.
# /dev/ttyUSB0 the device on my test systems, the Arduino Diecimila Board.
# It may need to be changed for your needs.
serialport="/dev/ttyUSB0"
os.system("chmod 666 "+serialport)
os.system("stty -F "+serialport+" 1200")
os.system("stty -F "+serialport+" raw")
if sys.platform=="win32":
# This is the COM port number generated on a test system.
# It may need to be changed for your needs.
serialport="COM3:"
os.system("MODE "+serialport+" BAUD=1200 PARITY=N DATA=8 STOP=1 to=on")
def clrscn():
if sys.platform=="amiga": print "\f",
if sys.platform=="linux2": print os.system("clear"),chr(13)," ",chr(13),
if sys.platform=="win32": print os.system("CLS"),chr(13)," ",chr(13),
def startscreen():
global savefile
if sys.platform=="amiga": savefile="S:LoggerStartup.txt"
if sys.platform=="linux2": savefile="/tmp/LoggerStartup.txt"
if sys.platform=="win32": savefile="C:\\Windows\\Temp\\LoggerStartup.txt"
def savescreen():
global LoggerWindow
global savefile
global LoggerScreen
LoggerWindow=open(savefile,"wb+")
LoggerWindow.write(LoggerScreen)
LoggerWindow.close()
def doplot():
global horiz
global position
global savefile
global LoggerWindow
global LoggerScreen
global demo
global pause
global pausestring
global plot
global pointer
global mybyte
global serialport
global grab
horiz=1
while horiz<=64:
# Generate a byte as though grabbed from Arduino.
if demo==1: grab=int(random.random()*256)
# Generate a byte from Arduino.
if demo==0:
pointer=open(serialport,"rb",2)
mybyte=str(pointer.read(1))
pointer.close()
# Convert to a decimal value.
grab=ord(mybyte)
# Convert to 4 bit depth.
plot=int(grab/16)
# Invert to suit the text display window.
plot=15-plot
if plot<=0: plot=0
if plot>=15: plot=15
# Set up the plot position per grab.
position=79+horiz+plot*79
LoggerWindow=open(savefile,"rb+")
LoggerWindow.seek(position)
LoggerWindow.write("o")
# Now get the whole array.
LoggerWindow.seek(0)
LoggerScreen=LoggerWindow.read(1659)
LoggerWindow.close()
# End of screen array update per plot.
# Wait for a period for none AMIGA platforms.
if sys.platform!="amiga": time.sleep(pause)
# time.sleep() does NOT work on an A1200, WinUAE and E-UAE so pause......
if sys.platform=="amiga":
pausestring=str(pause)
os.system("C:Wait "+pausestring)
# ......and then do a clear screen.
print "\f",
# Do a clear screen for other platforms.
if sys.platform=="linux2": print os.system("clear"),chr(13)," ",chr(13),
if sys.platform=="win32": print os.system("CLS"),chr(13)," ",chr(13),
# Now print the whole on screen...
print LoggerScreen+"Real 8 bit decimal value is "+str(grab)+"; displayed 4 bit decimal value is "+str(15-plot)+"..."
horiz=horiz+1
while 1:
# Set up DataLogger screen, use "\r\n" to suit Windows, "\r" is *ignored* on Linux and AMIGA_OS.
# This is for the default Command Prompt, (Windows), Terminal, (Linux) and CLI, (AMIGA), modes.
LoggerScreen="+-------+-------+-------+-------+-------+-------+-------+--------+ +--------+\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | |>(R)UN |\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | +--------+\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | +--------+\r\n"
LoggerScreen=LoggerScreen+"+-------+-------+-------+-------+-------+-------+-------+--------+ | Ctrl-C |\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | +--------+\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | +--------+\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | | (K)B |\r\n"
LoggerScreen=LoggerScreen+"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-++ +--------+\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | +--------+\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | | (S)LOW |\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | +---------\r\n"
LoggerScreen=LoggerScreen+"+-------+-------+-------+-------+-------+-------+-------+--------+ +--------+\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | | 1(0)S |\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | +--------+\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | +--------+\r\n"
LoggerScreen=LoggerScreen+"| | | | + | | | | |>(1)S |\r\n"
LoggerScreen=LoggerScreen+"+-------+-------+-------+-------+-------+-------+-------+--------+ +--------+\r\n"
LoggerScreen=LoggerScreen+"+----------------------------------------------------------------+ +--------+\r\n"
LoggerScreen=LoggerScreen+"| Status:- Running in DEMO mode. | | (U)NCAL|\r\n"
LoggerScreen=LoggerScreen+"+----------------------------------------------------------------+ +--------+\r\n"
# Save the startscreen to write to.
startscreen()
savescreen()
clrscn()
print LoggerScreen
doplot()
main()
# DataLogger program end.
# Enjoy finding simple solutions to often very difficult problems.
|
_________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|