Initial commit

This commit is contained in:
ftc
2020-04-19 10:21:55 +03:00
commit 0b95847b30
7 changed files with 334 additions and 0 deletions
+112
View File
@@ -0,0 +1,112 @@
#include "grove.h"
#include "jhd1313m1.h"
#include "mraa/common.hpp"
#include "mraa/gpio.hpp"
#include <climits>
#include <iostream>
#include <sstream>
#include <unistd.h>
#define GPIO_PIN_1 2
void temperature_update(upm::GroveTemp* temperature_sensor, upm::Jhd1313m1 *lcd)
{
// minimum and maximum temperatures registered, the initial values will be
// replaced after the first read
static int min_temperature = INT_MAX;
static int max_temperature = INT_MIN;
// the temperature range in degrees Celsius,
// adapt to your room temperature for a nicer effect!
const int TEMPERATURE_RANGE_MIN_VAL = 18;
const int TEMPERATURE_RANGE_MAX_VAL = 31;
// other helper variables
int temperature; // temperature sensor value in degrees Celsius
float fade; // fade value [0.0 .. 1.0]
uint8_t r, g, b; // resulting LCD backlight color components [0 .. 255]
std::stringstream row_1, row_2; // LCD rows
// update the min and max temperature values
temperature = temperature_sensor->value();
if (temperature < min_temperature) {
min_temperature = temperature;
}
if (temperature > max_temperature) {
max_temperature = temperature;
}
// display the temperature values on the LCD
row_1 << "Temp " << temperature << " ";
row_2 << "Min " << min_temperature << " Max " << max_temperature << " ";
lcd->setCursor(0,0);
lcd->write(row_1.str());
lcd->setCursor(1,0);
lcd->write(row_2.str());
// set the fade value depending on where we are in the temperature range
if (temperature <= TEMPERATURE_RANGE_MIN_VAL) {
fade = 0.0;
} else if (temperature >= TEMPERATURE_RANGE_MAX_VAL) {
fade = 1.0;
} else {
fade = (float)(temperature - TEMPERATURE_RANGE_MIN_VAL) /
(TEMPERATURE_RANGE_MAX_VAL - TEMPERATURE_RANGE_MIN_VAL);
}
// fade the color components separately
r = (int)(255 * fade);
g = (int)(64 * fade);
b = (int)(255 * (1 - fade));
// apply the calculated result
lcd->setColor(r, g, b);
}
int main()
{
// check that we are running on Galileo or Edison
mraa_platform_t platform = mraa_get_platform_type();
if ((platform != MRAA_INTEL_GALILEO_GEN1) &&
(platform != MRAA_INTEL_GALILEO_GEN2) &&
(platform != MRAA_INTEL_EDISON_FAB_C))
{
std::cerr << "Unsupported platform, exiting" << std::endl;
return MRAA_ERROR_INVALID_PLATFORM;
}
mraa::Gpio gpio(GPIO_PIN_1);
mraa_result_t status = gpio.dir(mraa::DIR_OUT);
if (status != MRAA_SUCCESS)
{
return EXIT_FAILURE;
}
// temperature sensor connected to A0 (analog in)
upm::GroveTemp* temp_sensor = new upm::GroveTemp(0);
// LCD connected to the default I2C bus
upm::Jhd1313m1* lcd = new upm::Jhd1313m1(0);
if ((temp_sensor == NULL) || (lcd == NULL))
{
std::cerr << "Can't create all objects, exiting" << std::endl;
return MRAA_ERROR_UNSPECIFIED;
}
int io_value = 1;
// loop forever updating the temperature values every second
for (;;) {
temperature_update(temp_sensor, lcd);
gpio.write(io_value);
io_value = 1 - io_value;
std::cerr << io_value << "\n";
sleep(1);
}
return MRAA_SUCCESS;
}