static void crt_rpi_switch(int width, int height, float hz) { char buffer[1024]; VCHI_INSTANCE_T vchi_instance; VCHI_CONNECTION_T *vchi_connection = NULL; static char output[250] = {0}; static char output1[250] = {0}; static char output2[250] = {0}; static char set_hdmi[250] = {0}; static char set_hdmi_timing[250] = {0}; int i = 0; int hfp = 0; int hsp = 0; int hbp = 0; int vfp = 0; int vsp = 0; int vbp = 0; int hmax = 0; int vmax = 0; int pdefault = 8; int pwidth = 0; float roundw = 0.0f; float roundh = 0.0f; float pixel_clock = 0; int ip_flag = 0; /* set core refresh from hz */ video_monitor_set_refresh_rate(hz); /* following code is the mode line generator */ pwidth = width; if (height < 400 && width > 400) pwidth = width / 2; roundw = roundf((float)pwidth / (float)height * 100) / 100; if (height > width) roundw = roundf((float)height / (float)width * 100) / 100; if (roundw > 1.35) roundw = 1.25; if (roundw < 1.20) roundw = 1.34; hfp = width * 0.065; hsp = width * 0.1433-hfp+(crt_center_adjust*4); hbp = width * 0.3-hsp-hfp; if (height < 241) vmax = 261; if (height < 241 && hz > 56 && hz < 58) vmax = 280; if (height < 241 && hz < 55) vmax = 313; if (height > 250 && height < 260 && hz > 54) vmax = 296; if (height > 250 && height < 260 && hz > 52 && hz < 54) vmax = 285; if (height > 250 && height < 260 && hz < 52) vmax = 313; if (height > 260 && height < 300) vmax = 318; if (height > 400 && hz > 56) vmax = 533; if (height > 520 && hz < 57) vmax = 580; if (height > 300 && hz < 56) vmax = 615; if (height > 500 && hz < 56) vmax = 624; if (height > 300) pdefault = pdefault * 2; vfp = (height + ((vmax - height) / 2) - pdefault) - height; if (height < 300) vsp = vfp + 3; /* needs to be 3 for progressive */ if (height > 300) vsp = vfp + 6; /* needs to be 6 for interlaced */ vsp = 3; vbp = (vmax-height)-vsp-vfp; hmax = width+hfp+hsp+hbp; if (height < 300) { pixel_clock = (hmax * vmax * hz) ; ip_flag = 0; } if (height > 300) { pixel_clock = (hmax * vmax * (hz/2)) /2 ; ip_flag = 1; } /* above code is the modeline generator */ snprintf(set_hdmi_timing, sizeof(set_hdmi_timing), "hdmi_timings %d 1 %d %d %d %d 1 %d %d %d 0 0 0 %f %d %f 1 ", width, hfp, hsp, hbp, height, vfp,vsp, vbp, hz, ip_flag, pixel_clock); vcos_init(); vchi_initialise(&vchi_instance); vchi_connect(NULL, 0, vchi_instance); vc_vchi_gencmd_init(vchi_instance, &vchi_connection, 1); vc_gencmd(buffer, sizeof(buffer), set_hdmi_timing); vc_gencmd_stop(); vchi_disconnect(vchi_instance); snprintf(output1, sizeof(output1), "tvservice -e \"DMT 87\" > /dev/null"); system(output1); snprintf(output2, sizeof(output1), "fbset -g %d %d %d %d 24 > /dev/null", width, height, width, height); system(output2); }
int main( int argc, char **argv ) { int instNum = 0; VCHI_INSTANCE_T vchi_instance; VCHI_CONNECTION_T *vchi_connection = NULL; if ( argc > 1 ) { if (( strcmp( argv[1], "0" ) == 0 ) || ( strcmp( argv[1], "1" ) == 0 )) { instNum = atoi( argv[1] ); argv++; argc--; } } vcos_init(); if ( vchi_initialise( &vchi_instance ) != 0) { printf( "VCHI initialization failed\n" ); return -1; } //create a vchi connection if ( vchi_connect( NULL, 0, vchi_instance ) != 0) { printf( "VCHI connection failed\n" ); return -1; } vc_vchi_gencmd_init(vchi_instance, &vchi_connection, 1 ); if (argc > 1) { int i = 1; char buffer[ 1024 ]; size_t buffer_offset = 0; clock_t before=0, after=0; double time_diff; uint32_t show_time = 0; int ret; //reset the string buffer[0] = '\0'; //first, strip out a potential leading -t if( strcmp( argv[1], "-t" ) == 0 ) { show_time = 1; i++; } for (; i <= argc-1; i++) { buffer_offset = vcos_safe_strcpy( buffer, argv[i], sizeof(buffer), buffer_offset ); buffer_offset = vcos_safe_strcpy( buffer, " ", sizeof(buffer), buffer_offset ); } if( show_time ) before = clock(); //send the gencmd for the argument if (( ret = vc_gencmd_send( "%s", buffer )) != 0 ) { printf( "vc_gencmd_send returned %d\n", ret ); } //get + print out the response! if (( ret = vc_gencmd_read_response( buffer, sizeof( buffer ) )) != 0 ) { printf( "vc_gencmd_read_response returned %d\n", ret ); } if( show_time ) after = clock(); if( show_time ) { time_diff = ((double) (after - before)) / CLOCKS_PER_SEC; printf( "Time took %f seconds (%f msecs) (%f usecs)\n", time_diff, time_diff * 1000, time_diff * 1000000 ); } if ( buffer[0] != '\0' ) { if ( buffer[ strlen( buffer) - 1] == '\n' ) { fputs( buffer, stdout ); } else { printf("%s\n", buffer ); } } } vc_gencmd_stop(); //close the vchi connection if ( vchi_disconnect( vchi_instance ) != 0) { printf( "VCHI disconnect failed\n" ); return -1; } return 0; }