Slide 124
Slide 124 text
ϦΞϧλΠϜΫϩοΫ
boost::posix_time::ptime now(
boost::gregorian::date(2000+unpack( dump[ 7 ] ), unpack( dump[ 6 ] & 0x1F ), unpack( dump[ 5 ] & 0x3F )),
boost::posix_time::time_duration(unpack( dump[ 3 ] & 0x3F ), unpack( dump[ 2 ] & 0x7F ), unpack( dump[ 1 ] & 0x7F ))
);
return now;
}
void set_time( int fd, const boost::posix_time::ptime &time ) {
std::vector< uint8_t > dump{{0,0,
pack( time.time_of_day().seconds() ), pack( time.time_of_day().minutes() ),
pack( time.time_of_day().hours() ), pack( time.date().week_number() ),
pack( time.date().day() ), pack( time.date().month() ),
pack( time.date().year() - 2000 )
}};
if( write( fd, dump.data(), dump.size() ) < 0 ) throw communication_failed();
}
int main( int argc, char* argv[] ) {
boost::program_options::options_description options("Options");
options.add_options()( "help,h", "show this message" )
( "device,d", boost::program_options::value< std::string >(), "I2C device" )
( "set,s", boost::program_options::value< std::string >(), "set current time" )
( "get,g", "get current time" );
boost::program_options::variables_map variables;
boost::program_options::store( boost::program_options::parse_command_line( argc, argv, options ), variables );
boost::program_options::notify( variables );
if( variables.count( "help" ) || !( variables.count( "device" ) ) ||
!( variables.count( "set" ) || variables.count( "get" ) ) ||
( variables.count( "set" ) && variables.count( "get" ) ) ) std::cout << options << std::endl;
else {
const int fd = open( variables[ "device" ].as< std::string >().c_str(), O_RDWR );
if( fd < 0 ) exit( 1 );
ioctl( fd, I2C_SLAVE, 0x68 );
try {
if( variables.count( "set" ) )
set_time( fd, boost::posix_time::time_from_string( variables[ "set" ].as< std::string >() ) );
else if( variables.count( "get" ) )
std::cout << get_time( fd ) << std::endl;
} catch( const communication_failed& ) { exit( 1 ); }
}
}
const int fd = open(
variables["device"].as().c_str(),
O_RDWR
);
ioctl( fd, I2C_SLAVE, 0x68 );
read( fd, &data, 1 );
write( fd, dump.data(), dump.size() );