Skip to content

Instantly share code, notes, and snippets.

@admalledd
Created February 7, 2011 10:02
Show Gist options
  • Save admalledd/814199 to your computer and use it in GitHub Desktop.
Save admalledd/814199 to your computer and use it in GitHub Desktop.
import serial
import time
import subprocess
def scale(toscale, fromr=[-1,1],tor=[0,100],invert=0):#scale any number in one range to another
#linier only right now. uses y=mx+b
x1=fromr[0]
x2=fromr[1]
y1=tor[0]
y2=tor[1]
m=((y2-y1)/(x2-x1))
b=((-m)*x1+y1)
ret=(m*toscale+b)
if invert ==1:
#((self.ax * -1) + 100)
ret=((ret*-1)+tor[1])
return ret
CMD=['mpstat', '1', '1']
def getcpu():
cpu=subprocess.Popen(CMD,stdout=subprocess.PIPE).stdout.read().split('\n')[-2].split()[2]
return float(cpu)
def main():
ard=serial.Serial('/dev/ttyUSB0')
ard.write('g')#g for go!
ard.flushInput()#got to clear the stream that started at the arduino
while True:
time.sleep(0.25)#got to give servo chance to move
f=getcpu()
full=scale(f,[0.0,100.0],[0,255],1)
print full,f
ard.write(chr(int(full)))
if __name__=='__main__':
main()
@admalledd
Copy link
Author

simple program to read and set my cpu dail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment