From d21e9911bfd3ae4dbbb5a91b559e91b58cb5f3b3 Mon Sep 17 00:00:00 2001 From: "Sascha Schade (strongly-typed)" Date: Sun, 29 Mar 2020 23:23:33 +0200 Subject: [PATCH] [example] Simple example using HX711 driver --- .../weight_scale_hx711/main.cpp | 123 ++++++++++++++++++ .../weight_scale_hx711/openocd.cfg | 2 + .../weight_scale_hx711/project.xml | 16 +++ 3 files changed, 141 insertions(+) create mode 100644 examples/stm32f103c8t6_blue_pill/weight_scale_hx711/main.cpp create mode 100644 examples/stm32f103c8t6_blue_pill/weight_scale_hx711/openocd.cfg create mode 100644 examples/stm32f103c8t6_blue_pill/weight_scale_hx711/project.xml diff --git a/examples/stm32f103c8t6_blue_pill/weight_scale_hx711/main.cpp b/examples/stm32f103c8t6_blue_pill/weight_scale_hx711/main.cpp new file mode 100644 index 0000000000..af9e66b0f5 --- /dev/null +++ b/examples/stm32f103c8t6_blue_pill/weight_scale_hx711/main.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2020, Sascha Schade + * + * This file is part of the modm project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +// ---------------------------------------------------------------------------- + +#include +#include +#include +#include +#include +using namespace modm::literals; + +using namespace Board; + +// ---------------------------------------------------------------------------- +// Set the log level +#undef MODM_LOG_LEVEL +#define MODM_LOG_LEVEL modm::log::DEBUG + +// Create an IODeviceWrapper around the Uart Peripheral we want to use +modm::IODeviceWrapper< Usart1, modm::IOBuffer::BlockIfFull > loggerDevice; + +// Set all four logger streams to use the UART +modm::log::Logger modm::log::debug(loggerDevice); +modm::log::Logger modm::log::info(loggerDevice); +modm::log::Logger modm::log::warning(loggerDevice); +modm::log::Logger modm::log::error(loggerDevice); + + +struct hx711_config : public modm::hx711::Config +{ + using Sck = GpioOutputA9; + using Data = GpioInputA10; + // static const modm::hx711::InputChannelAndGain mode = modm::hx711::InputChannelAndGain::ChA_64; +}; +using Hx711 = modm::Hx711< hx711_config >; + +class Hx711Thread : public modm::pt::Protothread +{ +public: + bool + run() + { + PT_BEGIN(); + + while (true) + { + result = PT_CALL(hx711.singleConversion()); + MODM_LOG_DEBUG.printf("%" PRIi32 "\n", result); + } + + PT_END(); + } + +protected: + Hx711 hx711; + int32_t result; +}; + +Hx711Thread hx711_thread; + + +class BlinkThread : public modm::pt::Protothread +{ +public: + bool + run() + { + PT_BEGIN(); + + while (true) { + PT_WAIT_UNTIL(timer.execute()); + LedGreen::toggle(); + } + + PT_END(); + } + +protected: + modm::ShortPeriodicTimer timer{1'000}; +}; + +BlinkThread blink_thread; + + +/* + * Blinks the green user LED with 1 Hz while measuring. + * + * Make the following connections to HX711: + * Data PA10 + * Clock PA9 + */ +int +main() +{ + Board::initialize(); + + // Initialize Uart1 for MODM_LOG_* + Usart1::connect(); + Usart1::initialize(); + + // Use the logging streams to print some messages. + MODM_LOG_DEBUG << "HX711 demo" << modm::endl; + + hx711_config::Sck::setOutput(); + hx711_config::Data::setInput(); + + LedGreen::set(); + + while (true) + { + blink_thread.run(); + hx711_thread.run(); + } + + return 0; +} diff --git a/examples/stm32f103c8t6_blue_pill/weight_scale_hx711/openocd.cfg b/examples/stm32f103c8t6_blue_pill/weight_scale_hx711/openocd.cfg new file mode 100644 index 0000000000..b157e7e432 --- /dev/null +++ b/examples/stm32f103c8t6_blue_pill/weight_scale_hx711/openocd.cfg @@ -0,0 +1,2 @@ +# Replace this with your custom programmer +source [find interface/stlink-v2.cfg] diff --git a/examples/stm32f103c8t6_blue_pill/weight_scale_hx711/project.xml b/examples/stm32f103c8t6_blue_pill/weight_scale_hx711/project.xml new file mode 100644 index 0000000000..fb6f88f446 --- /dev/null +++ b/examples/stm32f103c8t6_blue_pill/weight_scale_hx711/project.xml @@ -0,0 +1,16 @@ + + modm:blue-pill + + + + + + modm:debug + modm:platform:gpio + modm:platform:uart:1 + modm:processing:timer + modm:processing:protothread + modm:build:scons + modm:driver:hx711 + +