First draft HMC5883 magnetometer sensor using physical quantities, outputting via serial port using std::cout on mbed os 5
Revision 11:de7c9ae7ef65, committed 2020-03-26
- Comitter:
- skyscraper
- Date:
- Thu Mar 26 22:58:21 2020 +0000
- Parent:
- 10:75c8ce89aeb7
- Commit message:
- Changed to use threads
Changed in this revision
diff -r 75c8ce89aeb7 -r de7c9ae7ef65 I2CBusDevice.h --- a/I2CBusDevice.h Thu Mar 26 21:59:18 2020 +0000 +++ b/I2CBusDevice.h Thu Mar 26 22:58:21 2020 +0000 @@ -8,6 +8,7 @@ constexpr I2CBusDevice(I2C& i2cIn,uint8_t addressIn) : m_i2c{i2cIn},m_address{addressIn}{} + int i2c_write(const char *data, int length,bool repeated = false)const { return m_i2c.write(m_address,data,length, repeated);
diff -r 75c8ce89aeb7 -r de7c9ae7ef65 magnetometer.cpp --- a/magnetometer.cpp Thu Mar 26 21:59:18 2020 +0000 +++ b/magnetometer.cpp Thu Mar 26 22:58:21 2020 +0000 @@ -10,7 +10,7 @@ bool mag_init() { -// if startup, allow magnetometer hardware time to start + // if startup, allow magnetometer hardware time to start if ( Kernel::get_ms_count() < 500U){ ThisThread::sleep_until(500U); }
diff -r 75c8ce89aeb7 -r de7c9ae7ef65 main.cpp --- a/main.cpp Thu Mar 26 21:59:18 2020 +0000 +++ b/main.cpp Thu Mar 26 22:58:21 2020 +0000 @@ -2,31 +2,19 @@ #include "mbed.h" #include "magnetometer.h" -namespace { - - // do something with magnetometer output - void callback(quan::three_d::vect<quan::magnetic_flux_density::uT> const & v) - { - std::cout << "val = " << v << '\n'; - } - -}// namespace - void loop_forever(std::string const & str); -int main() +void mag_service() { - DigitalOut led2(LED2,1); - std::cout << "magnetometer test\n"; + std::cout << "magnetometer service\n"; if ( !mag_init()){ loop_forever("failed to init magnetometer\n"); } uint64_t constexpr update_rate_ms = 20U; - - auto prev_led = Kernel::get_ms_count(); - auto now = Kernel::get_ms_count(); + + auto wake = Kernel::get_ms_count(); for (;;){ @@ -38,16 +26,37 @@ quan::three_d::vect<quan::magnetic_flux_density::uT> v; if (mag_read(v)){ - callback(v); - } - - if ( (now - prev_led) >= 500U){ - prev_led = now; - led2 = (led2 == 0) ? 1: 0; + std::cout << "val = " << v << '\n'; } - - auto const next_wake = now + update_rate_ms; - ThisThread::sleep_until(next_wake); - now = next_wake; + + wake += update_rate_ms; + ThisThread::sleep_until(wake); } +} + +void led_blink_service() +{ + DigitalOut led(LED2,1); + + std::cout << "Blink service\n"; + + uint64_t constexpr update_rate_ms = 500U; + auto wake = Kernel::get_ms_count(); + for (;;){ + wake += update_rate_ms; + ThisThread::sleep_until(wake); + led = ( led == 0) ? 1: 0; + } +} + +int main() +{ + Thread magThread; + magThread.start(mag_service); + + Thread blinkThread; + blinkThread.start(led_blink_service); + + for (;;){;} + } \ No newline at end of file