wiki:Docs/Prog/Manual/ApplicationLibraries/lib825

Standard 825 Library with Application class (lib825)

The standard library (lib825) is a collection of Cardinal utilities that simplifies the process of writing code for the Cardinal 825. The library is distributed free of charge and available here to download.

This library contains all of the same functions as the newer lib825ev library http://tech.825spectrum.com/trac/wiki/Docs/Prog/Manual/ApplicationLibraries/lib825ev#Standard825LibrarywithEVENTfeatureslib825ev plus it contains the Application and DynamicForm classes. Applications using Application or DynamicForm classes will be required to link to this library. The Application class is an alternative to the event based programming model preferred for the lib825ev library.

For details about the Application and Dynamic Form classes refer to the lib825 library source code and Doxygen comments included in the source code.

Utility Functions

Some simple routines may be used in the apps to convert between the hexadecimal strings and the various structures.

Line 
1unsigned char inline hex_char_val(char ch)
2{
3  if(ch >= '0' && ch <= '9')
4    return ch - '0';
5  else
6    return ch - 'A' + 10;
7}
8
9void inline convert_hex_to_binary(char *hex, unsigned char *out)
10{
11  int n = 0, i = 0;
12  while(hex[n] != 0)
13  {
14    out[i++] = (hex_char_val(hex[n]) * 16) + hex_char_val(hex[n + 1]);
15    if(hex[n + 1] == '\0')
16    {
17      printf("Invalid hex string\r\n");
18      break;
19    }
20    n += 2;
21  }
22}
23
24static char hexchar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
25
26void binary_to_hex_str(unsigned char *in, unsigned char *out, int len)
27{
28  int n;
29  for(n = 0; n < len; n++)
30  {
31    out[n * 2] = hexchar[((0xF0 & in[n]) >> 4)];
32    out[n * 2 + 1] = hexchar[(0x0F & in[n])];
33  }
34  out[n * 2] = '\0';
35}

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:

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];
  }
}

[^]

Last modified 3 years ago Last modified on Jan 27, 2015, 2:22:57 PM