diff --git a/C++/Examples/Multithread/Makefile b/C++/Examples/Multithread/Makefile new file mode 100644 index 00000000..b2be6fff --- /dev/null +++ b/C++/Examples/Multithread/Makefile @@ -0,0 +1,9 @@ +CC = g++ +NAVIO = ../../Navio/Common +INCLUDES = -I ../../Navio + +all: + $(CC) $(INCLUDES) threaded_baro.cpp $(NAVIO)/MS5611.cpp $(NAVIO)/I2Cdev.cpp $(NAVIO)/Util.cpp -lpthread -o threaded_baro + +clean: + rm threaded_baro diff --git a/C++/Examples/Multithread/threaded_baro.cpp b/C++/Examples/Multithread/threaded_baro.cpp new file mode 100644 index 00000000..c50e7d45 --- /dev/null +++ b/C++/Examples/Multithread/threaded_baro.cpp @@ -0,0 +1,66 @@ +/* +Provided to you by Emlid Ltd (c) 2014. +twitter.com/emlidtech || www.emlid.com || info@emlid.com + +Example: Get pressure from MS5611 barometer onboard of Navio shield for Raspberry Pi +using a different thread, to update pressure info in the background + +To run this example navigate to the directory containing it and run following commands: +make +sudo ./threaded_baro +*/ + +#include +#include +#include +#include +#include + +void * acquireBarometerData(void * barom) +{ + MS5611* barometer = (MS5611*)barom; + + while (true) { + barometer->refreshPressure(); + usleep(10000); // Waiting for pressure data ready + barometer->readPressure(); + + barometer->refreshTemperature(); + usleep(10000); // Waiting for temperature data ready + barometer->readTemperature(); + + barometer->calculatePressureAndTemperature(); + + //sleep(0.5); + } + + pthread_exit(NULL); +} + +int main() +{ + if (check_apm()) { + return 1; + } + + MS5611 baro; + baro.initialize(); + + pthread_t baro_thread; + + if(pthread_create(&baro_thread, NULL, acquireBarometerData, (void *)&baro)) + { + printf("Error: Failed to create barometer thread\n"); + return 0; + } + + while(true) + { + printf("Temperature(C): %f Pressure(millibar): %f\n", baro.getTemperature(), baro.getPressure()); + sleep(1); + } + + pthread_exit(NULL); + + return 1; +}