Open dComposer opened 3 years ago
I updated Chapter 6's bossButton.py for python 3. The only changes were to add the parenthesis to the print statements and to change the response logic from: if response = 'X' to: if response = b'X'
## Simple demo
## Sits forever listening to serial port
## When you press button, opens website of your choosing.
## Extend this to many buttons and you'll have a physical
## web-launcher.
BOSS_SITE = "http://www.cartalk.com/content/boss-redirect"
## or perhaps more topical...
XKCD = "http://xkcd.com/353/"
## list all serial ports being used: python -m serial.tools.list_ports
SERIAL_PORT = "/dev/cu.usbserial-14130"
BAUD_RATE = 9600
import serial
import webbrowser
sp = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout = 5)
sp.flush()
print("Boss Button")
while(1): # Sit and wait forever
response = sp.read(1) # get one byte
if response == b'O':
print("Got OK Byte. Waiting for button press.")
elif response == b'X':
print("Got Boss Byte! Alarm!")
webbrowser.open(BOSS_SITE)
else:
print("Got nothing. Still waiting.")
Chapter 7's serialScope.py updated for python 3:
import serial
def readValue(serialPort):
return(ord(serialPort.read(1)))
def plotValue(value):
""" Displays the value on a scaled scrolling bargraph"""
leadingSpaces = "-" * int(value*(SCREEN_WIDTH-3) / 255)
print(f"{leadingSpaces} {value:03}")
def cheapoScope(serialPort):
while(1):
newValue = readValue(serialPort)
plotValue(newValue)
if __name__ == "__main__":
## list all serial ports being used: python -m serial.tools.list_ports
PORT = '/dev/cu.usbserial-14230' # update to whatever port is listed in serial.tools.list_ports
BAUDRATE = 9600
TIMEOUT = None
SCREEN_WIDTH = 80
## Take command-line arguments to override defaults above
import sys
if len(sys.argv) == 3:
port = sys.argv[1]
baudrate = int(sys.argv[2])
else: # nothing passed, use defaults
print ("Optional arguments port, baudrate set to defaults.")
port, baudrate = (PORT, BAUDRATE)
serialPort = serial.Serial(port, baudrate, timeout=TIMEOUT)
serialPort.flush()
cheapoScope(serialPort)
Hiya! That's lovely! I'll roll those into the code.
(I haven't been doing as much maintenance on this codebase as I probably should. Thanks for helping out.)
My pleasure! As I convert more python code over I'll post them into this issue. Thanks for writing this great book!
I updated autoPlay.py (Chapter 5, serialOrgan) for python 3 (and skipped the serialorgansongs.jottit.com lines since that site no longer exists) and added some notes for figuring out the current serial port. Thanks so much for this wonderful book!