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: mbed SSD1306_128x64_I2C
main.cpp
00001 #include "mbed.h" 00002 #include "SSD1306.h" 00003 #include "bme280.h" 00004 #include "stdlib.h" 00005 #include "mbed_logo.h" 00006 00007 #define D_SDA P0_0 00008 #define D_SCL P0_1 00009 00010 //Functions 00011 void triggerfall_1();//Button1 falling interrupt function 00012 void triggerrise_1();//Button1 rising interrupt function 00013 void triggerfall_2();//Button1 falling interrupt function 00014 void triggerrise_2();//Button1 rising interrupt function 00015 00016 float t; 00017 float pr; 00018 int h; 00019 00020 uint8_t address = 0x78; 00021 00022 I2C i2c(D_SDA, D_SCL); 00023 00024 SSD1306 oled(i2c, address); 00025 BME280 bme280; 00026 00027 //Initiate IO 00028 DigitalOut led0(LED1); 00029 DigitalOut led1(LED2); 00030 DigitalOut led2(P0_20); 00031 DigitalOut led3(P0_21); 00032 DigitalOut led4(P0_22, 0); 00033 DigitalIn sw1(BUTTON1); 00034 DigitalIn sw2(BUTTON2); 00035 00036 //Initiate input interrupts 00037 InterruptIn sw1Press(BUTTON1); 00038 InterruptIn sw2Press(BUTTON2); 00039 00040 //bme280 00041 void i2cWrite(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size, uint8_t repeated_start) 00042 { 00043 // mbed uses 8-bit addresses, always confusing. 00044 i2c.write(i2c_address<<1,(const char *)p_data,data_size,repeated_start); 00045 } 00046 00047 void i2cRead(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size) 00048 { 00049 // mbed uses 8-bit addresses, always confusing. 00050 i2c.read(i2c_address<<1,(char *)p_data,data_size); 00051 } 00052 00053 //ssd1306 00054 void Start_page() 00055 { 00056 int i=0; 00057 oled.writeString(1, 3, "Fight Robot"); 00058 while(i<100) { 00059 oled.writeProgressBar(20,40,i); 00060 i++; 00061 wait_ms(10); 00062 } 00063 i = 0; 00064 oled.writeBitmap((uint8_t*) mbed_logo); 00065 while(i<100){ 00066 i++; 00067 wait_ms(10); 00068 } 00069 } 00070 void Display_Meteo(float t, float pr, int h) 00071 { 00072 // sprintf(buf, "%f", t); 00073 oled.writeString(1, 2, "T: "); 00074 oled.printf("%.2f",t); 00075 oled.printf("%s"," C"); 00076 oled.writeString(3, 2, "P: "); 00077 oled.printf("%.1f",pr); 00078 oled.printf("%s"," mmHg"); 00079 oled.writeString(5, 2, "H: "); 00080 oled.printf("%d",h); 00081 oled.printf("%s"," %"); 00082 } 00083 00084 int main() 00085 { 00086 //Initialise LED output 00087 led0=0; 00088 led1=0; 00089 led2=0; 00090 led3=0; 00091 00092 //Set falling and rising edge to apppropriate interrup function 00093 sw1Press.fall(&triggerfall_1); 00094 sw1Press.rise(&triggerrise_1); 00095 sw2Press.fall(&triggerfall_2); 00096 sw2Press.rise(&triggerrise_2); 00097 00098 bme280.begin(BME280_I2C_ADDRESS1); 00099 // Configure for test purposes. 00100 bme280.writeConfigRegister(BME280_STANDBY_500_US,BME280_FILTER_OFF,0); 00101 bme280.writeControlRegisters(BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_MODE_NORMAL); 00102 Start_page(); 00103 oled.clearDisplay(); 00104 00105 while(1) { 00106 led4=!led4; 00107 bme280.read(); 00108 t = bme280.temperature(); 00109 float p = bme280.pressure()/100; 00110 pr = p*0.750062; 00111 h = bme280.humidity(); 00112 Display_Meteo(t, pr, h); 00113 wait_ms(1000); 00114 } 00115 } 00116 //Button1 falling interrupt function 00117 void triggerfall_1() 00118 { 00119 //Toggle LED1 00120 led0=!led0; 00121 } 00122 00123 //Button1 rising interrupt function 00124 void triggerrise_1() 00125 { 00126 //Toggle LED2 00127 led1=!led1; 00128 } 00129 //Button1 falling interrupt function 00130 void triggerfall_2() 00131 { 00132 //Toggle LED1 00133 led2=!led2; 00134 } 00135 00136 //Button1 rising interrupt function 00137 void triggerrise_2() 00138 { 00139 //Toggle LED2 00140 led3=!led3; 00141 }
Generated on Sat Aug 27 2022 09:57:51 by
