git_cberry/src/bildercycle.c

148 lines
3.9 KiB
C

/* ------------------------------------------------------------------
* (c) schick Informatik 2017
* Hilfsprogramm, um Bilder aus einem Verzeichnis auf dem TFT CBerry
* anzuzeigen.
* ------------------------------------------------------------------ */
#include <bcm2835.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include "tft.h"
#include "ST7789.h"
#include "bmp.h"
#include "examples.h"
#ifdef CM_262K
uint32_t** imageList;
#else
uint16_t** imageList;
#endif
#define INTERVAL 15
int MAXCNT = 256;
int numImages = 0;
int main( int argc, char **argv )
{
if(argc < 2) {
printf("Usage: bildercycle <path>\n");
return 1;
}
char *sourcePath = argv[1];
#ifdef CM_262K
imageList = calloc(MAXCNT, sizeof(uint32_t*));
#else
imageList = calloc(MAXCNT, sizeof(uint16_t*));
#endif
if(ReadFromDirectory(sourcePath)) {
fprintf(stderr, "Error reading images from source path %s\n", sourcePath);
return 1;
}
printf("%d images read\n", numImages);
if (!bcm2835_init()) {
fprintf(stderr, "Error initializing TFT (bcm2835_init)\n");
return 1;
}
TFT_init_board();
TFT_hard_reset();
STcontroller_init();
TFT_SetBacklightPWMValue( 20 );
// depict a BMP file
// ---------------------------------------------
//example_DepictBMP( &my_filename[0] );
int i;
for(i=0;i<numImages;i++)
{
STcontroller_Write_Picture ( imageList[i], PICTURE_PIXELS );
//printf("[%d]", i);
//fflush(stdout);
sleep(INTERVAL);
}
bcm2835_close();
return 0;
}
int ReadFromDirectory(char *in_dir)
{
DIR* FD;
struct dirent* in_file;
FILE *entry_file;
char buffer[BUFSIZ];
int32_t result = 1;
fprintf(stderr, "reading %s\n", in_dir);
/* Scanning the in directory */
if (NULL == (FD = opendir (in_dir)))
{
fprintf(stderr, "Error : Failed to open input directory - %s\n", strerror(errno));
return 1;
}
while ((in_file = readdir(FD)))
{
/* On linux/Unix we don't want current and parent directories
* On windows machine too, thanks Greg Hewgill
*/
if (!strcmp (in_file->d_name, ".")) continue;
if (!strcmp (in_file->d_name, "..")) continue;
/* Open directory entry file for common operation */
/* TODO : change permissions to meet your need! */
char *bmpFileName = calloc(strlen(in_file->d_name) + strlen(in_dir) + 2, sizeof(char *));
sprintf(bmpFileName, "%s/%s", in_dir, in_file->d_name);
// fprintf(stderr, "reading %s..\n", bmpFileName);
#ifdef CM_262K
imageList[numImages] = calloc(PICTURE_PIXELS, sizeof(uint32_t));
// Achtung Funktion liest von hinten nach vorn, daher den Bilderzeiger auf das letzte Pixel setzen
result = Read_bmp2memory ( bmpFileName, &imageList[numImages][PICTURE_PIXELS - 1] );
#else
imageList[numImages] = calloc(PICTURE_PIXELS, sizeof(uint16_t));
result = Read_bmp2memory ( bmpFileName, &imageList[numImages][PICTURE_PIXELS - 1] );
#endif
if(result) {
fprintf(stderr,"reading BMP %s failed\n", in_file->d_name);
} else {
result = 0; // at least one! successful
numImages++;
}
/*
entry_file = fopen(in_file->d_name, "r");
if (entry_file == NULL)
{
fprintf(stderr, "Error : Failed to open entry file - %s\n", strerror(errno));
fclose(common_file);
return 1;
}
*/
}
return result;
}