Skip to content

Commit

Permalink
C++: Examples: add Multithread Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Anokhin authored and staroselskii committed Nov 17, 2017
1 parent a63641b commit 5965781
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions C++/Examples/Multithread/Makefile
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions C++/Examples/Multithread/threaded_baro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Provided to you by Emlid Ltd (c) 2014.
twitter.com/emlidtech || www.emlid.com || [email protected]
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 <Common/MS5611.h>
#include <Common/Util.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>

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;
}

0 comments on commit 5965781

Please sign in to comment.