libftdi Archives

Subject: Re: [PATCH] -FT230X: Readout, decode and encode the RS232 inverson configuration bits.

From: Rogier Wolff <R.E.Wolff@xxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 18 Aug 2014 09:31:54 +0200
On Thu, Aug 14, 2014 at 03:43:01PM +0200, Uwe Bonnes wrote:
> +                fprintf(stdout,"Inversion on ");
> +                if (eeprom->rs232_inversion & INVERT_TXD) {
> +                    fprintf(stdout,"TXD");
> +                    i = 1;
> +                }
> +                if (eeprom->rs232_inversion & INVERT_RXD) {
> +                    if (i)
> +                        fprintf(stdout,", ");
> +                    fprintf(stdout,"RXD");
> +                    i = 1;
> +                }

I would write this a bit different: 

Already at the first call of the helper function, the total code is
less. When you reuse the helper, you start having /much/ less code...

Oh, and my code does "one pin" / "two pins" correctly...  :-) 

        Roger. 

------

#include <stdio.h>
#include <stdlib.h>

#define INVERT_TXD 1
#define INVERT_RXD 2
#define INVERT_RTS 4
#define INVERT_CTS 8
#define INVERT_DTR 16
#define INVERT_DSR 32
#define INVERT_DCD 64
#define INVERT_RI 128

struct bitnames {
  int mask; 
  char *name;
};

struct bitnames invbitlist[] = {
  {INVERT_TXD, "TXD"}, 
  {INVERT_RXD, "RXD"}, 
  {INVERT_RTS, "RTS"}, 
  {INVERT_CTS, "CTS"}, 
  {INVERT_DTR, "DTR"}, 
  {INVERT_DSR, "DSR"}, 
  {INVERT_DCD, "DCD"}, 
  {INVERT_RI, "RI"}, 
  {0, NULL}, 
};

void print_bitlist (char *pre, char *post, int bits, struct bitnames *bitnames)
{
  int i, n;
  if (!bits) return;

  printf ("%s", pre); 

  for (i=0, n=0;bitnames[i].mask;i++) {
    if(bits & bitnames[i].mask) {
      if (n++) printf (","); 
      printf (" %s", bitnames[i].name);
    } 
  }
  printf (post, (n==1)?"":"s"); 
}


int main (int argc, char **argv)
{
  int i;

  i = atoi (argv[1]);
  print_bitlist ("Inversion on", " pin%s\n", i, invbitlist);
  exit (0);
}
  


-- 
** R.E.Wolff@xxxxxxxxxxxx ** http://www.BitWizard.nl/ ** +31-15-2600998 **
**    Delftechpark 26 2628 XH  Delft, The Netherlands. KVK: 27239233    **
*-- BitWizard writes Linux device drivers for any device you may have! --*
The plan was simple, like my brother-in-law Phil. But unlike
Phil, this plan just might work.

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread