In this, the third part of the Mail Light project, I describe how the core routines were implemented. The core routines revolve around a timer that flashed the LEDs, four timers that check for email at a certain rate, and the REALbasic objects that manage the pop connection.
The simplest component is the timer that goes out to get mail every couple of minutes. There is a timer for each of the four mail accounts. When the timer times out, it simply calls the pop3 object and tells it to connect:
After the connection is established, the pop3 object prints that status in the first status box. After it connects, then a login is attempted. When the login is successful, the number of messages that are on the server is counted:
The pop3 object counts the number of messages on the server and displays that number. If the number is 0, then a boolean variable is set to false. If there is 1 or more messages, then that variable is set. That variable determines that the LED will be blinking. After counting the number of messages, the pop3 object disconnects and clears the status text:
The timer that checks for mail doesn’t run until the connection is made for the fist time with the press of the connect button. When the button is pushed, the server, user name, and other data is passed to the pop3 object and a connection invoked. The timer is started (for next email checking time) and the button is changed from a connect button to a disconnect button:
If that same button is clicked after it has been toggled to be a disconnect button, then the pop3 object is disconnected from the server (if still connected), the timer is turned off, status cleared, the LED blinking is disabled, and the button again becomes a connect button:
This isn’t an email program – you don’t get to read the email that you have with this program – you are just notified that you have email waiting on your server. After you are notified that you have email, you would go get those emails with your email program. The LED will likely still be blinking after you get the email with the other program. Clicking the button to disconnect and reconnect will start another check and discover that your email had been cleared.
The LED blinking routine is actually the longest of all of these functions. When the blink timer times out, the USBmicro DLL stuff is defined. The value for the LED port of the U421 (port A) is set to 0 before accumulating the status of the email accounts:
The blink timer toggles the graphic indicator (and eventually the LED) on and off every other time that the timer times out. A boolean variable is flipped from true to false every other time. When false, the indicator is off. When true the indicator is on if there has been a message detected. And if the indicator is on, then LEDval is set to turn on a bit for turning on a specific LED:
The first LED on the front and the first LED on the back are turned on with the value of &H11. The 2nd LEDs with the value of &H22, the third with a value of &H44, the 4th with &H88.
Once these values are all combined, then the value is sent to port A ov the U421:
Download the REALbasic project and the executable code: MailLights Project and program.
To set this up for your email, edit the maildata.txt file to include your four (or fewer) email settings. When you run the program, these settings are loaded. You can leave the password as a blank line if you don’t like having a password in this file.
Click connect on all of the accounts that you will use to start the mail checking process. Click hide to hide the window to the tray icon. Recover the main window by double clicking on the tray icon.
When the program discovers an email in the accounts that it checks, the visual indicator will flash. If you have built the Mail Light hardware, then you will see flashing LEDS.
There is a lot of flexibility in the LEDs/lights that you can put together for this project. Originally I had planned to put a stack of ping pong balls on the top of my monitor to light up with some LEDs. This looked great in a dimly lit room and in the evening. But with my office window open, the ping pong balls just were not light enough against the open window to see from my lab.
Code could be changed to do more than just light LEDs. You could attach an LCD display to the U421 that would display, in text, the number of emails that are in each account. The U421 could directly drive a pair of seven-segment LED displays. You could use multiple U421 with a combination of hardware designs.
Enjoy this project. I have found that it is a very useful device just in the week that I have been using it on my PC. It will be a permanent part of my PC sertup now. If I make improvements to the code I will share it here. You are free to make improvements, suggest features, and port the code to other development environments. Or you can just build the hardware and use the software freely to drive your own Mail Light. Share pictures of your mail light and experiences building. 🙂
Leave a Reply