Skip to content

Instantly share code, notes, and snippets.

@kenci
Created October 18, 2015 12:58
Show Gist options
  • Save kenci/199322f7e513b58dfb83 to your computer and use it in GitHub Desktop.
Save kenci/199322f7e513b58dfb83 to your computer and use it in GitHub Desktop.
TM1608 Display
/**
* gcc -std=c99 clk.c -o clk -lbcm2835 -ltm1638
*
* @file
* @brief Hello World for TM1638 based displays
* @author Martin Oldfield <[email protected]>
* @version 0.1
*
* @section DESCRIPTION
*
* A simple "Hello World" example program for the TM1638.
*
* @section HARDWARE
*
* The code hard wires pin connections:
*
* * data: GPIO 17
* * clock: GPIO 21
* * strobe: GPIO 22
*
* @section LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details at
* http://www.gnu.org/copyleft/gpl.html
*
*/
/** @cond NEVER */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <bcm2835.h>
#include <tm1638.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
double ds1820read(char *sensorid) {
double temp;
char crc_buffer[64], temp_buffer[64], fn[128];
char crc_ok[] = "YES";
FILE *fp;
sprintf(fn, "/sys/bus/w1/devices/%s/w1_slave", sensorid );
if ( (fp = fopen ( fn, "r" )) == NULL )
{
return(-1);
}
else
{
fgets( crc_buffer, sizeof(crc_buffer), fp );
if ( !strstr ( crc_buffer, crc_ok ) )
{
syslog(LOG_INFO, "CRC check failed, SensorID: %s", sensorid);
fclose ( fp );
return(-1);
}
else
{
fgets( temp_buffer, sizeof(temp_buffer), fp );
char *pos = strstr(temp_buffer, "t=");
if (pos == NULL) return -1;
pos += 2;
temp = atof(pos)/1000;
fclose ( fp );
//printf("Abfrage der Temperatur");
return temp;
}
}
}
int main(int argc, char *argv[])
{
tm1638_p t;
if (!bcm2835_init())
{
printf("Unable to initialize BCM library\n");
return -1;
}
t = tm1638_alloc(23, 24, 25);
if (!t)
{
printf("Unable to allocate TM1638\n");
return -2;
}
while (1)
{
char tempPool[20];
char tempSolar[20];
char tempAussen[20];
int starttime;
snprintf(tempPool, sizeof(tempPool), "P %.2fC", ds1820read("28-000005569d94"));
snprintf(tempSolar, sizeof(tempSolar), "S %.2fC", ds1820read("28-000005e30f15"));
snprintf(tempAussen, sizeof(tempAussen), "A %.2fC", ds1820read("28-000005876396"));
starttime = (int)time(NULL);
while(starttime+900 > (int)time(NULL))
{
//printf("Display-Schleife\r\n");
time_t now = time(NULL);
struct tm *tm = localtime(&now);
char text[9];
snprintf(text, 8, " %02d %02d", tm->tm_hour, tm->tm_min);
tm1638_set_7seg_text(t, text, 0x00);
sleep(5);
tm1638_send_cls(t);
sleep(1);
tm1638_set_7seg_text(t, tempPool, 0x00);
sleep(10);
tm1638_send_cls(t);
sleep(1);
tm1638_set_7seg_text(t, tempAussen, 0x00);
sleep(5);
tm1638_send_cls(t);
sleep(1);
tm1638_set_7seg_text(t, tempSolar, 0x00);
sleep(5);
tm1638_send_cls(t);
sleep(1);
tm1638_set_7seg_text(t, "powered", 0x00);
sleep(2);
tm1638_set_7seg_text(t, " by ", 0x00);
sleep(2);
tm1638_set_7seg_text(t, "kenosys", 0x00);
sleep(5);
tm1638_send_cls(t);
sleep(1);
}
//tm1638_free(&t);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment