Skip to content

Commit

Permalink
[example] Add F746 TMP102 example
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Nov 6, 2018
1 parent 6b870ed commit 804516a
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
117 changes: 117 additions & 0 deletions examples/stm32f746g_discovery/tmp102/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2014, Sascha Schade
* Copyright (c) 2014-2017, Niklas Hauser
*
* 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 <modm/board.hpp>

#include <modm/processing/timer.hpp>
#include <modm/processing/protothread.hpp>
#include <modm/driver/temperature/tmp102.hpp>

typedef I2cMaster1 MyI2cMaster;


class ThreadOne : public modm::pt::Protothread
{
public:
ThreadOne()
: temp(temperatureData, 0x48)
{
}

bool
update()
{
temp.update();

PT_BEGIN();

// ping the device until it responds
while(true)
{
// we wait until the task started
if (PT_CALL(temp.ping()))
break;
// otherwise, try again in 100ms
this->timeout.restart(100);
PT_WAIT_UNTIL(this->timeout.isExpired());
}


PT_CALL(temp.setUpdateRate(200));
PT_CALL(temp.enableExtendedMode());

PT_CALL(temp.configureAlertMode(
modm::tmp102::ThermostatMode::Comparator,
modm::tmp102::AlertPolarity::ActiveLow,
modm::tmp102::FaultQueue::Faults6));
PT_CALL(temp.setLowerLimit(28.f));
PT_CALL(temp.setUpperLimit(30.f));

while (true)
{
{
PT_CALL(temp.readComparatorMode(result));
float temperature = temperatureData.getTemperature();
uint8_t tI = (int) temperature;
uint16_t tP = (temperature - tI) * 10000;
MODM_LOG_INFO << "T= " << tI << ".";
if (tP == 0)
{
MODM_LOG_INFO << "0000 C";
}
else if (tP == 625)
{
MODM_LOG_INFO << "0" << tP << " C";
}
else
{
MODM_LOG_INFO << tP << " C";
}
if (result) { MODM_LOG_INFO << " Heat me up!"; }
MODM_LOG_INFO << modm::endl;
}
this->timeout.restart(200);
PT_WAIT_UNTIL(this->timeout.isExpired());
Board::LedD13::toggle();
}

PT_END();
}

private:
bool result;
modm::ShortTimeout timeout;
modm::tmp102::Data temperatureData;
modm::Tmp102<MyI2cMaster> temp;
};

ThreadOne one;

// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();
Board::LedD13::setOutput(modm::Gpio::Low);

MyI2cMaster::connect<Board::D14::Sda, Board::D15::Scl>();
MyI2cMaster::initialize<Board::systemClock, 400'000>();

MODM_LOG_INFO << "\n\nRESTART\n\n";

while (1)
{
one.update();
}

return 0;
}
13 changes: 13 additions & 0 deletions examples/stm32f746g_discovery/tmp102/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<library>
<extends>modm:board:disco-f746ng</extends>
<options>
<option name=":build:build.path">../../../build/stm32f746g_discovery/tmp102</option>
</options>
<modules>
<module>:driver:tmp102</module>
<module>:io</module>
<module>:platform:i2c:1</module>
<module>:processing:protothread</module>
<module>:build:scons</module>
</modules>
</library>

0 comments on commit 804516a

Please sign in to comment.