Using Threads to control LCD display and LEDS

Dependencies:   mbed mbed-rtos C12832_lcd LCD_fonts

Committer:
T00209563
Date:
Sat Aug 01 15:47:24 2020 +0000
Revision:
4:3202052e5a8b
Using Threads to control LCD display and LEDS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
T00209563 4:3202052e5a8b 1 // example to test the mbed Lab Board lcd lib with the mbed rtos
T00209563 4:3202052e5a8b 2 // Pot1 change the contrast
T00209563 4:3202052e5a8b 3 // Pot2 change the speed of the sin wave
T00209563 4:3202052e5a8b 4
T00209563 4:3202052e5a8b 5 #include "mbed.h"
T00209563 4:3202052e5a8b 6 #include "rtos.h"
T00209563 4:3202052e5a8b 7 #include "Small_6.h"
T00209563 4:3202052e5a8b 8 #include "Small_7.h"
T00209563 4:3202052e5a8b 9 #include "Arial_9.h"
T00209563 4:3202052e5a8b 10 #include "stdio.h"
T00209563 4:3202052e5a8b 11 #include "C12832_lcd.h"
T00209563 4:3202052e5a8b 12
T00209563 4:3202052e5a8b 13 // LCD object
T00209563 4:3202052e5a8b 14 C12832_LCD LCD;
T00209563 4:3202052e5a8b 15
T00209563 4:3202052e5a8b 16 AnalogIn Pot1(p19);
T00209563 4:3202052e5a8b 17 AnalogIn Pot2(p20);
T00209563 4:3202052e5a8b 18 BusIn joy(p15,p12,p13,p16);
T00209563 4:3202052e5a8b 19 DigitalIn fire(p14);
T00209563 4:3202052e5a8b 20
T00209563 4:3202052e5a8b 21 BusOut leds(LED1,LED2,LED3,LED4);
T00209563 4:3202052e5a8b 22
T00209563 4:3202052e5a8b 23
T00209563 4:3202052e5a8b 24 // mutex to make the lcd lib thread save
T00209563 4:3202052e5a8b 25 Mutex lcd_mutex;
T00209563 4:3202052e5a8b 26
T00209563 4:3202052e5a8b 27 // Thread 1
T00209563 4:3202052e5a8b 28 // print counter into first line and wait for 1 s
T00209563 4:3202052e5a8b 29 void thread1(void const *args)
T00209563 4:3202052e5a8b 30 {
T00209563 4:3202052e5a8b 31 int i;
T00209563 4:3202052e5a8b 32 while(true) { // thread loop
T00209563 4:3202052e5a8b 33 lcd_mutex.lock();
T00209563 4:3202052e5a8b 34 LCD.locate(0,0);
T00209563 4:3202052e5a8b 35 LCD.set_font((unsigned char*) Small_6);
T00209563 4:3202052e5a8b 36 LCD.printf("Thread1 count: %d",i);
T00209563 4:3202052e5a8b 37 lcd_mutex.unlock();
T00209563 4:3202052e5a8b 38 i++;
T00209563 4:3202052e5a8b 39 Thread::wait(1000);
T00209563 4:3202052e5a8b 40 }
T00209563 4:3202052e5a8b 41 }
T00209563 4:3202052e5a8b 42
T00209563 4:3202052e5a8b 43 // Thread 2
T00209563 4:3202052e5a8b 44 // print counter into third line and wait for 0,5s
T00209563 4:3202052e5a8b 45 void thread2(void const *args)
T00209563 4:3202052e5a8b 46 {
T00209563 4:3202052e5a8b 47 int k;
T00209563 4:3202052e5a8b 48 while(true) { // thread loop
T00209563 4:3202052e5a8b 49 lcd_mutex.lock();
T00209563 4:3202052e5a8b 50 LCD.locate(0,20);
T00209563 4:3202052e5a8b 51 LCD.set_font((unsigned char*) Arial_9);
T00209563 4:3202052e5a8b 52 LCD.printf("Thread 2 count : %d",k);
T00209563 4:3202052e5a8b 53 lcd_mutex.unlock();
T00209563 4:3202052e5a8b 54 k++;
T00209563 4:3202052e5a8b 55 Thread::wait(500); // wait 0.5s
T00209563 4:3202052e5a8b 56 }
T00209563 4:3202052e5a8b 57 }
T00209563 4:3202052e5a8b 58
T00209563 4:3202052e5a8b 59 // Thread 3
T00209563 4:3202052e5a8b 60 // print a sin function in a small window
T00209563 4:3202052e5a8b 61 // the value of pot 1 change the speed of the sinwave
T00209563 4:3202052e5a8b 62 void thread3(void const *args)
T00209563 4:3202052e5a8b 63 {
T00209563 4:3202052e5a8b 64 int i,k,v;
T00209563 4:3202052e5a8b 65 double s,a;
T00209563 4:3202052e5a8b 66 k = 1;
T00209563 4:3202052e5a8b 67 lcd_mutex.lock();
T00209563 4:3202052e5a8b 68 LCD.rect(89,0,127,17,1);
T00209563 4:3202052e5a8b 69 lcd_mutex.unlock();
T00209563 4:3202052e5a8b 70 while(true) { // thread loop
T00209563 4:3202052e5a8b 71 v = Pot1.read_u16(); // get value of pot 1
T00209563 4:3202052e5a8b 72 lcd_mutex.lock();
T00209563 4:3202052e5a8b 73 for (i=90; i<127; i++) {
T00209563 4:3202052e5a8b 74 s = 8 * sin((long double)(i+k) /5); // pixel to print
T00209563 4:3202052e5a8b 75 a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase
T00209563 4:3202052e5a8b 76 LCD.pixel(i,9 + (int)a,0); // erase pixel
T00209563 4:3202052e5a8b 77 LCD.pixel(i,9 + (int)s,1); // print pixel
T00209563 4:3202052e5a8b 78 }
T00209563 4:3202052e5a8b 79 LCD.copy_to_lcd(); // LCD.pixel do not update the lcd
T00209563 4:3202052e5a8b 80 lcd_mutex.unlock();
T00209563 4:3202052e5a8b 81 k++;
T00209563 4:3202052e5a8b 82 Thread::wait(v/100); // value of pot1 / 100
T00209563 4:3202052e5a8b 83 }
T00209563 4:3202052e5a8b 84 }
T00209563 4:3202052e5a8b 85
T00209563 4:3202052e5a8b 86 // Thread 4
T00209563 4:3202052e5a8b 87 // input pot 2 and change the contrast of lcd
T00209563 4:3202052e5a8b 88 void thread4(void const *args)
T00209563 4:3202052e5a8b 89 {
T00209563 4:3202052e5a8b 90 int k;
T00209563 4:3202052e5a8b 91 while(true) { // thread loop
T00209563 4:3202052e5a8b 92 k = Pot2.read_u16(); // get the value of poti 2
T00209563 4:3202052e5a8b 93 k = k >> 10; // we need only 6 bit for contrast
T00209563 4:3202052e5a8b 94 lcd_mutex.lock();
T00209563 4:3202052e5a8b 95 LCD.set_contrast(k);
T00209563 4:3202052e5a8b 96 lcd_mutex.unlock();
T00209563 4:3202052e5a8b 97 Thread::wait(500); // wait 0.5s
T00209563 4:3202052e5a8b 98 }
T00209563 4:3202052e5a8b 99 }
T00209563 4:3202052e5a8b 100
T00209563 4:3202052e5a8b 101
T00209563 4:3202052e5a8b 102 // print the actual contrast
T00209563 4:3202052e5a8b 103
T00209563 4:3202052e5a8b 104 //Thraed 5
T00209563 4:3202052e5a8b 105 // using joystick to control Leds
T00209563 4:3202052e5a8b 106 void thread5(void const *args)
T00209563 4:3202052e5a8b 107 {
T00209563 4:3202052e5a8b 108 while(1) {
T00209563 4:3202052e5a8b 109 if (fire) {
T00209563 4:3202052e5a8b 110 leds=0xf;
T00209563 4:3202052e5a8b 111 } else {
T00209563 4:3202052e5a8b 112 leds=joy;
T00209563 4:3202052e5a8b 113 }
T00209563 4:3202052e5a8b 114 wait(0.1);
T00209563 4:3202052e5a8b 115 }
T00209563 4:3202052e5a8b 116 }
T00209563 4:3202052e5a8b 117
T00209563 4:3202052e5a8b 118
T00209563 4:3202052e5a8b 119
T00209563 4:3202052e5a8b 120
T00209563 4:3202052e5a8b 121 int main()
T00209563 4:3202052e5a8b 122 {
T00209563 4:3202052e5a8b 123 int j;
T00209563 4:3202052e5a8b 124 LCD.cls();
T00209563 4:3202052e5a8b 125
T00209563 4:3202052e5a8b 126 Thread t1(thread1); //start thread1
T00209563 4:3202052e5a8b 127 Thread t2(thread2); //start thread2
T00209563 4:3202052e5a8b 128 Thread t3(thread3); //start thread3
T00209563 4:3202052e5a8b 129 Thread t4(thread4); //start thread4
T00209563 4:3202052e5a8b 130 Thread t5(thread5); //start thread5
T00209563 4:3202052e5a8b 131
T00209563 4:3202052e5a8b 132
T00209563 4:3202052e5a8b 133 while(true) { // main is the next thread
T00209563 4:3202052e5a8b 134 lcd_mutex.lock();
T00209563 4:3202052e5a8b 135 LCD.locate(0,9);
T00209563 4:3202052e5a8b 136 LCD.set_font((unsigned char*) Small_7);
T00209563 4:3202052e5a8b 137 j = LCD.get_contrast(); // read the actual contrast
T00209563 4:3202052e5a8b 138 LCD.printf("contrast : %d",j);
T00209563 4:3202052e5a8b 139 lcd_mutex.unlock();
T00209563 4:3202052e5a8b 140 Thread::wait(500); // wait 0.5s
T00209563 4:3202052e5a8b 141 }
T00209563 4:3202052e5a8b 142
T00209563 4:3202052e5a8b 143 }