Demos restructuring. Added term demo.
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <wiringPi.h>
|
||||
#include "spilcd.h"
|
||||
#include "spilcd_gfx.h"
|
||||
#include "base.h"
|
||||
|
||||
inline uint8 random_color_r(int seed) { return seed % (256 / 7) * 7; }
|
||||
inline uint8 random_color_g(int seed) { return seed % (256 / 13) * 13; }
|
||||
inline uint8 random_color_b(int seed) { return seed % (256 / 23) * 23; }
|
||||
|
||||
lcd_t* demo_init()
|
||||
{
|
||||
setbuf(stdout, NULL);
|
||||
srand(time(NULL));
|
||||
|
||||
wiringPiSetup();
|
||||
|
||||
return lcd_init(40000000, 1, 10, 7, 8);
|
||||
}
|
||||
|
||||
void demo_deinit(lcd_t* lcd)
|
||||
{
|
||||
printf("...waiting 2 seconds before shutdown...\n");
|
||||
sleep(2);
|
||||
|
||||
printf("Terminating...\n");
|
||||
lcd_fillScreen(lcd, 0, 0, 0);
|
||||
lcd_deinit(lcd);
|
||||
printf("DONE\n");
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <wiringPi.h>
|
||||
#include "spilcd.h"
|
||||
|
||||
uint8 random_color_r(int seed);
|
||||
uint8 random_color_g(int seed);
|
||||
uint8 random_color_b(int seed);
|
||||
|
||||
lcd_t* demo_init();
|
||||
void demo_deinit(lcd_t* lcd);
|
||||
|
||||
Executable
+1295
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,56 @@
|
||||
#include "base.h"
|
||||
#include "spilcd_gfx.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
lcd_t* lcd = demo_init();
|
||||
|
||||
printf("Fill display...");
|
||||
printf("blue...");
|
||||
lcd_fillScreen(lcd, 0, 70, 160);
|
||||
lcd_redrawBuffer(lcd);
|
||||
printf("...waiting 1 second...");
|
||||
sleep(1);
|
||||
printf("black...");
|
||||
lcd_fillScreen(lcd, 0, 0, 0);
|
||||
lcd_redrawBuffer(lcd);
|
||||
printf("DONE\n");
|
||||
|
||||
printf("...waiting 1 second...\n");
|
||||
sleep(1);
|
||||
|
||||
printf("Points...");
|
||||
for (int i = 1; i < 2000; i++) {
|
||||
int r = rand();
|
||||
lcd_drawPixel(lcd, r % 128, i % 160,
|
||||
random_color_r(i),
|
||||
random_color_g(i),
|
||||
random_color_b(i));
|
||||
}
|
||||
lcd_redrawBuffer(lcd);
|
||||
printf("DONE\n");
|
||||
|
||||
printf("...waiting 1 second...\n");
|
||||
sleep(1);
|
||||
|
||||
printf("Regions...");
|
||||
int w = 15;
|
||||
int h = 20;
|
||||
for (int i = 1; i < 100; i++) {
|
||||
int x = rand() % (128 - w);
|
||||
int y = rand() % (160 - h);
|
||||
lcd_setWindow(lcd, x, y, x + w - 1, y + h - 1);
|
||||
uint8 r = rand();
|
||||
uint8 g = rand();
|
||||
uint8 b = rand();
|
||||
for (int p = 0; p < w*h; p++) {
|
||||
lcd_pushPixel(lcd, r * p / (w*h), g * (w*h-p) / (w*h), b);
|
||||
}
|
||||
lcd_redrawBuffer(lcd);
|
||||
}
|
||||
printf("DONE\n");
|
||||
|
||||
demo_deinit(lcd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <linux/if_link.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "spilcd_gfx.h"
|
||||
#include "spilcd_font.h"
|
||||
#include "base.h"
|
||||
|
||||
|
||||
#define NI_MAXHOST 1025
|
||||
#define NI_MAXSERV 32
|
||||
|
||||
volatile sig_atomic_t done = 0;
|
||||
struct sigaction action;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
float GetCPULoad() {
|
||||
int FileHandler;
|
||||
char FileBuffer[1024];
|
||||
float load;
|
||||
|
||||
FileHandler = open("/proc/loadavg", O_RDONLY);
|
||||
if(FileHandler < 0) {
|
||||
return -1; }
|
||||
read(FileHandler, FileBuffer, sizeof(FileBuffer) - 1);
|
||||
sscanf(FileBuffer, "%f", &load);
|
||||
close(FileHandler);
|
||||
return load;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
float GetMemUsage() {
|
||||
int FileHandler;
|
||||
char FileBuffer[1024];
|
||||
int memTotal, memFree, memAvail, memBuf,memCached;
|
||||
float result;
|
||||
FileHandler = open("/proc/meminfo", O_RDONLY);
|
||||
if(FileHandler < 0) {
|
||||
return -1; }
|
||||
read(FileHandler, FileBuffer, sizeof(FileBuffer) - 1);
|
||||
sscanf(FileBuffer, "MemTotal: %d kB\n MemFree: %d kB\n MemAvailable: %d kB\n Buffers: %d kB\n Cached: %d kB",
|
||||
&memTotal, &memFree, &memAvail, &memBuf, &memCached);
|
||||
close(FileHandler);
|
||||
result = 1.0 - (float)(memFree + memCached) / memTotal;
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int GetCPUTemp() {
|
||||
int FileHandler;
|
||||
char FileBuffer[1024];
|
||||
int CPU_temp;
|
||||
FileHandler = open("/sys/devices/virtual/thermal/thermal_zone0/temp", O_RDONLY);
|
||||
if(FileHandler < 0) {
|
||||
return -1; }
|
||||
read(FileHandler, FileBuffer, sizeof(FileBuffer) - 1);
|
||||
sscanf(FileBuffer, "%d", &CPU_temp);
|
||||
close(FileHandler);
|
||||
return CPU_temp / 1000;
|
||||
}
|
||||
|
||||
void sig_handler(int signo)
|
||||
{
|
||||
done = 1;
|
||||
printf("received signo :%d \n", signo);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
int main(int argc, char** argv) {
|
||||
time_t mytime;
|
||||
struct tm *tm;
|
||||
uint8_t time_buffer[80];
|
||||
uint8_t text_buffer[100];
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
int family, s, n, row;
|
||||
char host[NI_MAXHOST];
|
||||
|
||||
|
||||
// Print pid, so that we can send signals from other shells
|
||||
printf("My pid is: %d\n", getpid());
|
||||
memset(&action, 0, sizeof(struct sigaction));
|
||||
action.sa_handler = sig_handler;
|
||||
// Intercept SIGHUP and SIGINT
|
||||
if (sigaction(SIGINT, &action, NULL) == -1) {
|
||||
perror("Error: cannot handle SIGINT"); // Should not happen
|
||||
}
|
||||
if (sigaction(SIGTERM, &action, NULL) == -1) {
|
||||
perror("Error: cannot handle SIGTERM"); // Should not happen
|
||||
}
|
||||
|
||||
lcd_t* lcd = demo_init();
|
||||
|
||||
int rowy = 10;
|
||||
while (done == 0) {
|
||||
row = 0;
|
||||
|
||||
// Date and Time
|
||||
mytime = time(NULL);
|
||||
tm = localtime (&mytime);
|
||||
lcd_fillScreen(lcd, 0, 0, 0);
|
||||
strftime(time_buffer, 80," %H:%M:%S", tm);
|
||||
lcd_drawText(lcd, 0, row * rowy, time_buffer, 255, 255, 255);
|
||||
row++;
|
||||
strftime(time_buffer, 80," %x", tm);
|
||||
lcd_drawText(lcd, 0, row * rowy, time_buffer, 200, 200, 200);
|
||||
row++;
|
||||
row++;
|
||||
|
||||
// CPU
|
||||
float c = GetCPULoad() ;
|
||||
lcd_drawText(lcd, 0, row * rowy, "CPU load:", 255, 255, 100);
|
||||
row++;
|
||||
snprintf(text_buffer, sizeof(text_buffer), " %0.2f", c);
|
||||
printf("CPU load avg: %s\n", text_buffer);
|
||||
if (c < 0.2) {
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 50, 255, 50);
|
||||
} else if (c < 0.5) {
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 255, 50);
|
||||
} else {
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 50, 50);
|
||||
}
|
||||
row++;
|
||||
|
||||
/* Memory usage */
|
||||
float m = GetMemUsage();
|
||||
snprintf(text_buffer, sizeof(text_buffer), " %3.0f%%", m*100);
|
||||
printf("Mem used: %s\n", text_buffer);
|
||||
lcd_drawText(lcd, 0, row * rowy, "Mem used:", 150, 150, 255);
|
||||
row++;
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 255, 255);
|
||||
//ssd1306DrawRect(0, 0, 127, 13, INVERSE, LAYER0);
|
||||
//ssd1306FillRect(2, 2, (int)(123 * m), 9, INVERSE, LAYER0);
|
||||
row++;
|
||||
|
||||
/* CPU temperature */
|
||||
int t = GetCPUTemp() ;
|
||||
snprintf ( text_buffer, sizeof(text_buffer), "%3d C", t );
|
||||
printf("CPU temp: %s\n", text_buffer);
|
||||
lcd_drawText(lcd, 0, row * rowy, "CPU temp:", 255, 180, 170);
|
||||
row++;
|
||||
if (t < 20) {
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 150, 150, 255);
|
||||
} else if (t < 25) {
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 150, 255, 150);
|
||||
} else if (t < 30) {
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 255, 100);
|
||||
} else if (t < 35) {
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 120, 100);
|
||||
} else {
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 80, 20);
|
||||
}
|
||||
row++;
|
||||
|
||||
// Network devices and IPs
|
||||
if (getifaddrs(&ifaddr) == -1)
|
||||
{
|
||||
perror("getifaddrs");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
|
||||
if (ifa->ifa_addr == NULL)
|
||||
continue;
|
||||
|
||||
family = ifa->ifa_addr->sa_family;
|
||||
if ((strncmp ("eth", ifa->ifa_name, 3 ) == 0) && family == AF_INET) {
|
||||
s = getnameinfo(ifa->ifa_addr,
|
||||
(ifa->ifa_addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) :
|
||||
sizeof(struct sockaddr_in6),
|
||||
host, NI_MAXHOST,
|
||||
NULL, 0, NI_NUMERICHOST);
|
||||
if (s != 0) {
|
||||
printf("getnameinfo() failed: %s\n", gai_strerror(s));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("%-8s <%s>\n", ifa->ifa_name, host);
|
||||
snprintf ( text_buffer, sizeof(text_buffer), "%s:", ifa->ifa_name);
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 200, 200, 255);
|
||||
row++;
|
||||
snprintf ( text_buffer, sizeof(text_buffer), " %s", host );
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 200, 255, 200);
|
||||
row++;
|
||||
}
|
||||
if ((strncmp ("wlan", ifa->ifa_name, 4 ) == 0) && family == AF_INET) {
|
||||
s = getnameinfo(ifa->ifa_addr,
|
||||
(ifa->ifa_addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) :
|
||||
sizeof(struct sockaddr_in6),
|
||||
host, NI_MAXHOST,
|
||||
NULL, 0, NI_NUMERICHOST);
|
||||
if (s != 0) {
|
||||
printf("getnameinfo() failed: %s\n", gai_strerror(s));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("%-8s <%s>\n", ifa->ifa_name, host);
|
||||
snprintf ( text_buffer, sizeof(text_buffer), "%s:",ifa->ifa_name);
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 255, 200, 200);
|
||||
row++;
|
||||
snprintf ( text_buffer, sizeof(text_buffer), " %s", host );
|
||||
lcd_drawText(lcd, 0, row * rowy, text_buffer, 200, 255, 200);
|
||||
row++;
|
||||
}
|
||||
}
|
||||
freeifaddrs(ifaddr);
|
||||
row++;
|
||||
|
||||
lcd_redrawBuffer(lcd);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
demo_deinit(lcd);
|
||||
}
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
#include "base.h"
|
||||
#include "spilcd_gfx.h"
|
||||
#include "spilcd_font.h"
|
||||
#include "img_green_screen.h"
|
||||
|
||||
#define ABS(x) ((x) < 0 ? -(x) : (x))
|
||||
#define GLOW (((i + row) % 6)*8)
|
||||
|
||||
void draw_background(lcd_t* lcd, uint8 lightness)
|
||||
{
|
||||
lcd_setWindow(lcd, 0, 0, lcd->width-1, lcd->height-1);
|
||||
char* img = header_data;
|
||||
for (int i = 0; i < lcd->width * lcd->height; i++) {
|
||||
uint8 pixel[3];
|
||||
HEADER_PIXEL(img, pixel)
|
||||
lcd_pushPixel(lcd, pixel[0] * (int)lightness / 255, pixel[1] * (int)lightness / 255, pixel[2] * (int)lightness / 255);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
lcd_t* lcd = demo_init();
|
||||
|
||||
for (int p = 0; p < 20; p++) {
|
||||
draw_background(lcd, p * 200 / 20);
|
||||
lcd_redrawBuffer(lcd);
|
||||
}
|
||||
|
||||
int rowy = 9;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
draw_background(lcd, 200 - ABS(4 - (i%8)) * 6);
|
||||
int shift_x = ABS(4 - (i%8)) / 4;
|
||||
int shift_y = ABS(5 - (i%10)) / 4;
|
||||
int row = 2;
|
||||
lcd_drawText(lcd, 12 + shift_x, row*rowy + shift_y, "SPI Term v1.8", 0, 220-GLOW, 0);
|
||||
row++;
|
||||
row++;
|
||||
lcd_drawText(lcd, 8 + shift_x, row*rowy + shift_y, "#/> dir", 0, 220-GLOW, 0);
|
||||
row++;
|
||||
|
||||
lcd_drawText(lcd, 8 + shift_x, row*rowy + shift_y, " a.out", 0, 220-GLOW, 0);
|
||||
row++;
|
||||
lcd_drawText(lcd, 8 + shift_x, row*rowy + shift_y, " db.txt", 0, 220-GLOW, 0);
|
||||
row++;
|
||||
lcd_drawText(lcd, 8 + shift_x, row*rowy + shift_y, "#/>", 0, 220-GLOW, 0);
|
||||
lcd_redrawBuffer(lcd);
|
||||
usleep(1000);
|
||||
}
|
||||
|
||||
demo_deinit(lcd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user