nRF51822, BME280, SSD1306_i2c, BLE400
Dependencies: mbed SSD1306_128x64_I2C
main.cpp@0:6e40dd247a04, 2019-11-28 (annotated)
- Committer:
- mamont090671
- Date:
- Thu Nov 28 11:43:48 2019 +0000
- Revision:
- 0:6e40dd247a04
BME280, nRF51822, SSD1306_i2c, BLE400;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mamont090671 | 0:6e40dd247a04 | 1 | #include "mbed.h" |
mamont090671 | 0:6e40dd247a04 | 2 | #include "SSD1306.h" |
mamont090671 | 0:6e40dd247a04 | 3 | #include "bme280.h" |
mamont090671 | 0:6e40dd247a04 | 4 | #include "stdlib.h" |
mamont090671 | 0:6e40dd247a04 | 5 | #include "mbed_logo.h" |
mamont090671 | 0:6e40dd247a04 | 6 | |
mamont090671 | 0:6e40dd247a04 | 7 | #define D_SDA P0_0 |
mamont090671 | 0:6e40dd247a04 | 8 | #define D_SCL P0_1 |
mamont090671 | 0:6e40dd247a04 | 9 | |
mamont090671 | 0:6e40dd247a04 | 10 | //Functions |
mamont090671 | 0:6e40dd247a04 | 11 | void triggerfall_1();//Button1 falling interrupt function |
mamont090671 | 0:6e40dd247a04 | 12 | void triggerrise_1();//Button1 rising interrupt function |
mamont090671 | 0:6e40dd247a04 | 13 | void triggerfall_2();//Button1 falling interrupt function |
mamont090671 | 0:6e40dd247a04 | 14 | void triggerrise_2();//Button1 rising interrupt function |
mamont090671 | 0:6e40dd247a04 | 15 | |
mamont090671 | 0:6e40dd247a04 | 16 | float t; |
mamont090671 | 0:6e40dd247a04 | 17 | float pr; |
mamont090671 | 0:6e40dd247a04 | 18 | int h; |
mamont090671 | 0:6e40dd247a04 | 19 | |
mamont090671 | 0:6e40dd247a04 | 20 | uint8_t address = 0x78; |
mamont090671 | 0:6e40dd247a04 | 21 | |
mamont090671 | 0:6e40dd247a04 | 22 | I2C i2c(D_SDA, D_SCL); |
mamont090671 | 0:6e40dd247a04 | 23 | |
mamont090671 | 0:6e40dd247a04 | 24 | SSD1306 oled(i2c, address); |
mamont090671 | 0:6e40dd247a04 | 25 | BME280 bme280; |
mamont090671 | 0:6e40dd247a04 | 26 | |
mamont090671 | 0:6e40dd247a04 | 27 | //Initiate IO |
mamont090671 | 0:6e40dd247a04 | 28 | DigitalOut led0(LED1); |
mamont090671 | 0:6e40dd247a04 | 29 | DigitalOut led1(LED2); |
mamont090671 | 0:6e40dd247a04 | 30 | DigitalOut led2(P0_20); |
mamont090671 | 0:6e40dd247a04 | 31 | DigitalOut led3(P0_21); |
mamont090671 | 0:6e40dd247a04 | 32 | DigitalOut led4(P0_22, 0); |
mamont090671 | 0:6e40dd247a04 | 33 | DigitalIn sw1(BUTTON1); |
mamont090671 | 0:6e40dd247a04 | 34 | DigitalIn sw2(BUTTON2); |
mamont090671 | 0:6e40dd247a04 | 35 | |
mamont090671 | 0:6e40dd247a04 | 36 | //Initiate input interrupts |
mamont090671 | 0:6e40dd247a04 | 37 | InterruptIn sw1Press(BUTTON1); |
mamont090671 | 0:6e40dd247a04 | 38 | InterruptIn sw2Press(BUTTON2); |
mamont090671 | 0:6e40dd247a04 | 39 | |
mamont090671 | 0:6e40dd247a04 | 40 | //bme280 |
mamont090671 | 0:6e40dd247a04 | 41 | void i2cWrite(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size, uint8_t repeated_start) |
mamont090671 | 0:6e40dd247a04 | 42 | { |
mamont090671 | 0:6e40dd247a04 | 43 | // mbed uses 8-bit addresses, always confusing. |
mamont090671 | 0:6e40dd247a04 | 44 | i2c.write(i2c_address<<1,(const char *)p_data,data_size,repeated_start); |
mamont090671 | 0:6e40dd247a04 | 45 | } |
mamont090671 | 0:6e40dd247a04 | 46 | |
mamont090671 | 0:6e40dd247a04 | 47 | void i2cRead(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size) |
mamont090671 | 0:6e40dd247a04 | 48 | { |
mamont090671 | 0:6e40dd247a04 | 49 | // mbed uses 8-bit addresses, always confusing. |
mamont090671 | 0:6e40dd247a04 | 50 | i2c.read(i2c_address<<1,(char *)p_data,data_size); |
mamont090671 | 0:6e40dd247a04 | 51 | } |
mamont090671 | 0:6e40dd247a04 | 52 | |
mamont090671 | 0:6e40dd247a04 | 53 | //ssd1306 |
mamont090671 | 0:6e40dd247a04 | 54 | void Start_page() |
mamont090671 | 0:6e40dd247a04 | 55 | { |
mamont090671 | 0:6e40dd247a04 | 56 | int i=0; |
mamont090671 | 0:6e40dd247a04 | 57 | oled.writeString(1, 3, "Fight Robot"); |
mamont090671 | 0:6e40dd247a04 | 58 | while(i<100) { |
mamont090671 | 0:6e40dd247a04 | 59 | oled.writeProgressBar(20,40,i); |
mamont090671 | 0:6e40dd247a04 | 60 | i++; |
mamont090671 | 0:6e40dd247a04 | 61 | wait_ms(10); |
mamont090671 | 0:6e40dd247a04 | 62 | } |
mamont090671 | 0:6e40dd247a04 | 63 | i = 0; |
mamont090671 | 0:6e40dd247a04 | 64 | oled.writeBitmap((uint8_t*) mbed_logo); |
mamont090671 | 0:6e40dd247a04 | 65 | while(i<100){ |
mamont090671 | 0:6e40dd247a04 | 66 | i++; |
mamont090671 | 0:6e40dd247a04 | 67 | wait_ms(10); |
mamont090671 | 0:6e40dd247a04 | 68 | } |
mamont090671 | 0:6e40dd247a04 | 69 | } |
mamont090671 | 0:6e40dd247a04 | 70 | void Display_Meteo(float t, float pr, int h) |
mamont090671 | 0:6e40dd247a04 | 71 | { |
mamont090671 | 0:6e40dd247a04 | 72 | // sprintf(buf, "%f", t); |
mamont090671 | 0:6e40dd247a04 | 73 | oled.writeString(1, 2, "T: "); |
mamont090671 | 0:6e40dd247a04 | 74 | oled.printf("%.2f",t); |
mamont090671 | 0:6e40dd247a04 | 75 | oled.printf("%s"," C"); |
mamont090671 | 0:6e40dd247a04 | 76 | oled.writeString(3, 2, "P: "); |
mamont090671 | 0:6e40dd247a04 | 77 | oled.printf("%.1f",pr); |
mamont090671 | 0:6e40dd247a04 | 78 | oled.printf("%s"," mmHg"); |
mamont090671 | 0:6e40dd247a04 | 79 | oled.writeString(5, 2, "H: "); |
mamont090671 | 0:6e40dd247a04 | 80 | oled.printf("%d",h); |
mamont090671 | 0:6e40dd247a04 | 81 | oled.printf("%s"," %"); |
mamont090671 | 0:6e40dd247a04 | 82 | } |
mamont090671 | 0:6e40dd247a04 | 83 | |
mamont090671 | 0:6e40dd247a04 | 84 | int main() |
mamont090671 | 0:6e40dd247a04 | 85 | { |
mamont090671 | 0:6e40dd247a04 | 86 | //Initialise LED output |
mamont090671 | 0:6e40dd247a04 | 87 | led0=0; |
mamont090671 | 0:6e40dd247a04 | 88 | led1=0; |
mamont090671 | 0:6e40dd247a04 | 89 | led2=0; |
mamont090671 | 0:6e40dd247a04 | 90 | led3=0; |
mamont090671 | 0:6e40dd247a04 | 91 | |
mamont090671 | 0:6e40dd247a04 | 92 | //Set falling and rising edge to apppropriate interrup function |
mamont090671 | 0:6e40dd247a04 | 93 | sw1Press.fall(&triggerfall_1); |
mamont090671 | 0:6e40dd247a04 | 94 | sw1Press.rise(&triggerrise_1); |
mamont090671 | 0:6e40dd247a04 | 95 | sw2Press.fall(&triggerfall_2); |
mamont090671 | 0:6e40dd247a04 | 96 | sw2Press.rise(&triggerrise_2); |
mamont090671 | 0:6e40dd247a04 | 97 | |
mamont090671 | 0:6e40dd247a04 | 98 | bme280.begin(BME280_I2C_ADDRESS1); |
mamont090671 | 0:6e40dd247a04 | 99 | // Configure for test purposes. |
mamont090671 | 0:6e40dd247a04 | 100 | bme280.writeConfigRegister(BME280_STANDBY_500_US,BME280_FILTER_OFF,0); |
mamont090671 | 0:6e40dd247a04 | 101 | bme280.writeControlRegisters(BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_MODE_NORMAL); |
mamont090671 | 0:6e40dd247a04 | 102 | Start_page(); |
mamont090671 | 0:6e40dd247a04 | 103 | oled.clearDisplay(); |
mamont090671 | 0:6e40dd247a04 | 104 | |
mamont090671 | 0:6e40dd247a04 | 105 | while(1) { |
mamont090671 | 0:6e40dd247a04 | 106 | led4=!led4; |
mamont090671 | 0:6e40dd247a04 | 107 | bme280.read(); |
mamont090671 | 0:6e40dd247a04 | 108 | t = bme280.temperature(); |
mamont090671 | 0:6e40dd247a04 | 109 | float p = bme280.pressure()/100; |
mamont090671 | 0:6e40dd247a04 | 110 | pr = p*0.750062; |
mamont090671 | 0:6e40dd247a04 | 111 | h = bme280.humidity(); |
mamont090671 | 0:6e40dd247a04 | 112 | Display_Meteo(t, pr, h); |
mamont090671 | 0:6e40dd247a04 | 113 | wait_ms(1000); |
mamont090671 | 0:6e40dd247a04 | 114 | } |
mamont090671 | 0:6e40dd247a04 | 115 | } |
mamont090671 | 0:6e40dd247a04 | 116 | //Button1 falling interrupt function |
mamont090671 | 0:6e40dd247a04 | 117 | void triggerfall_1() |
mamont090671 | 0:6e40dd247a04 | 118 | { |
mamont090671 | 0:6e40dd247a04 | 119 | //Toggle LED1 |
mamont090671 | 0:6e40dd247a04 | 120 | led0=!led0; |
mamont090671 | 0:6e40dd247a04 | 121 | } |
mamont090671 | 0:6e40dd247a04 | 122 | |
mamont090671 | 0:6e40dd247a04 | 123 | //Button1 rising interrupt function |
mamont090671 | 0:6e40dd247a04 | 124 | void triggerrise_1() |
mamont090671 | 0:6e40dd247a04 | 125 | { |
mamont090671 | 0:6e40dd247a04 | 126 | //Toggle LED2 |
mamont090671 | 0:6e40dd247a04 | 127 | led1=!led1; |
mamont090671 | 0:6e40dd247a04 | 128 | } |
mamont090671 | 0:6e40dd247a04 | 129 | //Button1 falling interrupt function |
mamont090671 | 0:6e40dd247a04 | 130 | void triggerfall_2() |
mamont090671 | 0:6e40dd247a04 | 131 | { |
mamont090671 | 0:6e40dd247a04 | 132 | //Toggle LED1 |
mamont090671 | 0:6e40dd247a04 | 133 | led2=!led2; |
mamont090671 | 0:6e40dd247a04 | 134 | } |
mamont090671 | 0:6e40dd247a04 | 135 | |
mamont090671 | 0:6e40dd247a04 | 136 | //Button1 rising interrupt function |
mamont090671 | 0:6e40dd247a04 | 137 | void triggerrise_2() |
mamont090671 | 0:6e40dd247a04 | 138 | { |
mamont090671 | 0:6e40dd247a04 | 139 | //Toggle LED2 |
mamont090671 | 0:6e40dd247a04 | 140 | led3=!led3; |
mamont090671 | 0:6e40dd247a04 | 141 | } |