changeset 3:a70e0a8ac73e

ostream inserters are nice
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Thu, 27 Mar 2014 17:24:33 +0000
parents dca0635e2d44
children 1e7540113e8d
files Makefile commands.cc formatting.h gpib.cc main.cc
diffstat 5 files changed, 107 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/Makefile	Tue Mar 25 20:44:48 2014 +0000
     1.2 +++ b/Makefile	Thu Mar 27 17:24:33 2014 +0000
     1.3 @@ -71,7 +71,7 @@
     1.4  		@echo g++ $^
     1.5  		avr-g++ -c $(CFLAGS) -o $@ $<
     1.6  
     1.7 -main.avr:       o/main.o o/commands.o o/gpib.o o/serial.o o/usb_serial.o
     1.8 +main.avr:       o/main.o o/formatting.o o/commands.o o/gpib.o o/serial.o o/usb_serial.o
     1.9  		@echo ld $^
    1.10  		avr-g++ -L/usr/local/avr/lib $(LDFLAGS) -o $@ $^ $(DASHV)
    1.11  		chmod -x $@
     2.1 --- a/commands.cc	Tue Mar 25 20:44:48 2014 +0000
     2.2 +++ b/commands.cc	Thu Mar 27 17:24:33 2014 +0000
     2.3 @@ -180,7 +180,7 @@
     2.4   */
     2.5  int16_t Command_auto(char *tail)
     2.6  {
     2.7 -   Serial.Send(ROMS("auto ")); Serial.Send(tail); Serial.Send();
     2.8 +   Serial<<ROMS("auto ")<<tail<<endl;
     2.9     return ECOMMAND;
    2.10  }
    2.11  
    2.12 @@ -235,7 +235,7 @@
    2.13   */
    2.14  int16_t Command_clr(char *tail)
    2.15  {
    2.16 -   Serial.Send(ROMS("clr ")); Serial.Send(tail); Serial.Send();
    2.17 +   Serial<<ROMS("clr ")<<tail<<endl;
    2.18     return ECOMMAND;
    2.19  }
    2.20  
    2.21 @@ -504,8 +504,7 @@
    2.22  
    2.23        if(!*tail)
    2.24        {
    2.25 -         USB.Send(addr);
    2.26 -         USB.Send();
    2.27 +         USB<<addr<<endl;
    2.28           USB.Flush();
    2.29  
    2.30           _delay_ms(100);
    2.31 @@ -526,33 +525,33 @@
    2.32        }
    2.33     }
    2.34  
    2.35 -   USB.Send(ROMS("ddrb="));   USB.Hex(uint8_t(DDRB));
    2.36 -   USB.Send(ROMS(" portb=")); USB.Hex(uint8_t(PORTB));
    2.37 -   USB.Send(ROMS(" pinb="));  USB.Hex(uint8_t(PINB));
    2.38 -   USB.Send();
    2.39 +   USB<<ROMS("ddrb=");   USB.Hex(uint8_t(DDRB));
    2.40 +   USB<<ROMS(" portb="); USB.Hex(uint8_t(PORTB));
    2.41 +   USB<<ROMS(" pinb=");  USB.Hex(uint8_t(PINB));
    2.42 +   USB<<endl;
    2.43  
    2.44 -   USB.Send(ROMS("ddrc="));   USB.Hex(uint8_t(DDRC));
    2.45 -   USB.Send(ROMS(" portc=")); USB.Hex(uint8_t(PORTC));
    2.46 -   USB.Send(ROMS(" pinc="));  USB.Hex(uint8_t(PINC));
    2.47 -   USB.Send();
    2.48 +   USB<<ROMS("ddrc=");   USB.Hex(uint8_t(DDRC));
    2.49 +   USB<<ROMS(" portc="); USB.Hex(uint8_t(PORTC));
    2.50 +   USB<<ROMS(" pinc=");  USB.Hex(uint8_t(PINC));
    2.51 +   USB<<endl;
    2.52  
    2.53 -   USB.Send(ROMS("ddrd="));   USB.Hex(uint8_t(DDRD));
    2.54 -   USB.Send(ROMS(" portd=")); USB.Hex(uint8_t(PORTD));
    2.55 -   USB.Send(ROMS(" pind="));  USB.Hex(uint8_t(PIND));
    2.56 -   USB.Send();
    2.57 +   USB<<ROMS("ddrd=");   USB.Hex(uint8_t(DDRD));
    2.58 +   USB<<ROMS(" portd="); USB.Hex(uint8_t(PORTD));
    2.59 +   USB<<ROMS(" pind=");  USB.Hex(uint8_t(PIND));
    2.60 +   USB<<endl;
    2.61  
    2.62 -   USB.Send(ROMS("ddre="));   USB.Hex(uint8_t(DDRE));
    2.63 -   USB.Send(ROMS(" porte=")); USB.Hex(uint8_t(PORTE));
    2.64 -   USB.Send(ROMS(" pine="));  USB.Hex(uint8_t(PINE));
    2.65 -   USB.Send();
    2.66 +   USB<<ROMS("ddre=");   USB.Hex(uint8_t(DDRE));
    2.67 +   USB<<ROMS(" porte="); USB.Hex(uint8_t(PORTE));
    2.68 +   USB<<ROMS(" pine=");  USB.Hex(uint8_t(PINE));
    2.69 +   USB<<endl;
    2.70  
    2.71 -   USB.Send(ROMS("ddrf="));   USB.Hex(uint8_t(DDRF));
    2.72 -   USB.Send(ROMS(" portf=")); USB.Hex(uint8_t(PORTF));
    2.73 -   USB.Send(ROMS(" pinf="));  USB.Hex(uint8_t(PINF));
    2.74 -   USB.Send();
    2.75 +   USB<<ROMS("ddrf=");   USB.Hex(uint8_t(DDRF));
    2.76 +   USB<<ROMS(" portf="); USB.Hex(uint8_t(PORTF));
    2.77 +   USB<<ROMS(" pinf=");  USB.Hex(uint8_t(PINF));
    2.78 +   USB<<endl;
    2.79  
    2.80     GPIB.Report();
    2.81 -   USB.Send();
    2.82 +   USB<<endl;
    2.83  
    2.84     return NONE;
    2.85  }
    2.86 @@ -587,7 +586,7 @@
    2.87   */
    2.88  int16_t Command_eoi(char *tail)
    2.89  {
    2.90 -   Serial.Send(ROMS("eoi ")); Serial.Send(tail); Serial.Send();
    2.91 +   Serial<<ROMS("eoi ")<<tail<<endl;
    2.92     return ECOMMAND;
    2.93  }
    2.94  
    2.95 @@ -606,7 +605,7 @@
    2.96   */
    2.97  int16_t Command_eos(char *tail)
    2.98  {
    2.99 -   Serial.Send(ROMS("eos ")); Serial.Send(tail); Serial.Send();
   2.100 +   Serial<<ROMS("eos ")<<tail<<endl;
   2.101     return ECOMMAND;
   2.102  }
   2.103  
   2.104 @@ -617,7 +616,7 @@
   2.105   */
   2.106  int16_t Command_eot_char(char *tail)
   2.107  {
   2.108 -   Serial.Send(ROMS("eot_char ")); Serial.Send(tail); Serial.Send();
   2.109 +   Serial<<ROMS("eot_char ")<<tail<<endl;
   2.110     return ECOMMAND;
   2.111  }
   2.112  
   2.113 @@ -631,7 +630,7 @@
   2.114   */
   2.115  int16_t Command_eot_enable(char *tail)
   2.116  {
   2.117 -   Serial.Send(ROMS("eot_enable ")); Serial.Send(tail); Serial.Send();
   2.118 +   Serial<<ROMS("eot_enable ")<<tail<<endl;
   2.119     return ECOMMAND;
   2.120  }
   2.121  
   2.122 @@ -642,7 +641,7 @@
   2.123   */
   2.124  int16_t Command_error(char *tail)
   2.125  {
   2.126 -   Serial.Send(ROMS("error ")); Serial.Send(tail); Serial.Send();
   2.127 +   Serial<<ROMS("error ")<<tail<<endl;
   2.128     return ECOMMAND;
   2.129  }
   2.130  
   2.131 @@ -656,7 +655,7 @@
   2.132   */
   2.133  int16_t Command_gts(char *tail)
   2.134  {
   2.135 -   Serial.Send(ROMS("gts ")); Serial.Send(tail); Serial.Send();
   2.136 +   Serial<<ROMS("gts ")<<tail<<endl;
   2.137     GPIB.Float();
   2.138     return ECOMMAND;
   2.139  }
   2.140 @@ -672,12 +671,12 @@
   2.141                            " version"
   2.142                            #include "version.h"
   2.143                            "\r\n";
   2.144 -   USB.Send(ROMS("Kingswood USB-GPIB-32U4 "));
   2.145 +   USB<<ROMS("Kingswood USB-GPIB-32U4 ");
   2.146     if(GPIB.MicroPro)
   2.147 -      USB.Send(ROMS("MicroPro"));
   2.148 +      USB<<ROMS("MicroPro");
   2.149     else
   2.150 -      USB.Send(ROMS("Teensy"));
   2.151 -   USB.Send(ROM(version));
   2.152 +      USB<<ROMS("Teensy");
   2.153 +   USB<<ROM(version);
   2.154  }
   2.155  
   2.156  /* ..........................................................................
   2.157 @@ -728,7 +727,7 @@
   2.158   */
   2.159  int16_t Command_llo(char *tail)
   2.160  {
   2.161 -   Serial.Send(ROMS("llo ")); Serial.Send(tail); Serial.Send();
   2.162 +   Serial<<ROMS("llo ")<<tail<<endl;
   2.163     return ECOMMAND;
   2.164  }
   2.165  
   2.166 @@ -739,7 +738,7 @@
   2.167   */
   2.168  int16_t Command_loc(char *tail)
   2.169  {
   2.170 -   Serial.Send(ROMS("loc ")); Serial.Send(tail); Serial.Send();
   2.171 +   Serial<<ROMS("loc ")<<tail<<endl;
   2.172     return ECOMMAND;
   2.173  }
   2.174  
   2.175 @@ -750,7 +749,7 @@
   2.176   */
   2.177  int16_t Command_lon(char *tail)
   2.178  {
   2.179 -   Serial.Send(ROMS("lon ")); Serial.Send(tail); Serial.Send();
   2.180 +   Serial<<ROMS("lon ")<<tail<<endl;
   2.181     return ECOMMAND;
   2.182  }
   2.183  
   2.184 @@ -763,7 +762,7 @@
   2.185   */
   2.186  int16_t Command_mode(char *tail)
   2.187  {
   2.188 -   Serial.Send(ROMS("mode ")); Serial.Send(tail); Serial.Send();
   2.189 +   Serial<<ROMS("mode ")<<tail<<endl;
   2.190     return ECOMMAND;
   2.191  }
   2.192  
   2.193 @@ -819,7 +818,7 @@
   2.194   */
   2.195  int16_t Command_pct(char *tail)
   2.196  {
   2.197 -   Serial.Send(ROMS("pct ")); Serial.Send(tail); Serial.Send();
   2.198 +   Serial<<ROMS("pct ")<<tail<<endl;
   2.199     return ECOMMAND;
   2.200  }
   2.201  
   2.202 @@ -883,7 +882,7 @@
   2.203     GPIB.NDAC(false);
   2.204     GPIB.NRFD(false);
   2.205  
   2.206 -   USB.Send();
   2.207 +   USB<<endl;
   2.208     return r<0 ? r : NONE;
   2.209  }
   2.210  
   2.211 @@ -916,7 +915,7 @@
   2.212   */
   2.213  int16_t Command_rpp(char *tail)
   2.214  {
   2.215 -   Serial.Send(ROMS("rpp ")); Serial.Send(tail); Serial.Send();
   2.216 +   Serial<<ROMS("rpp ")<<tail<<endl;
   2.217     return ECOMMAND;
   2.218  }
   2.219  
   2.220 @@ -930,7 +929,7 @@
   2.221   */
   2.222  int16_t Command_rsp(char *tail)
   2.223  {
   2.224 -   Serial.Send(ROMS("rsp ")); Serial.Send(tail); Serial.Send();
   2.225 +   Serial<<ROMS("rsp ")<<tail<<endl;
   2.226     return ECOMMAND;
   2.227  }
   2.228  
   2.229 @@ -955,7 +954,7 @@
   2.230   */
   2.231  int16_t Command_savecfg(char *tail)
   2.232  {
   2.233 -   Serial.Send(ROMS("savecfg ")); Serial.Send(tail); Serial.Send();
   2.234 +   Serial<<ROMS("savecfg ")<<tail<<endl;
   2.235     return ECOMMAND;
   2.236  }
   2.237  
   2.238 @@ -1018,7 +1017,7 @@
   2.239   */
   2.240  int16_t Command_spoll(char *tail)
   2.241  {
   2.242 -   Serial.Send(ROMS("spoll ")); Serial.Send(tail); Serial.Send();
   2.243 +   Serial<<ROMS("spoll ")<<tail<<endl;
   2.244     return ECOMMAND;
   2.245  }
   2.246  
   2.247 @@ -1055,7 +1054,7 @@
   2.248   */
   2.249  int16_t Command_status(char *tail)
   2.250  {
   2.251 -   Serial.Send(ROMS("status ")); Serial.Send(tail); Serial.Send();
   2.252 +   Serial<<ROMS("status ")<<tail<<endl;
   2.253     return ECOMMAND;
   2.254  }
   2.255  
   2.256 @@ -1085,7 +1084,7 @@
   2.257   */
   2.258  int16_t Command_trg(char *tail)
   2.259  {
   2.260 -   Serial.Send(ROMS("trg ")); Serial.Send(tail); Serial.Send();
   2.261 +   Serial<<ROMS("trg ")<<tail<<endl;
   2.262     return ECOMMAND;
   2.263  }
   2.264  
   2.265 @@ -1239,10 +1238,7 @@
   2.266     }
   2.267  
   2.268     if(r!=NONE)
   2.269 -   {
   2.270 -      USB.Send(r);
   2.271 -      USB.Send();
   2.272 -   }
   2.273 +      USB<<r<<endl;
   2.274     return r;
   2.275  }
   2.276  
   2.277 @@ -1255,7 +1251,7 @@
   2.278  int16_t Command_help(char *tail)
   2.279  {
   2.280     Version();
   2.281 -   USB.Send(ROMS("Available commands:"));
   2.282 +   USB<<ROMS("Available commands:");
   2.283  
   2.284     const char *p;
   2.285     for(uint8_t i=0; (p=(const char *)pgm_read_word(&commandTable[i].Command))!=0; i++)
   2.286 @@ -1264,19 +1260,16 @@
   2.287  
   2.288        if(*tail=='?' || c>='a' || c==*tail)
   2.289        {
   2.290 -         USB.Send(ROMS("\r\n   ++"));
   2.291 -         USB.Send(ROM(p));
   2.292 +         USB<<ROMS("\r\n   ++")<<ROM(p);
   2.293           if(p==String_cmd)
   2.294           {
   2.295 -            USB.Send(ROMS(" [-]<byte> ... or one of:\r\n        "));
   2.296 +            USB<<ROMS(" [-]<byte> ... or one of:\r\n        ");
   2.297              for(uint8_t i=0; (p=(const char *)pgm_read_word(&byteTable[i].Command))!=0; i++)
   2.298 -            {  USB.Send(' ');
   2.299 -               USB.Send(ROM(p));
   2.300 -            }
   2.301 +               USB<<' '<<ROM(p);
   2.302           }
   2.303        }
   2.304     }
   2.305 -   USB.Send(ROMS("\r\n"));
   2.306 +   USB<<endl;
   2.307     return NONE;
   2.308  }
   2.309  
     3.1 --- a/formatting.h	Tue Mar 25 20:44:48 2014 +0000
     3.2 +++ b/formatting.h	Thu Mar 27 17:24:33 2014 +0000
     3.3 @@ -16,6 +16,7 @@
     3.4  #define ROM(s) ((_ROMS)(intptr_t)(s))
     3.5  #define ROMS(s) ROM(PSTR(s))
     3.6  
     3.7 +PROGMEM extern const char CRLF[];
     3.8  template<class Output> class Formatting
     3.9  {
    3.10  public:
    3.11 @@ -112,8 +113,7 @@
    3.12     }
    3.13  
    3.14     static void __attribute__((noinline)) Send()
    3.15 -   {  Send(char('\r'));
    3.16 -      Send(char('\n'));
    3.17 +   {  Send(ROM(CRLF));
    3.18     }
    3.19  
    3.20  private:
    3.21 @@ -130,6 +130,40 @@
    3.22     }
    3.23  };
    3.24  
    3.25 +class FormattingNL { };
    3.26 +extern const FormattingNL endl;
    3.27 +
    3.28 +template<class Output> const Formatting<Output> &operator <<(const Formatting<Output> &o, int16_t value)
    3.29 +{  o.Send(value);
    3.30 +   return o;
    3.31 +}
    3.32 +
    3.33 +template<class Output> const Formatting<Output> &operator <<(const Formatting<Output> &o, uint16_t value)
    3.34 +{  o.Send(value);
    3.35 +   return o;
    3.36 +}
    3.37 +
    3.38 +template<class Output> const Formatting<Output> &operator <<(const Formatting<Output> &o, char value)
    3.39 +{  o.Send(value);
    3.40 +   return o;
    3.41 +}
    3.42 +
    3.43 +template<class Output> const Formatting<Output> &operator <<(const Formatting<Output> &o, const char *value)
    3.44 +{  o.Send(value);
    3.45 +   return o;
    3.46 +}
    3.47 +
    3.48 +template<class Output> const Formatting<Output> &operator <<(const Formatting<Output> &o, _ROMS value)
    3.49 +{  o.Send(value);
    3.50 +   return o;
    3.51 +}
    3.52 +
    3.53 +template<class Output> const Formatting<Output> &operator <<(const Formatting<Output> &o, const FormattingNL &)
    3.54 +{  o.Send();
    3.55 +   return o;
    3.56 +}
    3.57 +
    3.58 +
    3.59  #endif /* FORMATTING_H_ */
    3.60  
    3.61  /* ----- EOF formatting.h ----- */
     4.1 --- a/gpib.cc	Tue Mar 25 20:44:48 2014 +0000
     4.2 +++ b/gpib.cc	Thu Mar 27 17:24:33 2014 +0000
     4.3 @@ -126,25 +126,24 @@
     4.4  {
     4.5     if(msg)
     4.6     {
     4.7 -      Debug.Send(msg);
     4.8 -      Debug.Send(':');
     4.9 +      Debug<<msg<<':';
    4.10     }
    4.11 -   Debug.Send('<');
    4.12 +   Debug<<'<';
    4.13     Debug.Hex(Data());
    4.14  
    4.15     uint8_t lines=Lines();
    4.16 -   if(lines & (1<<PORTD_EOI))  Debug.Send(ROMS(" EOI"));
    4.17 -   if(lines & (1<<PORTF_ATN))  Debug.Send(ROMS(" ATN"));
    4.18 -   if(lines & (1<<PORTD_SRQ))  Debug.Send(ROMS(" SRQ"));
    4.19 -   if(lines & (1<<PORTF_REN))  Debug.Send(ROMS(" REN"));
    4.20 -   if(lines & (1<<PORTF_IFC))  Debug.Send(ROMS(" IFC"));
    4.21 -   if(lines & (1<<PORTD_NRFD)) Debug.Send(ROMS(" NRFD"));
    4.22 -   if(lines & (1<<PORTD_NDAC)) Debug.Send(ROMS(" NDAC"));
    4.23 -   if(lines & (1<<PORTD_DAV))  Debug.Send(ROMS(" DAV"));
    4.24 +   if(lines & (1<<PORTD_EOI))  Debug<<ROMS(" EOI");
    4.25 +   if(lines & (1<<PORTF_ATN))  Debug<<ROMS(" ATN");
    4.26 +   if(lines & (1<<PORTD_SRQ))  Debug<<ROMS(" SRQ");
    4.27 +   if(lines & (1<<PORTF_REN))  Debug<<ROMS(" REN");
    4.28 +   if(lines & (1<<PORTF_IFC))  Debug<<ROMS(" IFC");
    4.29 +   if(lines & (1<<PORTD_NRFD)) Debug<<ROMS(" NRFD");
    4.30 +   if(lines & (1<<PORTD_NDAC)) Debug<<ROMS(" NDAC");
    4.31 +   if(lines & (1<<PORTD_DAV))  Debug<<ROMS(" DAV");
    4.32  
    4.33 -   Debug.Send('>');
    4.34 +   Debug<<'>';
    4.35     if(msg)
    4.36 -      Debug.Send();
    4.37 +      Debug<<endl;
    4.38  }
    4.39  
    4.40  static const uint32_t TIMEOUT=0x1ff000ULL;  // 20 seconds
    4.41 @@ -197,7 +196,7 @@
    4.42     DAV(false);
    4.43  
    4.44     if(!NRFD() && !NDAC())
    4.45 -   {  USB.Send("++error 1\r\n");
    4.46 +   {  USB<<ROMS("++error 1\r\n");
    4.47        // disabled while debugging
    4.48        // return -1;
    4.49     }
    4.50 @@ -248,7 +247,7 @@
    4.51     PORTB=0xff;
    4.52     DDRB=0;
    4.53  
    4.54 -   USB.Send(ROMS("++error 2\r\n"));
    4.55 +   USB<<ROMS("++error 2\r\n");
    4.56     return -1;
    4.57  }
    4.58  
    4.59 @@ -317,7 +316,7 @@
    4.60  fail:
    4.61     NDAC(false);
    4.62     NRFD(false);
    4.63 -   USB.Send(ROMS("++error 4\r\n"));
    4.64 +   USB<<ROMS("++error 4\r\n");
    4.65     return -1;
    4.66  }
    4.67  
     5.1 --- a/main.cc	Tue Mar 25 20:44:48 2014 +0000
     5.2 +++ b/main.cc	Thu Mar 27 17:24:33 2014 +0000
     5.3 @@ -73,7 +73,7 @@
     5.4        while(1)
     5.5        {
     5.6           //GPIB.Report();
     5.7 -         USB.Send(ROMS("++"));
     5.8 +         USB<<ROMS("++");
     5.9  
    5.10           GPIB.LED(false);
    5.11  
    5.12 @@ -86,12 +86,12 @@
    5.13  
    5.14           if(n>3 && buf[0]=='+' && buf[1]=='+')
    5.15           {
    5.16 -            USB.Send();
    5.17 +            USB<<endl;
    5.18              Command(buf+2);
    5.19           }
    5.20           else if(!DirectMode)
    5.21           {
    5.22 -            USB.Send();
    5.23 +            USB<<endl;
    5.24              Command(buf);
    5.25           }
    5.26           else