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.
Diff: main.cpp
- Revision:
- 1:667b9825e7ee
- Parent:
- 0:22a4f6c99d74
- Child:
- 2:45ed62566694
diff -r 22a4f6c99d74 -r 667b9825e7ee main.cpp
--- a/main.cpp Sat Aug 10 22:25:46 2019 +0000
+++ b/main.cpp Thu Aug 15 23:47:18 2019 +0300
@@ -1,96 +1,24 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2018 ARM Limited
- * SPDX-License-Identifier: Apache-2.0
- */
+/* BME280 BLE senior -- main.cpp
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+* Copyright 2019 Fedor Chervyakov
+*/
#include "mbed.h"
-#include "bme280.h"
-
-
-
-I2C sensor(I2C_SDA, I2C_SCL);
-
-void user_delay_ms(uint32_t period)
-{
- wait_ms(period);
-}
-
-
-int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
-{
- int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
- int addr8bit = dev_id << 1;
-
- char cmd[2];
- cmd[0] = (char) reg_addr;
-
- rslt = sensor.write(addr8bit, cmd, 1);
- rslt = sensor.read( addr8bit, (char *) reg_data, (int) len);
-
- /*if (sizeof(reg_data)/8 < len)
- return BME280_E_COMM_FAIL;
- */
- /*
- * The parameter dev_id can be used as a variable to store the I2C address of the device
- */
+#include "bme280_wrapper.h"
- /*
- * Data on the bus should be like
- * |------------+---------------------|
- * | I2C action | Data |
- * |------------+---------------------|
- * | Start | - |
- * | Write | (reg_addr) |
- * | Stop | - |
- * | Start | - |
- * | Read | (reg_data[0]) |
- * | Read | (....) |
- * | Read | (reg_data[len - 1]) |
- * | Stop | - |
- * |------------+---------------------|
- */
-
- return rslt;
-}
-
-int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
-{
- int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
-
- /*
- * The parameter dev_id can be used as a variable to store the I2C address of the device
- */
-
- int addr8bit = dev_id << 1;
- char cmd[2*len];
-
- int n;
- for (int i=0; i<len; i++) {
- n = 2*i;
- cmd[n] = reg_addr+i;
- cmd[n+1] = reg_data[i];
- }
-
- rslt = sensor.write(addr8bit, cmd, len*2);
- /*
- * Data on the bus should be like
- * |------------+----------------------|
- * | I2C action | Data |
- * |------------+----------------------|
- * | Start | - |
- * | Write | (reg_addr) |
- * | Write | (reg_data[0]) |
- * | Write | (reg_addr + 1) |
- * | Write | (reg_data[1]) |
- * | Write | (....) |
- * | Write | (reg_addr + len - 1) |
- * | Write | (reg_data[len - 1]) |
- * | Stop | - |
- * |------------+----------------------|
- */
-
- return rslt;
-}
+AnalogIn lm35_input(A0);
void print_sensor_data(struct bme280_data *comp_data)
{
@@ -110,56 +38,20 @@
hum = 1.0f / 1024.0f * comp_data->humidity;
#endif
#endif
- printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
+ printf("BME280 %0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
}
// main() runs in its own thread in the OS
int main()
{
- struct bme280_dev dev;
struct bme280_data comp_data;
- uint8_t settings_sel;
- int8_t rslt;
-
- dev.dev_id = BME280_I2C_ADDR_PRIM;
- dev.intf = BME280_I2C_INTF;
- dev.write = user_i2c_write;
- dev.read = user_i2c_read;
- dev.delay_ms = user_delay_ms;
-
- bme280_init(&dev);
-
- dev.settings.osr_h = BME280_OVERSAMPLING_1X;
- dev.settings.osr_p = BME280_OVERSAMPLING_16X;
- dev.settings.osr_t = BME280_OVERSAMPLING_2X;
- dev.settings.filter = BME280_FILTER_COEFF_16;
-
- settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
-
- rslt = bme280_set_sensor_settings(settings_sel, &dev);
- if (rslt != BME280_OK)
- {
- fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
- return rslt;
- }
+ BME280 bme280(I2C_SDA, I2C_SCL);
while (true) {
- rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, &dev);
- if (rslt != BME280_OK)
- {
- printf("Failed to set sensor mode (code %+d).\n", rslt);
- break;
- }
- dev.delay_ms(400);
- rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, &dev);
-
- if (rslt != BME280_OK)
- {
- printf("Failed to get sensor data (code %+d).\n", rslt);
- break;
- }
- print_sensor_data(&comp_data);
-
+ bme280.force_measurement();
+ printf("LM35 %.2f deg C; ", lm35_input / 0.01 * 3.3);
+ print_sensor_data(&bme280.comp_data);
+ wait(5);
}
}