= Standard 825 Library with Application class (lib825) = [[TOC(heading=Table of Contents, Docs/Prog/Manual/*)]] The standard library (lib825) is a collection of Cardinal utilities that simplifies the process of writing code for the [[wiki:WikiStart Cardinal 825]]. The library is distributed free of charge and available here to download. == Utility Functions == Some simple routines may be used in the apps to convert between the hexadecimal strings and the various structures. {{{ #!Lineno #!c unsigned char inline hex_char_val(char ch) { if(ch >= '0' && ch <= '9') return ch - '0'; else return ch - 'A' + 10; } void inline convert_hex_to_binary(char *hex, unsigned char *out) { int n = 0, i = 0; while(hex[n] != 0) { out[i++] = (hex_char_val(hex[n]) * 16) + hex_char_val(hex[n + 1]); if(hex[n + 1] == '\0') { printf("Invalid hex string\r\n"); break; } n += 2; } } static char hexchar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; void binary_to_hex_str(unsigned char *in, unsigned char *out, int len) { int n; for(n = 0; n < len; n++) { out[n * 2] = hexchar[((0xF0 & in[n]) >> 4)]; out[n * 2 + 1] = hexchar[(0x0F & in[n])]; } out[n * 2] = '\0'; } }}} ''These routines are packaged in [doxygen:hexutil_8cpp-source.html hexutil.cpp]'' For example, an app may access time/date from the mainboard with the following: {{{ #!c char* mnbd_dev "/dev/mnbd"; int g_fileMnBd = 0; int OpenMnBd(void) { /* O_NONBLOCK allows open even with no carrier detect */ g_fileMnBd = open(mnbd_dev, O_RDWR | O_NOCTTY | O_NONBLOCK); if(g_fileMnBd == -1) { printf("Error opening %s\n", mnbd_dev); return -1; } return 0; } void CloseMnBd(0) { close(g_fileMnBd); return; } int z; char readbuf![50]; char rcvbuf![50]; int rcvbufpos = 0; int nread; OpenMnBd(); write(g_fileMnBd, "S\r", 2); // Request date/time from mainboard // In main loop of application: for(;;) { nread = read(g_fileMnBd, &readbuf, sizeof(readbuf) ); if(nread > 0) { for(z = 0; z < nread; z++) { if(readbuf[z] == '\r') { rcvbuf[rcvbufpos] = '\0'; // When time/date is received from mainboard set the Linux clock if(rcvbuf![0] == 'S') { struct mnbd_datetime_struct dt; convert_hex_to_binary(rcvbuf + 2, (unsigned char *)&dt); printf("Date/time from mainboard %d/%d/%d %d:%d:%d\r\n", dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second); // Set regular C tm struct to appropriate time values struct tm time; time.tm_year = dt.year - 1900; time.tm_mon = dt.month - 1; time.tm_mday = dt.day; time.tm_hour = dt.hour; time.tm_min = dt.minute; time.tm_sec = dt.second; time_t tt = mktime(&time); stime(&tt); } rcvbufpos = 0; } else if(readbuf[z] >= 0x20 && readbuf[z] < 0x7F) { rcvbuf[rcvbufpos++] = readbuf[z]; } } }}} [[Top]]