Changes between Initial Version and Version 1 of Docs/Prog/Manual/ApplicationLibraries/lib825


Ignore:
Timestamp:
11/30/09 14:29:44 (15 years ago)
Author:
Don Wilson
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Docs/Prog/Manual/ApplicationLibraries/lib825

    v1 v1  
     1= Standard 825 Library (lib825) =
     2
     3[[TOC(heading=Table of Contents, Docs/Prog/Manual/*)]]
     4
     5== Utility Functions ==
     6Some simple routines may be used in the apps to convert between the hexadecimal strings and the various structures.
     7
     8{{{
     9#!Lineno
     10#!c
     11unsigned char inline hex_char_val(char ch)
     12{
     13  if(ch >= '0' && ch <= '9')
     14    return ch - '0';
     15  else
     16    return ch - 'A' + 10;
     17}
     18
     19void inline convert_hex_to_binary(char *hex, unsigned char *out)
     20{
     21  int n = 0, i = 0;
     22  while(hex[n] != 0)
     23  {
     24    out[i++] = (hex_char_val(hex[n]) * 16) + hex_char_val(hex[n + 1]);
     25    if(hex[n + 1] == '\0')
     26    {
     27      printf("Invalid hex string\r\n");
     28      break;
     29    }
     30    n += 2;
     31  }
     32}
     33
     34static char hexchar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
     35
     36void binary_to_hex_str(unsigned char *in, unsigned char *out, int len)
     37{
     38  int n;
     39  for(n = 0; n < len; n++)
     40  {
     41    out[n * 2] = hexchar[((0xF0 & in[n]) >> 4)];
     42    out[n * 2 + 1] = hexchar[(0x0F & in[n])];
     43  }
     44  out[n * 2] = '\0';
     45}
     46}}}
     47''These routines are packaged in [doxygen:hexutil_8cpp-source.html hexutil.cpp]''
     48
     49For example, an app may access time/date from the mainboard with the following:
     50
     51{{{
     52#!c
     53char* mnbd_dev "/dev/mnbd";
     54int g_fileMnBd = 0;
     55
     56int OpenMnBd(void)
     57{
     58  /* O_NONBLOCK allows open even with no carrier detect */
     59  g_fileMnBd = open(mnbd_dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
     60  if(g_fileMnBd == -1)
     61  {
     62    printf("Error opening %s\n", mnbd_dev);
     63    return -1;
     64  }
     65  return 0;
     66}
     67
     68void CloseMnBd(0)
     69{
     70  close(g_fileMnBd);
     71  return;
     72}
     73
     74int z;
     75char readbuf![50];
     76char rcvbuf![50];
     77int rcvbufpos = 0;
     78int nread;
     79
     80OpenMnBd();
     81
     82write(g_fileMnBd, "S\r", 2); // Request date/time from mainboard
     83
     84// In main loop of application:
     85for(;;)
     86{
     87  nread = read(g_fileMnBd, &readbuf, sizeof(readbuf) );
     88
     89  if(nread > 0)
     90  {
     91    for(z = 0; z < nread; z++)
     92    {
     93      if(readbuf[z] == '\r')
     94    {
     95
     96    rcvbuf[rcvbufpos] = '\0';
     97
     98    // When time/date is received from mainboard set the Linux clock
     99    if(rcvbuf![0] == 'S')
     100    {
     101      struct mnbd_datetime_struct dt;
     102      convert_hex_to_binary(rcvbuf + 2, (unsigned char *)&dt);
     103      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);
     104 
     105      // Set regular C tm struct to appropriate time values
     106      struct tm time;
     107      time.tm_year = dt.year - 1900;
     108      time.tm_mon = dt.month - 1;
     109      time.tm_mday = dt.day;
     110      time.tm_hour = dt.hour;
     111      time.tm_min = dt.minute;
     112      time.tm_sec = dt.second;
     113
     114      time_t tt = mktime(&time);
     115
     116      stime(&tt);
     117    }
     118
     119    rcvbufpos = 0;
     120  }
     121  else if(readbuf[z] >= 0x20 && readbuf[z] < 0x7F)
     122  {
     123    rcvbuf[rcvbufpos++] = readbuf[z];
     124  }
     125}
     126}}}
     127
     128[[Top]]
     129