Michael Mc Namara / Mbed 2 deprecated lab1

Dependencies:   mbed mbed-rtos C12832_lcd LCD_fonts

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // example to test the mbed Lab Board lcd lib with the mbed rtos
00002 // Pot1 change the contrast
00003 // Pot2 change the speed of the sin wave
00004 
00005 #include "mbed.h"
00006 #include "rtos.h"
00007 #include "Small_6.h"
00008 #include "Small_7.h"
00009 #include "Arial_9.h"
00010 #include "stdio.h"
00011 #include "C12832_lcd.h"
00012 
00013 // LCD object
00014 C12832_LCD LCD;
00015 
00016 AnalogIn Pot1(p19);
00017 AnalogIn Pot2(p20);
00018 
00019 //JOYSTICK
00020 BusIn joy(p15,p12,p13,p16);
00021 DigitalIn fire(p14);
00022 
00023 BusOut leds(LED1,LED2,LED3,LED4);
00024 
00025 
00026 // mutex to make the lcd lib thread save
00027 Mutex lcd_mutex;
00028 
00029 // Thread 1
00030 // print counter into first line and wait for 1 s
00031 void thread1(void const *args)
00032 {
00033     int i;
00034     while(true) {       // thread loop
00035         lcd_mutex.lock();
00036         LCD.locate(0,0);
00037         LCD.set_font((unsigned char*) Small_6);
00038         LCD.printf("Thread1 count: %d",i);
00039         lcd_mutex.unlock();
00040         i++;
00041         Thread::wait(1000);
00042     }
00043 }
00044 
00045 // Thread 2
00046 // print counter into third line and wait for 0,5s
00047 void thread2(void const *args)
00048 {
00049     int k;
00050     while(true) {       // thread loop
00051         lcd_mutex.lock();
00052         LCD.locate(0,20);
00053         LCD.set_font((unsigned char*) Arial_9);
00054         LCD.printf("Thread 2 count : %d",k);
00055         lcd_mutex.unlock();
00056         k++;
00057         Thread::wait(500); // wait 0.5s
00058     }
00059 }
00060 
00061 // Thread 3
00062 // print a sin function in a small window
00063 // the value of pot 1 change the speed of the sinwave
00064 void thread3(void const *args)
00065 {
00066     int i,k,v;
00067     double s,a;
00068     k = 1;
00069     lcd_mutex.lock();
00070     LCD.rect(89,0,127,17,1);
00071     lcd_mutex.unlock();
00072     while(true) {       // thread loop
00073         v = Pot1.read_u16();  // get value of pot 1
00074         lcd_mutex.lock();
00075         for (i=90; i<127; i++) {           
00076             s = 8 * sin((long double)(i+k) /5);   // pixel to print  
00077             a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase
00078             LCD.pixel(i,9 + (int)a ,0);           // erase pixel  
00079             LCD.pixel(i,9 + (int)s ,1);           // print pixel  
00080         }
00081         LCD.copy_to_lcd();  // LCD.pixel do not update the lcd 
00082         lcd_mutex.unlock();
00083         k++;
00084         Thread::wait(v/100);   // value of pot1 / 100
00085     }
00086 }
00087 
00088 // Thread 4
00089 // input pot 2 and change the contrast of lcd
00090 void thread4(void const *args)
00091 {
00092     int k;
00093     while(true) {         // thread loop
00094     k = Pot2.read_u16();  // get the value of poti 2
00095     k = k >> 10;          // we need only 6 bit for contrast
00096      lcd_mutex.lock();
00097     LCD.set_contrast(k);
00098      lcd_mutex.unlock();
00099     Thread::wait(500);    // wait 0.5s
00100 }
00101 }
00102 
00103 // Thread 5
00104 // input joystick and change LED
00105 void thread5(void const *args)
00106 
00107 {
00108     int main();
00109     while(1) {
00110         if (fire) {
00111             leds=0xf;
00112         } else {
00113             leds=joy;
00114         }
00115         wait(0.1);
00116     }
00117 }
00118 
00119 // print the actual contrast 
00120 int main()
00121 {
00122     int j;
00123     LCD.cls();
00124   
00125     Thread t1(thread1); //start thread1
00126     Thread t2(thread2); //start thread2
00127     Thread t3(thread3); //start thread3
00128     Thread t4(thread4); //start thread4
00129     Thread t5(thread5); //start thread5
00130     
00131     while(true) {       // main is the next thread
00132         lcd_mutex.lock();
00133         LCD.locate(0,9);
00134         LCD.set_font((unsigned char*) Small_7);
00135         j = LCD.get_contrast();    // read the actual contrast
00136         LCD.printf("contrast : %d",j);
00137         lcd_mutex.unlock();
00138         Thread::wait(500);   // wait 0.5s
00139     }
00140 
00141 }