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.
Dependencies: 4DGL-uLCD-SE ENS160_Library mbed PinDetect mbed-rtos
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "uLCD_4DGL.h" 00004 #include "PinDetect.h" 00005 #include "ens160_i2c.h" 00006 00007 ENS160 myENS(p9, p10, ENS160_ADDRESS_HIGH); 00008 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; 00009 PinDetect pb(p8); 00010 Mutex mutex; 00011 00012 uint8_t volatile aqi; 00013 uint32_t volatile co2,tvoc; 00014 uint8_t volatile current_screen = 0; 00015 00016 void redrawBG() 00017 { 00018 mutex.lock(); 00019 uLCD.color(WHITE); 00020 uLCD.circle(64,64,64,WHITE); 00021 uLCD.filled_circle(64,64,60,BLUE); 00022 mutex.unlock(); 00023 } 00024 00025 void getData(void const *args) 00026 { 00027 while(1) 00028 { 00029 aqi = myENS.getAQI(); 00030 co2 = myENS.getECO2(); 00031 tvoc = myENS.getTVOC(); 00032 Thread::wait(100); 00033 } 00034 } 00035 00036 void allScreens() 00037 { 00038 uLCD.text_width(1.75); //4X size text 00039 uLCD.text_height(1.75); 00040 uLCD.locate(6,4); 00041 uLCD.printf("AQI:"); 00042 uLCD.printf("%d",aqi); 00043 uLCD.locate(4,7); 00044 uLCD.printf("C02:"); 00045 uLCD.printf("%d", co2); 00046 uLCD.printf("ppm"); 00047 uLCD.locate(4,10); 00048 uLCD.printf("TVOC:"); 00049 uLCD.printf("%d",tvoc); 00050 uLCD.printf("ppb"); 00051 } 00052 00053 void AQI() 00054 { 00055 uLCD.text_width(2); //4X size text 00056 uLCD.text_height(2); 00057 uLCD.locate(3,3); 00058 uLCD.printf("AQI\n"); 00059 uLCD.locate(4,4); 00060 uLCD.printf("%d",aqi); 00061 } 00062 00063 void CO2() 00064 { 00065 uLCD.text_width(2); //4X size text 00066 uLCD.text_height(2); 00067 uLCD.locate(3,3); 00068 uLCD.printf("C02\n"); 00069 uLCD.locate(1,4); 00070 uLCD.printf("%d", co2); 00071 uLCD.printf("ppm"); 00072 } 00073 00074 void TVOC() 00075 { 00076 uLCD.text_width(2); //4X size text 00077 uLCD.text_height(2); 00078 uLCD.locate(3,3); 00079 uLCD.printf("TVOC\n"); 00080 uLCD.locate(2,4); 00081 uLCD.printf("%d",tvoc); 00082 uLCD.printf("ppb"); 00083 } 00084 00085 void updateScreen() 00086 { 00087 mutex.lock(); 00088 switch (current_screen) 00089 { 00090 case 0: 00091 allScreens(); 00092 break; 00093 case 1: 00094 AQI(); 00095 break; 00096 case 2: 00097 TVOC(); 00098 break; 00099 case 3: 00100 CO2(); 00101 break; 00102 } 00103 mutex.unlock(); 00104 } 00105 00106 void pb_hit_callback() 00107 { 00108 current_screen = current_screen + 1; 00109 if (current_screen > 3) 00110 current_screen = 0; 00111 } 00112 00113 int main() 00114 { 00115 pb.mode(PullUp); 00116 wait(.001); 00117 pb.attach_deasserted(&pb_hit_callback); 00118 pb.setSampleFrequency(); 00119 Thread t1(getData); 00120 redrawBG(); 00121 uint8_t oldScreen = current_screen; 00122 while(1) 00123 { 00124 if (oldScreen != current_screen) 00125 { 00126 oldScreen = current_screen; 00127 redrawBG(); 00128 } 00129 updateScreen(); 00130 Thread::wait(1000); 00131 } 00132 }
Generated on Thu Dec 8 2022 16:59:03 by
1.7.2