1)首先读取
开发板类型,如果不是对应开发板平台,退出。
2)定义端口
upm::GroveButton* button = new upm::GroveButton(4);
upm::GroveLed* led = new upm::GroveLed(3);
upm::GroveTemp* temp_sensor = new upm::GroveTemp(1);
upm::Jhd1313m1* lcd = new upm::Jhd1313m1(0);
3)然后判断每个接口是否创建成功。若失败,退出。
4)For循环间隔1s执行temperature_update(temp_sensor, button, led, lcd)函数。
void temperature_update(upm::GroveTemp* temperature_sensor, upm::GroveButton* button,
upm::GroveLed* led, upm::Jhd1313m1 *lcd)
{
// minimum and maximum temperatures registered, the ini
tial 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, reset them if the button is
// being pushed
temperature = temperature_sensor->value();
if (button->value() == 1) {
min_temperature = temperature;
max_temperature = temperature;
} else {
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));
// blink the led for 50 ms to show the temperature was actually sampled
led->on();
usleep(50000);
led->off();
// apply the calculated result
lcd->setColor(r, g, b);
}
temperature_update函数实现的功能:
1)读取温度值temperature = temperature_sensor->value();
2)统计温度最大值和最小值,使用按键实现初始化最大值和最小值。
3)LCD显示,第一行显示实时温度值。第二行显示最小值和最大值。
4)根据温度变化改变LCD的背景颜色。设置一个温度范围18~31.小于18,显示一种背景;大于31显示一种背景;在18~31之间,显示一种背景。
5,编译、上传、运行
编译时弹出一个错误
e:/intel_edison/eclipse/iss-iot-win_03-14-16/iss-iot-win/devkit-x86/sysroots/x86_64-pokysdk-mingw32/usr/bin/i586-poky-linux/../../libexec/i586-poky-linux/gcc/i586-poky-linux/4.9.1/ld.exe: cannot find -lupm-i2clcd
e:/intel_edison/eclipse/iss-iot-win_03-14-16/iss-iot-win/devkit-x86/sysroots/x86_64-pokysdk-mingw32/usr/bin/i586-poky-linux/../../libexec/i586-poky-linux/gcc/i586-poky-linux/4.9.1/ld.exe: cannot find -lupm-grove
collect2.exe: error: ld returned 1 exit status
6,错误解决
在官网上下载了新版的eclipse:iss-iot-win_03-24-16到intel英文官网上去下载的好像才是最新版。
运行iss-iot-win_03-24-16iss-iot-win下的iss-iot-launcher.bat。初次打开仍然需要更新库:
等待更新完成后编译还是有错:
E:Intel_Edisoneclipse_workGrove_Temper_LCDDebug/..src/Grove_Temper_LCD.cpp:161: undefined reference to `upm::GroveTemp::GroveTemp(unsigned int)'
通过重新加载grove库。
加载时点击yes。
加载后头文件变为:
这时编译还有错误。把头文件#include "grove.h"去掉,编译通过。终于解决问题。