Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 PwmOut rled(D3); 00004 PwmOut gled(D5); 00005 PwmOut bled(D6); 00006 DigitalOut led_common(D4); 00007 00008 typedef struct { 00009 float red; 00010 float green; 00011 float blue; 00012 } message_t; 00013 00014 Mail <message_t,4> mbox; //Mailbox for 4 messages 00015 00016 void led_thread(void const *argument) { 00017 rled.period_ms(20); //Set period to 20 ms 00018 rled.write(1.0f); //Initialize to 0% duty cycle 00019 gled.period_ms(20); //Set period to 20 ms 00020 gled.write(1.0f); //Initialize to 0% duty cycle 00021 bled.period_ms(20); //Set period to 20 ms 00022 bled.write(1.0f); //Initialize to 0% duty cycle 00023 while (true) { 00024 osEvent evt = mbox.get(); //Wait for a message 00025 if(evt.status == osEventMail) { 00026 message_t *mymessage = (message_t*)evt.value.p; 00027 rled = 1.0f - mymessage->red; 00028 gled = 1.0f - mymessage->green; 00029 bled = 1.0f - mymessage->blue; 00030 mbox.free(mymessage); //Free up memory 00031 } 00032 } 00033 } 00034 00035 float frand(void) { 00036 int32_t rv = 0x8000 -(rand()&0xFFFF); 00037 return (rv*rv/1073741824.0f); 00038 } 00039 00040 int main (void) { 00041 float RGB1[3]; 00042 float RGB2[3]; 00043 float INC[3]; 00044 led_common = 1; // for commono anode... 00045 Thread thread2(led_thread); 00046 //--- Create a random color --------------------- 00047 for (int x=0; x<3; x++) { 00048 RGB1[x] = frand(); 00049 } 00050 00051 while (true) { 00052 //--- Create a new random color ----------------- 00053 for (int x=0; x<3; x++) { 00054 RGB2[x] = frand(); 00055 } 00056 //--- Determine increments to go from color 1 to color 2 in 25 steps 00057 for (int x=0; x<3; x++) { 00058 INC[x] = (RGB1[x] - RGB2[x]) / 25; 00059 } 00060 //--- Send color codes to thread2 --------------- 00061 for (int s=0; s<25; s++) { 00062 message_t *message = mbox.alloc(); //Allocate memory 00063 message->red = RGB1[0]; 00064 message->green = RGB1[1]; 00065 message->blue = RGB1[2]; 00066 mbox.put(message); //Send data as message 00067 Thread::wait(100); 00068 for (int x=0; x<3; x++) { 00069 RGB1[x] -= INC[x]; //Approach to second colour 00070 } 00071 } 00072 } 00073 }
Generated on Thu Jul 14 2022 17:02:25 by
1.7.2