/ Repetier / gcode.h
gcode.h
  1  /*
  2      This file is part of Repetier-Firmware.
  3  
  4      Repetier-Firmware is free software: you can redistribute it and/or modify
  5      it under the terms of the GNU General Public License as published by
  6      the Free Software Foundation, either version 3 of the License, or
  7      (at your option) any later version.
  8  
  9      Repetier-Firmware is distributed in the hope that it will be useful,
 10      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12      GNU General Public License for more details.
 13  
 14      You should have received a copy of the GNU General Public License
 15      along with Repetier-Firmware.  If not, see <http://www.gnu.org/licenses/>.
 16  
 17  */
 18  #ifndef _GCODE_H
 19  #define _GCODE_H
 20  
 21  #include <avr/pgmspace.h>
 22  // Workaround for http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34734 
 23  //#ifdef PROGMEM 
 24  //#undef PROGMEM 
 25  //#define PROGMEM __attribute__((section(".progmem.data"))) 
 26  //#endif
 27  
 28  typedef struct { // 52 bytes per command needed
 29     unsigned int params;
 30     unsigned int params2;
 31     unsigned int N; // Line number
 32     unsigned int M;
 33     unsigned int G;
 34     float X;
 35     float Y;
 36     float Z;
 37     float E;
 38     float F;
 39     byte T;
 40     long S;
 41     long P;
 42     float I;
 43     float J;
 44     float R;   
 45     char *text; //text[17];
 46  } GCode;
 47  
 48  #ifndef EXTERNALSERIAL
 49  // Implement serial communication for one stream only!
 50  /*
 51    HardwareSerial.h - Hardware serial library for Wiring
 52    Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
 53  
 54    This library is free software; you can redistribute it and/or
 55    modify it under the terms of the GNU Lesser General Public
 56    License as published by the Free Software Foundation; either
 57    version 2.1 of the License, or (at your option) any later version.
 58  
 59    This library is distributed in the hope that it will be useful,
 60    but WITHOUT ANY WARRANTY; without even the implied warranty of
 61    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 62    Lesser General Public License for more details.
 63  
 64    You should have received a copy of the GNU Lesser General Public
 65    License along with this library; if not, write to the Free Software
 66    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 67  
 68    Modified 28 September 2010 by Mark Sproul
 69    
 70    Modified to use only 1 queue with fixed length by Repetier
 71  */
 72  
 73  #define SERIAL_BUFFER_SIZE 128
 74  #define SERIAL_BUFFER_MASK 127
 75  
 76  struct ring_buffer
 77  {
 78    unsigned char buffer[SERIAL_BUFFER_SIZE];
 79    volatile int head;
 80    volatile int tail;
 81  };
 82  class RFHardwareSerial : public Print
 83  {
 84    public:
 85      ring_buffer *_rx_buffer;
 86      ring_buffer *_tx_buffer;
 87      volatile uint8_t *_ubrrh;
 88      volatile uint8_t *_ubrrl;
 89      volatile uint8_t *_ucsra;
 90      volatile uint8_t *_ucsrb;
 91      volatile uint8_t *_udr;
 92      uint8_t _rxen;
 93      uint8_t _txen;
 94      uint8_t _rxcie;
 95      uint8_t _udrie;
 96      uint8_t _u2x;
 97    public:
 98      RFHardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
 99        volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
100        volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
101        volatile uint8_t *udr,
102        uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x);
103      void begin(unsigned long);
104      void end();
105      virtual int available(void);
106      virtual int peek(void);
107      virtual int read(void);
108      virtual void flush(void);
109  #ifdef COMPAT_PRE1
110      virtual void write(uint8_t);
111  #else
112      virtual size_t write(uint8_t);
113  #endif
114      using Print::write; // pull in write(str) and write(buf, size) from Print
115      operator bool();
116  };
117  extern RFHardwareSerial RFSerial; 
118  #define RFSERIAL RFSerial
119  extern ring_buffer tx_buffer;
120  #define WAIT_OUT_EMPTY while(tx_buffer.head != tx_buffer.tail) {}
121  #else
122  #define RFSERIAL Serial
123  #endif
124  
125  class SerialOutput : public Print {
126  public:
127    SerialOutput();
128  #ifdef COMPAT_PRE1
129    void write(uint8_t);
130  #else
131    size_t write(uint8_t);
132  #endif
133    void print_P(PGM_P ptr);
134    void println_P(PGM_P ptr);
135    void print_long_P(PGM_P ptr,long value);
136    void print_int_P(PGM_P ptr,int value);
137    void print_float_P(PGM_P ptr,float value,uint8_t digits = 2);
138    void println_long_P(PGM_P ptr,long value);
139    void println_int_P(PGM_P ptr,int value);
140    void println_float_P(PGM_P ptr,float value,uint8_t digits = 2);
141    void print_error_P(PGM_P ptr,bool newline);
142    void printFloat(double number, uint8_t digits=2); 
143  
144  };
145  #define OUT_P_I(p,i) out.print_int_P(PSTR(p),(int)(i))
146  #define OUT_P_I_LN(p,i) out.println_int_P(PSTR(p),(int)(i))
147  #define OUT_P_L(p,i) out.print_long_P(PSTR(p),(long)(i))
148  #define OUT_P_L_LN(p,i) out.println_long_P(PSTR(p),(long)(i))
149  #define OUT_P_F(p,i) out.print_float_P(PSTR(p),(float)(i))
150  #define OUT_P_F_LN(p,i) out.println_float_P(PSTR(p),(float)(i))
151  #define OUT_P_FX(p,i,x) out.print_float_P(PSTR(p),(float)(i),x)
152  #define OUT_P_FX_LN(p,i,x) out.println_float_P(PSTR(p),(float)(i),x)
153  #define OUT_P(p) out.print_P(PSTR(p))
154  #define OUT_P_LN(p) out.println_P(PSTR(p))
155  #define OUT_ERROR_P(p) out.print_error_P(PSTR(p),false)
156  #define OUT_ERROR_P_LN(p) out.print_error_P(PSTR(p),true)
157  #define OUT(v) out.print(v)
158  #define OUT_LN out.println()
159  extern SerialOutput out;
160  /** Get next command in command buffer. After the command is processed, call gcode_command_finished() */
161  extern GCode *gcode_next_command();
162  /** Frees the cache used by the last command fetched. */ 
163  extern void gcode_command_finished(GCode *code);
164  // check for new commands
165  extern void gcode_read_serial();
166  extern void gcode_execute_PString(PGM_P cmd);
167  extern void gcode_print_command(GCode *code);
168  extern byte gcode_comp_binary_size(char *ptr);
169  extern bool gcode_parse_binary(GCode *code,byte *buffer);
170  extern bool gcode_parse_ascii(GCode *code,char *line);
171  extern void emergencyStop();
172  
173  // Helper macros to detect, if parameter is stored in GCode struct
174  #define GCODE_HAS_N(a) ((a->params & 1)!=0)
175  #define GCODE_HAS_M(a) ((a->params & 2)!=0)
176  #define GCODE_HAS_G(a) ((a->params & 4)!=0)
177  #define GCODE_HAS_X(a) ((a->params & 8)!=0)
178  #define GCODE_HAS_Y(a) ((a->params & 16)!=0)
179  #define GCODE_HAS_Z(a) ((a->params & 32)!=0)
180  #define GCODE_HAS_NO_XYZ(a) ((a->params & 56)==0)
181  #define GCODE_HAS_E(a) ((a->params & 64)!=0)
182  #define GCODE_HAS_F(a) ((a->params & 256)!=0)
183  #define GCODE_HAS_T(a) ((a->params & 512)!=0)
184  #define GCODE_HAS_S(a) ((a->params & 1024)!=0)
185  #define GCODE_HAS_P(a) ((a->params & 2048)!=0)
186  #define GCODE_IS_V2(a) ((a->params & 4096)!=0)
187  #define GCODE_HAS_STRING(a) ((a->params & 32768)!=0)
188  #define GCODE_HAS_I(a) ((a->params2 & 1)!=0)
189  #define GCODE_HAS_J(a) ((a->params2 & 2)!=0)
190  #define GCODE_HAS_R(a) ((a->params2 & 4)!=0)
191  
192  extern byte debug_level;
193  #define DEBUG_ECHO ((debug_level & 1)!=0)
194  #define DEBUG_INFO ((debug_level & 2)!=0)
195  #define DEBUG_ERRORS ((debug_level & 4)!=0)
196  #define DEBUG_DRYRUN ((debug_level & 8)!=0)
197  #define DEBUG_COMMUNICATION ((debug_level & 16)!=0)
198  #define DEBUG_NO_MOVES ((debug_level & 32)!=0)
199  
200  #endif
201