fork

Dependencies:   mbed mbed-rtos C12832_lcd LCD_fonts

Committer:
saltire78
Date:
Fri Jul 31 13:16:40 2020 +0000
Revision:
4:1229f672b999
Parent:
3:3ec443c0842a
A combined multi-thread program displaying a PWM frequency on an LCD

Who changed what in which revision?

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