#include <stdio.h>
#include <time.h>               // Wichtig für Clocks per Sec und Sleep!!!!
#include <unistd.h>
#include <pthread.h>    //??? 

#include "ip_connection.h"
#include "bricklet_io4.h"

#define HOST "localhost"
#define PORT 4223
#define UID "9VU"               // Change to your UID
 
IPConnection ipcon; 
IO4 io; 
   

  

sendByte (char c){           
        
        io4_set_configuration(&io, 1<<0, 'o', true);
        usleep(600000);
        
        int i = 0;
                          
        for (i=0; i<8; i++) {
       
                    if (c & (i<<1)) {                           
                    io4_set_configuration(&io, 1<<0, 'o', true);
                    usleep(300000); 
                    }
                                
                    else {                                    
                    io4_set_configuration(&io, 1<<0, 'o', false);
                    usleep(300000);                                                              
                    }
        }
        
        io4_set_configuration(&io, 1<<0, 'o', false);
        usleep(600000); 
        
}  


void send(char *c)
{
        while (*c != 0) {       
                sendByte(*c);
                c++;
        }
}

                   
int main() {
  
    // Don't use device before ipcon is connected

   // Hauptprogramm
   
    // Create IP connection 
    //IPConnection ipcon;                     
    ipcon_create(&ipcon);

    // Create device object
    //IO4 io;
    io4_create(&io, UID, &ipcon); 

    //Connect to brickd
    if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
        fprintf(stderr, "Could not connect\n");
        exit(1);
    } 
             
        send("B");
        

    printf("Press key to exit\n");
    getchar();
    ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
}





