Will asked about the code I was using in my ambient email notifier. The full code is a bit difficult to figure out because I've got it tied to a system tray icon thingi, which can go in another post another day, but here are some relevant bits.
First, you can get the number of emails with a particular label in gmail with the following Python code:
import feedparser
def msgCount(uid, pwd, filter):
inbox = feedparser.parse("https://%s:%s@gmail.google.com/gmail/feed/atom%s" % (uid, pwd, filter))
return len(inbox["entries"])
uid is your gmail address without the @gmail.com bit, filter is "" for the inbox, and "/label/" to get messages tagged with a particular label.
So, after calling msgCount a few times, for different labels, I compute the colour of the RGB LED:
colour = (inbox > 0 and 1 or 0) + (news > 0 and 2 or 0) + (work > 0 and 4 or 0)
This is sent over the serial port to the picaxe which decodes bit 0 for blue, 1 for green and 2 for red.
def triggerAmbient(colour):
com = serial.Serial("COM3", 2400, timeout=0.25)
for attempt in range(0,10):
com.write("%c" % (colour) )
com.close()
It tries to send a few times in case the picaxe doesn't get it the first time. The code on the picaxe just listens for a byte on the serial input and outputs the lowest 4 bits to the output pins:
main:
serout 0, n2400, ("Ok")
serin 3, n2400, b0
gosub nibble3
b0 = b0 & 7
serout 0, n2400, (#b0)
goto main
nibble3:
if bit2 = 1 then
high 1
else
low 1
endif
if bit1 = 1 then
high 2
else
low 2
endif
if bit0 = 1 then
high 4
else
low 4
endif
return
This means the python script has control over the colour and you can test that it's working by simply opening up a terminal on COM3 and typing away (A is 01000001 in ASCII, meaning pin 1 is switched on, B is 01000010 so pin 2 is on, etc.)
Update: I've detailed the changes I made to get this working under Linux.









