本帖最后由 anger0925 于 2016-8-11 17:13 编辑
一,具体实现: 1,通过web界面设置闹钟时间; 2,LCD上显示实时时间; 3,保存每天唤醒你所花的时间。 二,工作原理: 该闹钟具有多项实用性,可以通过 手机web设置闹钟;闹钟到点后,蜂鸣器响起,LCD背景显示红色;LCD上显示实时时间;按键停止闹钟,保存闹钟响铃时间。三,硬件搭建 使用Grove* Starter Kit Plus套件,包括 1,Arduino breakout套件; 2,Grove按键; 3,Grove蜂鸣器; 4,Grove RGB LCD显示器; 组装: 1,Grove按键连接到D6上; 2,Grove蜂鸣器连接在D5上; 3,Grove RGB LCD显示器接在任意一个I2C接口上。
三,源码
- #include
- #include
- #include
- #include
- #include
- #include
- #include time>
- #include
- #include
- #include "../lib/restclient-cpp/include/restclient-cpp/restclient.h"
- #include "datastore.h"
- #include "mqtt.h"
- #include "../lib/crow/crow_all.h"
- #include "index_html.h"
- #include "styles_css.h"
- #include
- struct Devices
- {
- upm::GroveRotary* rotary;
- upm::GroveButton* button;
- upm::Buzzer* buzzer;
- upm::Jhd1313m1* screen;
- upm::GroveTemp* temp;
- Devices(){
- };
- void init() {
- button = new upm::GroveButton(6);
- buzzer = new upm::Buzzer(5);
- stop_buzzing();
- screen = new upm::Jhd1313m1(0);
- };
- void cleanup() {
- delete button;
- delete buzzer;
- delete screen;
- }
- void adjust_brightness(int shile)
- {
- std::size_t red ;
- std::size_t green;
- std::size_t blue;
- if(shile == 1)
- {
- red = 255;
- green = 255;
- blue = 255;
- }
- else if(shile == 2)
- {
- red = 255;
- green = 0;
- blue = 0;
- }
- screen->setColor(red, green, blue);
- }
- //显示时间和温度
- void display_time() {
- time_t rawtime;
- struct tm* timeinfo;
- char buffer[80];
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- //timeinfo->tm_hour = timeinfo->tm_hour+8;
- strftime(buffer, 80, "%H:%M:%S", timeinfo);
- std::string str(buffer);
- message(str, 0x00ff00);
- }
- //LCD上显示信息
- void message(const std::string& input, const std::size_t color = 0x0000ff) {
- std::size_t red = (color & 0xff0000) >> 16;
- std::size_t green = (color & 0x00ff00) >> 8;
- std::size_t blue = (color & 0x0000ff);
- std::string text(input);
- text.resize(16, ' ');
- screen->setCursor(0,0);
- screen->write(text);
- }
- //闹铃响起
- void start_buzzing() {
- buzzer->setVolume(0.5);
- buzzer->playSound(2600, 0);
- }
- //闹铃停止
- void stop_buzzing() {
- buzzer->setVolume(0);
- buzzer->stopSound();
- buzzer->stopSound();
- }
- };
- bool alARMRinging = false;
- std::time_t alarmTime ;
- //时间差
- double countdown(std::time_t& target) {
- time_t rawtime;
- struct tm* timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- return std::difftime(mktime(timeinfo), target);
- }
- //计算闹铃的时间
- double elapsed(std::time_t& target) {
- return countdown(target);
- }
- //监测是否到闹铃时间
- bool time_for_alarm(std::time_t& alarm) {
- double remaining = countdown(alarm);
- if (remaining > 0 && remaining < 5 && !alarmRinging) {
- return true;
- } else return false;
- }
- //被闹铃叫唤醒来的时间,保存数据
- void log_wakeup() {
- double duration = elapsed(alarmTime);
- std::cerr << "Alarm duration: " << std::to_string(duration) << std::endl;
- std::stringstream text;
- text << "{"value": "" << std::to_string(duration) << ""}";
- log_mqtt(text.str());
- log_datastore(text.str());
- }
- void runner(Devices& devices, std::time_t& alarmTime) {
- int tick = true;
- for (;;) {
- devices.display_time();
- if (time_for_alarm(alarmTime)) {
- alarmRinging = true;
- }
- if (alarmRinging) {
- if ( devices.button->value() ) {
- alarmRinging = false;
- devices.stop_buzzing();
- devices.stop_buzzing();
- log_wakeup();
- struct tm* timeinfo;
- timeinfo = localtime(&alarmTime);
- timeinfo->tm_mday++;
- alarmTime = mktime(timeinfo);
- std::cerr << "New alarm time: " << alarmTime << std::endl;
- devices.adjust_brightness(1);
- } else {
- devices.start_buzzing();
- devices.adjust_brightness(tick ? 1 : 2);
- // if (tick) { devices.stop_buzzing(); } else { devices.start_buzzing(); }
- tick = !tick;
- }
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(1000));
- }
- }
- Devices devices;
- void exit_handler(int param)
- {
- devices.cleanup();
- exit(1);
- }
- int main() {
-
- signal(SIGINT, exit_handler);
- 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 << "ERROR: Unsupported platform" << std::endl;
- return MRAA_ERROR_INVALID_PLATFORM;
- }
- devices.init();
- std::thread t1(runner, std::ref(devices), std::ref(alarmTime));
- crow::SimpleApp app;
- CROW_ROUTE(app, "/")
- ([](const crow::request& req) {
- bool timeChanged = false;
- std::time_t newTime = std::time(NULL);
- struct tm* timeinfo;
- timeinfo = localtime(&newTime);
- if(req.url_params.get("hour") != nullptr) {
- double hour = boost::lexical_cast(req.url_params.get("hour"));
- timeinfo->tm_hour = hour;
- timeChanged = true;
- }
- if(req.url_params.get("minute") != nullptr) {
- double minute = boost::lexical_cast(req.url_params.get("minute"));
- timeinfo->tm_min = minute;
- timeChanged = true;
- }
- if(req.url_params.get("second") != nullptr) {
- double second = boost::lexical_cast(req.url_params.get("second"));
- timeinfo->tm_sec = second;
- timeChanged = true;
- }
- if (timeChanged) {
- alarmTime = mktime(timeinfo);
- std::cerr << "Set alarm time: " << alarmTime << std::endl;
- }
- std::stringstream text;
- text << index_html;
- return text.str();
- });
- CROW_ROUTE(app, "/alarm.json")
- ([]() {
- struct tm* timeinfo;
- timeinfo = localtime(&alarmTime);
- crow::json::wvalue a;
- a["hour"] = timeinfo->tm_hour;
- a["minute"] = timeinfo->tm_min;
- a["second"] = timeinfo->tm_sec;
- return a;
- });
- CROW_ROUTE(app, "/styles.css")
- ([]() {
- std::stringstream text;
- text << styles_css;
- return text.str();
- });
- // start web server
- app.port(3000).multithreaded().run();
- t1.join();
- return MRAA_SUCCESS;
- }
复制代码
四,结果 首先在打开手机或者pc机web设置闹铃时间。
Lcd上事实显示当前时间
到闹铃设置时间后,lcd背景红白开始闪烁。蜂鸣器响起。 |