diff --git a/mirror2.c b/mirror2.c new file mode 100644 index 0000000000000000000000000000000000000000..9cd5ce30659773f0c2db76b3216399131e4e0161 --- /dev/null +++ b/mirror2.c @@ -0,0 +1,606 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> +#include <time.h> +#include <stdbool.h> +#include <unistd.h> +#include <wchar.h> +#include <locale.h> +#include <math.h> +#include <termios.h> +#include <pthread.h> + +#include <wiringPi.h> +#include <wiringPiSPI.h> +#include <mcp3004.h> + +#include <linux/input.h> + +#include <bcm_host.h> + +#include "VG/openvg.h" +#include "VG/vgu.h" +#include "shapes.h" +#include "digital_signage.h" + +#define csPin 0 +#define sclkPin 1 +#define mosiPin 2 +#define misoPin 3 + + +#define CUR_SIZ 16 + +#define DIAL 1 +#define VSLIDER 2 +#define HSLIDER 3 +#define CHECKBOX 4 +#define BUTTON 5 +#define AREA 6 +#define LAREA 7 + + +#define GREEN 0 +#define ORANGE 1 +#define GREY 2 +#define RED 3 +#define NONE 4 + +//äöüÄÖÜß +char umlaut[]={0xe4,0xf6,0xfc,0xc4,0xd6,0xdc,0xdf,0}; + +int count=12; +int seconds=3; + +int lcount=0; + +int commentwidth=500; + +typedef struct +{ +int x; +int y; +} point_t; + +point_t mp; +point_t handle; + + +typedef struct +{ +int x; +int y; +int w; +int h; +//int isactive; +//bool is_on; +} rectangle_t; + + +// visual controls structure: +typedef struct +{ + char name[30]; + int ctrltype; + rectangle_t coords; + int id; + bool isactive; + int color; + float value; + int r; + bool visible; + VGPath path; + int groupid; + +} control_t; + +// Mouse state structure +typedef struct { + int fd; + struct input_event ev; + VGfloat x, y; + int left, middle, right; + int max_x, max_y; +} mouse_t; + + +// BEGIN data structs +struct NEWS_TICKER nt = { + .pos_x = 220, + .pos_y = 20, + .screen_width = 400, + .font = &SansTypeface, + .font_size = 20, + .scroll_speed = 3, // 3 is the best balance between smoothness and readability + .fade_speed = 0.05, + .display_time = 4.0, + + .cur_pos_x = 0, + .cur_msg_index = 0, + .fades_in = true, + .fades_out = false, + .scrolls = true, + .current_alpha = 0.5, + .msg_count = 6, + .content = { + L"Der Wahnsinn ist eine Reise zur Hoelle.", + L"Das Gehirn erkrankt und schwankt in immer neue Dimensionen, da, wo die boesen Maechte wohnen" , + L"There is class warfare, all right, but it is my class, the rich class, that is making war, and we are winning.", + L"Krieg ist Frieden", + L"Freiheit ist Sklaverei", + L"Unwissenheit ist Staerke" + + } +}; + + + +//dispmanx for additional layers, eg backdrop + DISPMANX_DISPLAY_HANDLE_T display; + DISPMANX_UPDATE_HANDLE_T update; + DISPMANX_RESOURCE_HANDLE_T resource; + DISPMANX_ELEMENT_HANDLE_T element; + + VC_RECT_T dst_rect, src_rect; + +VGImage heart; + +point_t lastmouse; + +mouse_t mouse; // global mouse state +int left_count = 0; +int quitState = 0; + +bool mousechanged=false; + +bool isresizerx=false; +bool isresizery=false; + +int bcount=0; +control_t controls[100]; + +int currentactive=-1; + +int currentover=-1; + +bool doexit=false; +bool global_hide=false; +bool blank_bg=true; + +int bg_layer=3; + +char ltype[50]; +char lcol[10]; +char lvis[10]; + +int adcval[8]; +int adcctrlid[8]; + +int adc_hist[8][10]; +int adc_hist_index[8]; + +int r=1; + +static void blank_background(int layer,bool enable) +{ +// int ret; + if (enable) + { + update = vc_dispmanx_update_start(0); + + element = vc_dispmanx_element_add(update, display, layer, &dst_rect, resource, &src_rect, + DISPMANX_PROTECTION_NONE, NULL, NULL, DISPMANX_NO_ROTATE); //DISPMANX_STEREOSCOPIC_MONO ); + /*ret =*/ vc_dispmanx_update_submit_sync( update ); + } + else + { + update = vc_dispmanx_update_start(0); + /*ret=*/vc_dispmanx_element_remove( update, element ); + /*ret=*/vc_dispmanx_update_submit_sync( update ); + } + +} + + + +// BEGIN non-blocking and utility functions +void changemode(int dir) { + static struct termios oldt, newt; + + if (dir == 1) { + tcgetattr( STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~( ICANON | ECHO ); + tcsetattr( STDIN_FILENO, TCSANOW, &newt); + } else { + tcsetattr( STDIN_FILENO, TCSANOW, &oldt); + } +} + + + + +void *updaterThread(void *arg) { + +//printf("key thread running\n"); +while(1) { + sleep(1); + count+=1; + r=Random(1,lcount-1); + +} + + +return 0; +} + + + + + + +double clamp(double d, double min, double max) { + const double t = d < min ? min : d; + return t > max ? max : t; +} +// END non-blocking and utility functions + +// BEGIN common functions +double Fade(bool fades_in, bool fades_out, double current_alpha, double fade_speed) { + if (fades_in && current_alpha <= 1) { + return clamp(current_alpha + fade_speed, 0.0, 1.0); + } else if (fades_out && current_alpha >= 0) { + return clamp(current_alpha - fade_speed, 0.0, 1.0); + } + + return 1.0; +} + +// END common functions + +// BEGIN newsticker specific functions +VGfloat MeasureCurText(struct NEWS_TICKER *nt) { + return WTextWidth(nt->content[nt->cur_msg_index], *nt->font, nt->font_size); +} + +//thread for updating animated graphics, currently the ticker +int updaterinit() { + pthread_t updateThread; + return pthread_create(&updateThread, NULL, &updaterThread, NULL); +} + + +void setctrlcolor(int c, bool over, bool on) +{ + +if (c==NONE) +{Stroke(255, 255, 255, .5); +Fill(0, 0, 0, 0); +} + + +else if (c==GREEN) +{Stroke(0, 255, 0, 1); + if (!over) + { + if (!on) Fill(0, 255, 0, 0.1); + else Fill(0, 255, 0, 0.4); + } + else Fill(0, 255, 0, 0.4); +} +else if (c==ORANGE) + {Stroke(255, 128, 0, 1); + if (!over) + { if (!on) Fill(255, 128,0, 0.1); + else Fill(255, 128,0, 0.4); + } + else Fill(255, 128,0, 0.6); +} + +else if (c==GREY) + {Stroke(255, 255, 255, 1); + if (!over) + { if (!on) Fill(192, 192,192, 0.1); + else Fill(192, 192,192, 0.4); + } + else Fill(192, 192,192, 0.6); +} +else if (c==RED) + {Stroke(255, 0, 0, 1); + if (!over) + { if (!on) Fill(255, 0,0, 0.1); + else Fill(255,0,0, 0.4); + } + else Fill(255,0,0, 0.6); +} + +} + + + + + + +/////////////////////////////////////////////////// +//stolen from omxplayer.cpp: + +//create additional dispmanx context +//and set up a resource 1 by 1 + +static void create_bglayer() +{ + uint32_t vc_image_ptr; + VC_IMAGE_TYPE_T type = VC_IMAGE_RGB565; + uint16_t image = 0x0000; // black + + + display = vc_dispmanx_display_open(0); //m_config_video.display + + // we create a 1x1 black pixel image that is added to display just behind video + + resource = vc_dispmanx_resource_create( type, 1 /*width*/, 1 /*height*/, &vc_image_ptr ); + + vc_dispmanx_rect_set( &dst_rect, 0, 0, 1, 1); + + vc_dispmanx_resource_write_data( resource, type, sizeof(image), &image, &dst_rect ); + //assert(ret == 0); + + vc_dispmanx_rect_set( &src_rect, 0, 0, 1<<16, 1<<16); + vc_dispmanx_rect_set( &dst_rect, 0, 0, 0, 0); + +} + +int Random(int lower, int upper) +{ + int num = (rand() % + (upper - lower + 1)) + lower; +return num; +} + + + +/////////////////////////////////////////////////// + +int main(int argc, char **argv) { + time_t rawtime; + struct tm *timeinfo; + char time_formatted[30]; + + char gefaellt[30]; + char secondsago[30]; + char number[4]; + char times[4]; + + char msg[30]; + + + char tempinfo[1035]; + + int width = 1024; + int height = 600; + + int yval1=0; + int yval2=0; + + float angle1=0.0f; + + int min=0; + int lastmin=0; + + int xoff=-30; + int yoff=70; + + handle.x=0; + handle.y=0; + + char line[255]; + + printf("hello master\n"); + +///init openvg + init(&width, &height); + + +// mcp3004Setup (100, 0); //MCP3004/MCP3008 + + create_bglayer(); + blank_background(bg_layer,true); + + updaterinit(); + + heart=createImageFromJpeg("heart.jpg"); + +FILE * fp; +char * rline=NULL; +size_t len=0; +ssize_t read; +int stat=0; + +char textstore[60][110]; + +fp = fopen("texts.txt", "r"); + if (fp == NULL) + + exit(EXIT_FAILURE); + + while ((read = getline(&rline, &len, fp)) != -1) { + printf("Retrieved line of length %zu:\n", read); + lcount+=1; + if (stat<read){stat=read;} + printf("%s", rline); + size_t length = strlen(rline); + size_t i=0; + char t[3]=""; + char modline[150]=""; + + int havespecial=0; + for (i=0; i < length; i++) { + if (havespecial==1) {printf("special: %i\n", rline[i]);havespecial=0;} + if (rline[i]==195) {havespecial=1; continue;} + + //printf("%c\n", rline[i]); /* Print each character of the string. */ + if (rline[i]==164) sprintf(t,"%c",0xe4); + else if (rline[i]==182) sprintf(t,"%c",0xf6); + else if (rline[i]==188) sprintf(t,"%c",0xfc); + else if (rline[i]==132) sprintf(t,"%c",0xc4); + else if (rline[i]==150) sprintf(t,"%c",0xd6); + else if (rline[i]==156) sprintf(t,"%c",0xdc); + else if (rline[i]==159) sprintf(t,"%c",0xdf); + + else + sprintf(t,"%c",rline[i]); + strcat(modline,t); + } + + + strcpy(textstore[lcount],modline); + + } + + fclose(fp); +/* +printf("maxlength: %i, count: %i\n",stat,lcount); +size_t length = strlen(rline); +size_t i = 0; +printf("rline len: %i\n",length); +for (; i < length; i++) { +if (rline[i]==195) continue; + printf("%i\n", rline[i]); +} +*/ + + + + if (rline) + free(rline); + + + +///go! + Start(width, height); + +// changemode(1); + + +//blank_background(12,true); + +//main loop: + + while (!doexit) { + // doexit=kbhit(); + Clear(width, height); + +//values for testing + yval1+=2; + if (yval1>360) yval1=0; + + yval2+=2; + if (yval2>580) yval2=0; + + angle1-=0.01f; + if (angle1<-1.0f) angle1=0.0f; + + //checkadc(); + + float val=0.0; + float outval=0.0; + + //time display + + time(&rawtime); + timeinfo = localtime(&rawtime); + min=timeinfo->tm_min; + + +//strings for graphics: + + strftime(time_formatted, 30, "%d.%m.%Y @ %H:%M:%S", timeinfo); + //{0xe4,0xf6,0xfc,0xc4,0xd6,0xdc,0xdf,0} + char gef[]={'g','e','f',0xe4,'l','l','t'}; + + strcpy(gefaellt,gef); + strcpy(times,"Mal"); + + sprintf(secondsago,"vor %ld Sekunden", seconds); + sprintf(number,"%ld", count); + + float tw=TextWidth(number, SansTypeface, 140); + + if (!global_hide) + { + vgSetPixels(0, 150, heart, 0, 0, 150, 150); + + Fill(255, 255, 255, 1); + Text(100, 85, gefaellt, SansTypeface, 14); + Text(20, 45, secondsago, SansTypeface, 14); + Text(200, 45, number, SansTypeface, 140); + + Text(230+tw, 45, times, SansTypeface, 14); + + Text(200, 465, time_formatted, SansTypeface, 14); + + //printf("r: %i\n",r); + printf("from textstore: %s\n",textstore[r]); + + + char tmp[120]; + strcpy(tmp,textstore[r]); + + float cw=TextWidth(tmp, SansTypeface, 18); + + while (cw>commentwidth) + { + + char * pch; + pch=strrchr(tmp,' '); + printf ("Last space found at %d \n",pch-tmp+1); + + int loc=pch-tmp; + + char* substr = malloc(loc+2); + strncpy(substr, tmp, loc); + substr[loc] = '\0'; + + printf("actual sub:%s\n",substr); + cw=TextWidth(substr, SansTypeface, 18); + printf("%f\n",cw); + + strcpy(tmp,substr); + } + +printf("DONE: %f\n",cw); + + for (int i=0;i<5;i++) + { + + Fill(255,128,0,.5); + Roundrect(1000, 1080-90-i*80,commentwidth+200,70,10,10); + Fill(0,0,0,1); + //if (cw<commentwidth) Text(1000+20, 1080-90-i*80+20, textstore[r], SansTypeface, 18); + //else Text(1000+20, 1080-90-i*80+20, "XXXXXXX", SansTypeface, 18); + + Text(1000+20, 1080-90-i*80+20, tmp, SansTypeface, 18); + } + + } + + + End(width, height); + + } +//END of MAIN loop + +//printf("gui exiting\n"); + + changemode(0); + finish(); + + int c=0; + for (c=0;c<bcount;c++) + { + vgDestroyPath(controls[c].path); + } + return 0; +}