/* save in a bit stream */

uint8_t save(uint32_t value, int8_t length_in_bit, struct fat16_file_struct* fd){

static uint8_t data_block;

static uint8_t current_bit = 7;


// while still bytes to copy

    while ( length_in_bit > 0 )

    {

        // if next input bit is set, set destination bit 7

        if ( value & (uint32_t)0x01 << (length_in_bit-1))

            data_block |= 0x01 << current_bit;

        

length_in_bit--;

        

// byte boundary hit?

        if ( current_bit == 0 ){

            // goto next byte

            current_bit = 7;

if(fat16_write_file(fd, &data_block, 1) != 1){ //retry writing in case of error 

               SPI_Disable();

lcd_print("error writing file!",6,115,0,Rot,Blau);

SPI_Enable();

while(1){

fd = reopen_sdcard();

if(fat16_write_file(fd, &data_block, 1) == 1) //check number of bytes written

break;

}

SPI_Disable();

lcd_print("                    ",6,115,0,Rot,Blau);

SPI_Enable();

           }

data_block = 0x00;

}

else

current_bit--;

    }

return 1;

}




// Exemplarischer Aufruf:


save(descriptor,2,fd);

save(gps_converted.utc_in_sec,bit_time_comp,fd);

save(gps_converted.date,bit_date_comp,fd);

save(gps_converted.lat,bit_lat_comp,fd);

save(gps_converted.lon,bit_lon_comp,fd);

save(gps_converted.altitude,bit_alt_comp,fd);

save(gps_converted.satellites,bit_sat_comp,fd);