Home | History | Annotate | Download | only in c++
      1 /*
      2  * Author: Mihai Tudor Panu <mihai.tudor.panu (at) intel.com>
      3  * Copyright (c) 2015 Intel Corporation.
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining
      6  * a copy of this software and associated documentation files (the
      7  * "Software"), to deal in the Software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sublicense, and/or sell copies of the Software, and to
     10  * permit persons to whom the Software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 #include "tm1637.h"
     26 #include <signal.h>
     27 #include <unistd.h>
     28 #include <sstream>
     29 #include <time.h>
     30 
     31 using namespace std;
     32 using namespace upm;
     33 
     34 bool run = true;
     35 
     36 void sig_handler(int signo)
     37 {
     38   if (signo == SIGINT)
     39     run = false;
     40 }
     41 
     42 int
     43 main(int argc, char** argv)
     44 {
     45 	//! [Interesting]
     46 	bool point = true;
     47 	int timezone = -7; // Your UTC offset
     48 	time_t rawtime;
     49 	struct tm * gmt;
     50 	char myTime[5];
     51 
     52     fprintf(stdout, "TM1637 Display Example\n");
     53     signal(SIGINT, sig_handler);
     54 
     55     TM1637 myDisplay = TM1637(0, 1); // TM1637 on pins 0 (clk) and 1 (dio)
     56     myDisplay.write(0x39, 0x09, 0x09); // Start a box using 7-segment encoding
     57     myDisplay.writeAt(3, ']'); // Finish box using writeAt function
     58     sleep(3); // Wait 3 seconds
     59 
     60     while(run)
     61     {
     62     	time(&rawtime); // Update raw time
     63         gmt = gmtime(&rawtime); // Get current time
     64 
     65         int hour = (gmt) ? gmt->tm_hour : 0;
     66         int min = (gmt) ? gmt->tm_min : 0;
     67         // Format and store the time in 24 hour format
     68         snprintf(myTime, 5, "%2d%02d", (hour + timezone + 24) % 24, min);
     69 
     70         myDisplay.write(myTime); // Write to display as string
     71     	myDisplay.setColon(point ^= true); // Toggle the dots on the display
     72     	sleep(1); // Only update once every second
     73     }
     74 
     75 	//! [Interesting]
     76 	return 0;
     77 }
     78 
     79 
     80