Skip to content

Instantly share code, notes, and snippets.

@mitchtech
Created July 1, 2012 17:51
Show Gist options
  • Save mitchtech/3029096 to your computer and use it in GitHub Desktop.
Save mitchtech/3029096 to your computer and use it in GitHub Desktop.
Arduino Physical CPU/Memory Gauges
#!/usr/bin/env python
import serial, sys, time, psutil
# Assign Arduino's serial port address
# Windows example
# usbport = 'COM3'
# Linux example
# usbport = '/dev/ttyUSB0'
# MacOSX example
# usbport = '/dev/tty.usbserial-FTALLOK2'
usbport = '/dev/ttyUSB0'
# Set up serial baud rate
ser = serial.Serial(usbport, 9600, timeout=1)
def move(servo, angle):
'''Moves the specified servo to the supplied angle.
Arguments:
servo
the servo number to command, an integer from 1-4
angle
the desired servo angle, an integer from 0 to 180
(e.g.) >>> servo.move(2, 90)
... # "move servo #2 to 90 degrees"'''
if (0 <= angle <= 180):
ser.write(chr(255))
ser.write(chr(servo))
ser.write(chr(angle))
else:
print "Servo angle must be an integer between 0 and 180.\n"
def main():
while 1:
cpu_percent = psutil.cpu_percent(interval=1, percpu=False)
mem_percent = psutil.phymem_usage().percent
cpu_angle = int(cpu_percent / 100 * 180)
mem_angle = int(mem_percent / 100 * 180)
cpu_str = "CPU: " + str(cpu_percent) + " % (" + str(cpu_angle) + "/180)"
mem_str = "Mem: " + str(mem_percent) + " % (" + str(mem_angle) + "/180)"
print cpu_str
print mem_str
move(0, cpu_angle)
move(1, mem_angle)
#time.sleep(1)
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment