2626#include <netinet/ether.h>
2727#include <unistd.h>
2828#include <stdint.h>
29+ #include <time.h>
30+ #include <math.h>
2931
3032#include <types.h>
3133#include <timers.h>
@@ -41,7 +43,8 @@ typedef struct
4143 uint8_t r ;
4244} BGR ;
4345
44- BGR frame [HEIGHT ][WIDTH ];
46+ RGB frame_in [HEIGHT ][WIDTH ];
47+ BGR frame_out [HEIGHT ][WIDTH ];
4548int sockfd ;
4649struct sockaddr_ll socket_address ;
4750
@@ -185,25 +188,66 @@ int gety(int _modno)
185188
186189int set (int _modno , int x , int y , RGB color )
187190{
188- frame [y ][x ].r = color .red ;
189- frame [y ][x ].g = color .green ;
190- frame [y ][x ].b = color .blue ;
191+ frame_in [y ][x ] = color ;
191192 return 0 ;
192193}
193194
194195RGB get (int _modno , int x , int y )
195196{
196- return RGB ( frame [y ][x ]. r , frame [ y ][ x ]. g , frame [ y ][ x ]. b ) ;
197+ return frame_in [y ][x ];
197198}
198199
199200int clear (int _modno )
200201{
201- memset (frame , 0 , HEIGHT * WIDTH * 3 );
202+ memset (frame_in , 0 , HEIGHT * WIDTH * sizeof (RGB ));
203+ for (int y = 0 ; y < HEIGHT ; y ++ )
204+ {
205+ for (int x = 0 ; x < WIDTH ; x ++ )
206+ {
207+ frame_in [y ][x ].alpha = 255 ;
208+ }
209+ }
202210 return 0 ;
203211};
204212
213+ #define FADE_ENABLED 1
205214int render (void )
206215{
216+ time_t tm_now = time (NULL );
217+ struct tm * tm_struct = localtime (& tm_now );
218+ float hour = tm_struct -> tm_hour + tm_struct -> tm_min / 60.0 ;
219+
220+ // analyse
221+ int pixsum = 0 ;
222+ for (int y = 0 ; y < HEIGHT ; y ++ )
223+ {
224+ for (int x = 0 ; x < WIDTH ; x ++ )
225+ {
226+ pixsum += frame_in [y ][x ].red ;
227+ pixsum += frame_in [y ][x ].green ;
228+ pixsum += frame_in [y ][x ].blue ;
229+ }
230+ }
231+ double bright = (double )pixsum / (WIDTH * HEIGHT );
232+ double fade = 127.0 / bright ;
233+ fade = MIN (0.66 , fade );
234+ // TODO: get location from commandline, implement equation of time for sunset and sunrise
235+ if (!FADE_ENABLED || (hour > 5.5 && hour < 21.5 ))
236+ {
237+ fade = 1.0 ;
238+ }
239+
240+ // copy and fade
241+ for (int y = 0 ; y < HEIGHT ; y ++ )
242+ {
243+ for (int x = 0 ; x < WIDTH ; x ++ )
244+ {
245+ frame_out [y ][x ].r = frame_in [y ][x ].red * fade ;
246+ frame_out [y ][x ].g = frame_in [y ][x ].green * fade ;
247+ frame_out [y ][x ].b = frame_in [y ][x ].blue * fade ;
248+ }
249+ }
250+
207251 // Limit frame rate. In this configuration, the matrix is only stable up to ~70fps
208252 static oscore_time last = 0 ;
209253 oscore_time now = udate ();
@@ -224,7 +268,7 @@ int render(void)
224268 for (int i = 0 ; i < HEIGHT ; ++ i )
225269 {
226270 line [14 ] = i ;
227- memcpy (& line [21 ], & frame [i ][0 ], WIDTH * 3 );
271+ memcpy (& line [21 ], & frame_out [i ][0 ], WIDTH * 3 );
228272 /* Send line packet */
229273 if (sendto (sockfd , line , sizeof (line )/sizeof (line [0 ]), 0 , (struct sockaddr * )& socket_address , sizeof (struct sockaddr_ll )) < 0 )
230274 {
0 commit comments