This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
user:mtaft4:projects:hexclock [2010/12/17 00:20] – mtaft4 | user:mtaft4:projects:hexclock [2010/12/17 00:21] (current) – mtaft4 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | =====Hexadecimal Clock===== | ||
+ | - Start by using the date command with the %H, %M, %S options to get the current time in seconds. | ||
+ | - Next, take the current time and divide it by the total number of seconds in a day; 86,400 and representing it as a hexadecimal fraction. | ||
+ | - Taking the fraction by the first 4 digits shows the hours minutes and seconds in hexadecimal, | ||
+ | ====Stage 1 - Bash Code==== | ||
+ | <code bash># | ||
+ | # hexclock - convert time to a hexadecimal format | ||
+ | hour=`date +%H` # Get the current Hour (24 hour format) | ||
+ | minute=`date +%M` # Get the current Minute | ||
+ | second=`date +%S` # Get the current Second | ||
+ | totalsec=`echo " | ||
+ | htime=" | ||
+ | htime=`echo $htime | cut -d' | ||
+ | hexhour=`echo $htime | cut --bytes=1` | ||
+ | hexmin=`echo $htime | cut --bytes=2-3` | ||
+ | hexsec=`echo $htime | cut --bytes=4` | ||
+ | echo -n "The current time in hexadecimal is: " | ||
+ | echo $hexhour" | ||
+ | exit</ | ||
+ | ====Stage 2 - C++ Code==== | ||
+ | <code C++># | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | using namespace std; // | ||
+ | int hours, minutes, seconds, hhours, hmin1, hmin2, hseconds, htime; // Variable and Namespace setup | ||
+ | int hminutes; | ||
+ | double totalsec, hexsec; | ||
+ | time_t epoch_time; | ||
+ | struct tm *tm_p; | ||
+ | |||
+ | epoch_time = time( NULL ); // | ||
+ | tm_p = localtime( & | ||
+ | printf(" | ||
+ | tm_p-> | ||
+ | |||
+ | hours = tm_p-> | ||
+ | minutes = tm_p-> | ||
+ | seconds = tm_p-> | ||
+ | totalsec = ( ( ( hours * 60 ) + minutes ) * 60 ) + seconds; // Conversion of time to total seconds | ||
+ | |||
+ | |||
+ | hexsec = totalsec / 86400.00 * 65536; | ||
+ | htime = static_cast< | ||
+ | hhours = htime / 4096; // | ||
+ | htime = htime - (hhours * 4096); | ||
+ | hminutes = htime / 16; // | ||
+ | hmin1 = hminutes / 16; // | ||
+ | hmin2 = hminutes - (hmin1 * 16); // | ||
+ | hseconds = htime - (hminutes * 16); // | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | return 0; | ||
+ | }</ | ||
+ | As you can see the C++ code is a massive jump in size from the previous bash script. And yet it seems to do precisely the same thing. This is because it took much more doing in order to get and format the time the way I wanted in order to even come remotely close to being able to convert it. Then I had to convert each piece so that I could actually format the text the right way in order to just make a command line clock. | ||
+ | |||
+ | Now that I have at least a code base to start from I can work on throwing it into SDL and getting it to either actually display the time in a window or a widget or something or get it to determine the position of the hands of a true clock face and do it that way. The math at least for it isn't that hard, divide the 360 degree circle into 16 and 256 parts for hours and minutes respectively and have it move that percentage of the degrees each time a minute or hour changes. With how close hex seconds are to regular seconds (about 3 hex seconds to 4 regular seconds is the ratio) and the fact that there are only 16 hex seconds in a minute it would be impractical to include seconds in a clock face example but digital clock would easily be able to include seconds. | ||
+ | |||
+ | I ended up choosing C++ because that from all my fiddling with SDL was what I needed to use in order to make it work. I don't know if that is truly the case or if it's merely the fact that I never saw any strictly C SDL tutorials. Either way I did make the decision to use the cstdio library along with the standard iostream library just in case I ended up needing to use either even though I'm more comfortable with C as far is output goes which is the only thing I really foresee needing any sort of I/O for as almost all of the interaction comes between the program and the system in trying to determine the time. The only I/O is at the end when the time is reported to the user, but planning for eventualities never hurt anyone. |