Advertisement
Guest User

Monitor Switch

a guest
Jan 19th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <sys/ioctl.h>
  7.  
  8. #include <linux/types.h>
  9. #include <linux/i2c-dev.h>
  10.  
  11. struct i2c_msg {
  12.     __u16 addr;
  13.     __u16 flags;       
  14.     __u16 len;
  15.     __u8 *buf;
  16. };
  17.  
  18. int i, fd;
  19.  
  20. void send(const char* data, int len){  
  21.     struct i2c_rdwr_ioctl_data msg_rdwr;
  22.     struct i2c_msg             i2cmsg;
  23.    
  24.     msg_rdwr.msgs = &i2cmsg;
  25.     msg_rdwr.nmsgs = 1;
  26.  
  27.     i2cmsg.addr  = 0x37;
  28.     i2cmsg.flags = 0;
  29.     i2cmsg.len   = len;
  30.     i2cmsg.buf   = (__u8*)data;
  31.    
  32.     if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0 )
  33.     {
  34.         perror("ioctl()");
  35.         fprintf(stderr,"ioctl returned %d\n",i);
  36.         exit(-1);
  37.     }
  38. }
  39.  
  40. void init(){
  41.     fd = open("/dev/i2c-6",O_RDWR);
  42.     if(fd < 0){
  43.         printf("Failed to open bus 6\n");
  44.         exit(-1);
  45.     }
  46.     send("\x00",1);
  47. }
  48.  
  49. void print_usage(char* name){
  50.     printf("USAGE: %s h1 - switches to HDMI1 input\n", name);
  51.     printf("       %s h2 - switches to HDMI2 input\n", name);
  52. }
  53.  
  54. int main(int argc, char** argv){
  55.    
  56.     if(argc < 2){
  57.         print_usage(argv[0]);
  58.         return -2;
  59.     }else{
  60.         char c = argv[1][0];
  61.         if(c != 'h1' && c != 'h2'){
  62.             print_usage(argv[0]);
  63.             return -2;
  64.         }else{
  65.             init();
  66.             if(c == 'h1'){
  67.                 printf("Switching to HDMI1\n");
  68.                 send("\x51\x84\x03\x60\x00\x11\xc9\x00", 7);
  69.             }else if(c == 'h2'){
  70.                 printf("Switching to HDMI2\n");
  71.                 send("\x51\x84\x03\x60\x00\x10\xc8\x00", 7);
  72.             }
  73.             close(fd);
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement