ich habe auf der Basis von Bernhard's Vorlage den Demon für die Steuerung des 1,8" TFT
erweitert. Vielleicht ist das ja für den einen oder anderen interessant.
Code:
/*
* TFT ST7735 Display Demon for RBF iRadio
*
* Created: 01.01.2019
* Author : HolgerK
*
* Revision: 1.1 05.01.2019
*
*/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "wiringPi.h"
#include "wiringPiSPI.h"
#include "tft_st7735.h"
#include "tft_manager.h"
#include "tft_field.h"
#include <bcm2835.h>
using namespace std;
void rmSubstr(char *str, const char *toRemove); //prototype
int main (void)
{
// set SPI parameter, channel=0, rsPin=24, rstPin=25, speed 32MHz)
TFT_ST7735 tft = *new TFT_ST7735(0, 24, 25, 32000000);
TFT_manager manager = *new TFT_manager();
// define TFT fields
TFT_field f_title = *new TFT_field (tft, 2, 3, 159, 23, TFT_WHITE, 2, TFT_BLUE, false);
TFT_field f_now = *new TFT_field (tft, 2, 33, 159, 23, TFT_WHITE, 2, TFT_BLUE, false);
TFT_field f_genre = *new TFT_field (tft, 55, 70, 95, 10, TFT_BLACK, 1, TFT_YELLOW, false);
TFT_field f_channelmode = *new TFT_field (tft, 10, 70, 40, 10, TFT_BLACK, 1, TFT_RED, false);
TFT_field f_streamdata = *new TFT_field (tft, 10, 85, 140, 10, TFT_BLACK, 1, TFT_GREEN, false);
TFT_field f_date_time = *new TFT_field (tft, 1, 103, 159, 23, TFT_BLUE, 2, TFT_WHITE, false);
manager.add(&f_title);
manager.add(&f_now);
manager.add(&f_streamdata);
manager.add(&f_genre);
manager.add(&f_channelmode);
manager.add(&f_date_time);
struct tm *datetime;
time_t second;
char title[64], title_old[64], title_buffer[14];
char now[64], now_old[64], now_buffer[64];
char genre[64], genre_old[64];
char bitrate[64], bitrate_old[64];
char samplerate[64], samplerate_old[64];
char channelmode[64], channelmode_old[64];
char streamdata[30], vlc_status[8], vlc_status_old[8];
char date_time[14], date_time_old[14];
char i =0, j =0;
char space[6] =" ";
FILE *text, *text_now_playing, *text_genre, *text_bitrate, *text_samplerate, *text_channelmode, *text_vlc_status;
wiringPiSetupGpio(); // initialize wiringPi and wiringPiGpio
tft.commonInit(); // initialize SPI and reset display
tft.initR(); // initialize display
tft.setRotation(DEGREE_90); // set display rotation
tft.setBackground(TFT_BLUE); // set background colour
tft.clearScreen(); // reset Display
while (1)
{
text = popen("echo \"info\" | nc 127.0.0.1 9294 -N | grep title:", "r"); // open pipe
text_now_playing = popen("echo \"info\" | nc 127.0.0.1 9294 -N | grep now_playing:", "r");
text_genre = popen("echo \"info\" | nc 127.0.0.1 9294 -N | grep genre:", "r");
text_bitrate = popen("echo \"info\" | nc 127.0.0.1 9294 -N | grep Bitrate:", "r");
text_samplerate = popen("echo \"info\" | nc 127.0.0.1 9294 -N | grep 'Sample rate':", "r");
text_channelmode = popen("echo \"info\" | nc 127.0.0.1 9294 -N | grep Channels:", "r");
text_vlc_status = popen("echo \"status\" | nc 127.0.0.1 9294 -N | grep state", "r");
fgets(title, 24, text);
fgets(now, 64, text_now_playing); // read from stream
strcpy(genre, " ");
fgets(genre, 64, text_genre);
strcpy(bitrate, " ");
fgets(bitrate, 64, text_bitrate);
strcpy(samplerate, " ");
fgets(samplerate, 64, text_samplerate);
strcpy(channelmode, " ");
fgets(channelmode, 64, text_channelmode);
fgets(vlc_status, 64, text_vlc_status);
if (text != NULL) // close pipe
{
pclose(text);
text = NULL;
}
if (text_now_playing != NULL)
{
pclose(text_now_playing);
text_now_playing = NULL;
}
if (text_genre != NULL )
{
pclose(text_genre);
text_genre = NULL;
}
if (text_bitrate != NULL )
{
pclose(text_bitrate);
text_bitrate = NULL;
}
if (text_channelmode != NULL )
{
pclose(text_channelmode);
text_channelmode = NULL;
}
if (text_samplerate != NULL )
{
pclose(text_samplerate);
text_samplerate = NULL;
}
if (text_vlc_status != NULL )
{
pclose(text_vlc_status);
text_vlc_status = NULL;
}
rmSubstr(title,"| title: "); // trim string
rmSubstr(title,"\r");
rmSubstr(now,"| now_playing: ");
rmSubstr(now,"\r");
rmSubstr(genre,"| genre: ");
rmSubstr(genre,"\r");
rmSubstr(bitrate,"| Bitrate: ");
rmSubstr(bitrate,"\r");
rmSubstr(samplerate,"| Sample rate: ");
rmSubstr(samplerate,"\r");
rmSubstr(channelmode,"| Channels: ");
rmSubstr(channelmode,"\r");
rmSubstr(vlc_status,"( state ");
rmSubstr(vlc_status," )\r");
title[13] = '\0'; // set string end
now[30] = '\0';
now_buffer[13] = '\0';
genre[19] = '\0';
bitrate[20] = '\0';
samplerate[20] = '\0';
channelmode[20] = '\0';
vlc_status[8] = '\0';
if(strlen(title) <14) // strip \r if title is short
{
title[strlen(title)-1] ='\0';
}
now[strlen(now)-1]='\0'; // set string end
genre[strlen(genre)-1]='\0';
bitrate[strlen(bitrate)-1]='\0';
samplerate[strlen(samplerate)-1]='\0';
channelmode[strlen(channelmode)-1]='\0';
vlc_status[strlen(vlc_status)-1]='\0';
if ((strcmp(vlc_status, "playing") ==0) ||(strcmp(vlc_status, "paused") ==0))
{
if ((strcmp(title,title_old) !=0) ||(strcmp(now,now_old)!=0))
{
strncpy(title_buffer, space,(13-strlen(title)) /2); // center title
title_buffer[((13-strlen(title)) /2)]='\0';
strcat(title_buffer, title);
f_title.setValue(title_buffer); // show title
strcpy(title_old,title);
tft.drawHorizontalLine(2, 28, 156, TFT_WHITE);
}
if (strcmp(now,now_old)!=0) // clear now if changed
{
i =0;
now_buffer[0] = '\0';
}
if (strcmp(genre,genre_old)!=0) // show genre
{
f_genre.setValue(genre);
strcpy(genre_old,genre);
}
if (strcmp(channelmode,channelmode_old)!=0) // show channelmode
{
f_channelmode.setValue(channelmode);
strcpy(channelmode_old,channelmode);
}
// format + show streamdata
if ((strcmp(bitrate,bitrate_old)!=0) ||(strcmp(samplerate,samplerate_old)!=0))
{
strcpy(streamdata,bitrate);
strcat(streamdata," @");
strcat(streamdata, samplerate);
f_streamdata.setValue(streamdata);
strcpy(bitrate_old,bitrate);
strcpy(samplerate_old,samplerate);
}
if (strcmp(vlc_status, "playing") ==0)
{
if (strlen(now) >13) // scroll now
{
for (j=0; j< 13; j++)
{
now_buffer[j] = now[j +i];
}
i++;
if (i >(strlen(now) -11)) i =0;
}
else strcpy(now_buffer, now); // show now
f_now.setValue(now_buffer);
strcpy(now_old,now);
strcpy(vlc_status_old,vlc_status);
}
else if (strcmp(vlc_status, "paused") ==0) // vlc paused
{
f_now.setValue(" vlc paused");
strcpy(vlc_status_old,vlc_status);
}
}
else if (strcmp(vlc_status, "stopped") ==0) // vlc stopped
{
f_now.setValue(" vlc stopped");
f_title.setValue(" ");
strcpy(title_old,title);
f_genre.setValue(genre);
strcpy(genre_old,genre);
f_channelmode.setValue(channelmode);
strcpy(channelmode_old,channelmode);
f_streamdata.setValue(streamdata);
strcpy(bitrate_old,bitrate);
strcpy(samplerate_old,samplerate);
strcpy(vlc_status_old,vlc_status);
}
time(&second); // get date + time
datetime = localtime(&second);
strftime(date_time, 14,"%d.%m. %H:%M",datetime);
if (strcmp(date_time, date_time_old) !=0) // show date + time
f_date_time.setValue(date_time);
strcpy(date_time_old, date_time);
manager.refresh();
delay(500);
}
}
void rmSubstr(char *str, const char *toRemove)
{
size_t length = strlen(toRemove);
while((str = strstr(str, toRemove)))
{
memmove(str, str + length, 1 + strlen(str + length));
}
}