From 3d6508c9b9505cb3393b7c12df1a0c87f7828bbe Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Fri, 18 Dec 2009 04:40:15 +0000 Subject: Enhance the HotmailNotifier project so that the LEDs' individual brightness can be controlled individually via software PWM. --- Projects/HotmailNotifier/HotmailNotifier.c | 64 +++++++++++++++++++--- .../HotmailNotifier/WindowsApp/MailNotifier.cs | 44 ++++++++++----- 2 files changed, 84 insertions(+), 24 deletions(-) (limited to 'Projects') diff --git a/Projects/HotmailNotifier/HotmailNotifier.c b/Projects/HotmailNotifier/HotmailNotifier.c index 3d467e54a..5a7ebd563 100644 --- a/Projects/HotmailNotifier/HotmailNotifier.c +++ b/Projects/HotmailNotifier/HotmailNotifier.c @@ -60,6 +60,39 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = }, }; +/** Counter for the software PWM */ +static volatile uint8_t SoftPWM_Count; + +/** Duty cycle for the first software PWM channel */ +static volatile uint8_t SoftPWM_Channel1_Duty; + +/** Duty cycle for the second software PWM channel */ +static volatile uint8_t SoftPWM_Channel2_Duty; + +/** Duty cycle for the third software PWM channel */ +static volatile uint8_t SoftPWM_Channel3_Duty; + + +/** Interrupt handler for managing the software PWM channels for the LEDs */ +ISR(TIMER0_COMPA_vect, ISR_BLOCK) +{ + uint8_t LEDMask = LEDS_ALL_LEDS; + + if (++SoftPWM_Count == 0x1F) + SoftPWM_Count = 0; + + if (SoftPWM_Count >= SoftPWM_Channel1_Duty) + LEDMask &= ~LEDS_LED1; + + if (SoftPWM_Count >= SoftPWM_Channel2_Duty) + LEDMask &= ~LEDS_LED2; + + if (SoftPWM_Count >= SoftPWM_Channel3_Duty) + LEDMask &= ~LEDS_LED3; + + LEDs_SetAllLEDs(LEDMask); +} + /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be * used like any regular character stream in the C APIs */ @@ -77,11 +110,21 @@ int main(void) for (;;) { - /* Read next character - if a '1' turn on red led, if a '0' turn on green LED */ - if (fgetc(&USBSerialStream) == '1') - LEDs_SetAllLEDs(LEDS_LED3); - else - LEDs_SetAllLEDs(LEDS_LED2); + /* Read in next LED colour command from the host */ + uint8_t ColorUpdate = fgetc(&USBSerialStream); + + /* Top 3 bits select the LED, bottom three control the brightness */ + uint8_t Channel = (ColorUpdate & 0b11100000); + uint8_t Duty = (ColorUpdate & 0b00011111); + + if (Channel & (1 << 5)) + SoftPWM_Channel1_Duty = Duty; + + if (Channel & (1 << 6)) + SoftPWM_Channel2_Duty = Duty; + + if (Channel & (1 << 7)) + SoftPWM_Channel3_Duty = Duty; CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); @@ -101,15 +144,18 @@ void SetupHardware(void) /* Hardware Initialization */ LEDs_Init(); USB_Init(); + + /* Timer Initialization */ + OCR0A = 100; + TCCR0A = (1 << WGM01); + TCCR0B = (1 << CS00); + TIMSK0 = (1 << OCIE0A); } /** Event handler for the library USB Configuration Changed event. */ void EVENT_USB_Device_ConfigurationChanged(void) { - LEDs_SetAllLEDs(LEDS_ALL_LEDS); - - if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface))) - LEDs_SetAllLEDs(LEDS_LED1 | LEDS_LED3); + CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); } /** Event handler for the library USB Unhandled Control Request event. */ diff --git a/Projects/HotmailNotifier/WindowsApp/MailNotifier.cs b/Projects/HotmailNotifier/WindowsApp/MailNotifier.cs index d7f051687..742948fca 100644 --- a/Projects/HotmailNotifier/WindowsApp/MailNotifier.cs +++ b/Projects/HotmailNotifier/WindowsApp/MailNotifier.cs @@ -12,18 +12,20 @@ namespace TestWinForms public partial class MailNotifier : Form { private MessengerAPI.Messenger Messenger; - private RegistryKey AppRegKey; + private RegistryKey AppRegKey; + + private const int LIGHT_MAX = 0x1F; public MailNotifier() { InitializeComponent(); Messenger = new MessengerAPI.Messenger(); - AppRegKey = Registry.CurrentUser.CreateSubKey("Software\\MailNotifier"); + AppRegKey = Registry.CurrentUser.CreateSubKey("Software\\MailNotifier"); for (int i = 1; i < 99; i++) cmbComPort.Items.Add("COM" + i.ToString()); - + cmbComPort.SelectedIndex = System.Convert.ToInt32(AppRegKey.GetValue("Port", "1")) - 1; serSerialPort.PortName = cmbComPort.Text; @@ -34,7 +36,9 @@ namespace TestWinForms private void MailNotifier_Load(object sender, EventArgs e) { Messenger.OnUnreadEmailChange += new MessengerAPI.DMessengerEvents_OnUnreadEmailChangeEventHandler(NewEmail); - NotifyLight(Messenger.get_UnreadEmailCount(MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX) > 0); + + bool UnreadMail = (Messenger.get_UnreadEmailCount(MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX) > 0); + NotifyLight((!UnreadMail ? LIGHT_MAX : 0), (UnreadMail ? LIGHT_MAX : 0), 0); Hide(); } @@ -48,18 +52,23 @@ namespace TestWinForms private void NewEmail(MessengerAPI.MUAFOLDER folder, int amount, ref bool enableDefault) { if (folder == MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX) - NotifyLight(amount > 0); + { + bool UnreadMail = (Messenger.get_UnreadEmailCount(MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX) > 0); + NotifyLight((!UnreadMail ? LIGHT_MAX : 0), (UnreadMail ? LIGHT_MAX : 0), 0); + } } - private void NotifyLight(bool ShowGreen) + private void NotifyLight(int Red, int Green, int Blue) { - char[] buffer = new char[1]; - buffer[0] = ShowGreen ? '0' : '1'; + byte[] buffer = new byte[3]; + buffer[0] = (byte)(0x80 | (Red & LIGHT_MAX)); + buffer[1] = (byte)(0x40 | (Green & LIGHT_MAX)); + buffer[2] = (byte)(0x20 | (Blue & LIGHT_MAX)); try { serSerialPort.Open(); - serSerialPort.Write(buffer, 0, 1); + serSerialPort.Write(buffer, 0, buffer.Length); serSerialPort.Close(); } catch (Exception e) @@ -78,15 +87,20 @@ namespace TestWinForms AppRegKey.SetValue("Port", cmbComPort.SelectedIndex + 1); serSerialPort.PortName = cmbComPort.Text; - for (int i = 0; i < 5; i++) + for (int i = 1; i < 10; i++) + { + NotifyLight((LIGHT_MAX / i), (LIGHT_MAX / (i * 10)), 0); + System.Threading.Thread.Sleep(10); + } + + for (int i = 10; i > 0; i--) { - NotifyLight(true); - System.Threading.Thread.Sleep(20); - NotifyLight(false); - System.Threading.Thread.Sleep(20); + NotifyLight((LIGHT_MAX / i), (LIGHT_MAX / (i * 10)), 0); + System.Threading.Thread.Sleep(10); } - NotifyLight(Messenger.get_UnreadEmailCount(MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX) > 0); + bool UnreadMail = (Messenger.get_UnreadEmailCount(MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX) > 0); + NotifyLight((!UnreadMail ? LIGHT_MAX : 0), (UnreadMail ? LIGHT_MAX : 0), 0); } private void btnMinimize_Click(object sender, EventArgs e) -- cgit v1.2.3