Browse Source

core - arduino

Richard Wackerbarth 10 years ago
parent
commit
907032616e
42 changed files with 8078 additions and 0 deletions
  1. 249
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Arduino.h
  2. 211
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/CDC.cpp
  3. 45
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Client.h
  4. 518
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HID.cpp
  5. 252
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial.cpp
  6. 151
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial.h
  7. 79
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial0.cpp
  8. 69
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial1.cpp
  9. 57
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial2.cpp
  10. 57
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial3.cpp
  11. 123
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial_private.h
  12. 74
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/IPAddress.cpp
  13. 75
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/IPAddress.h
  14. 264
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Print.cpp
  15. 84
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Print.h
  16. 40
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Printable.h
  17. 30
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Server.h
  18. 317
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Stream.cpp
  19. 113
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Stream.h
  20. 618
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Tone.cpp
  21. 244
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/USBAPI.h
  22. 699
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/USBCore.cpp
  23. 303
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/USBCore.h
  24. 63
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/USBDesc.h
  25. 88
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Udp.h
  26. 168
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WCharacter.h
  27. 334
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WInterrupts.c
  28. 60
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WMath.cpp
  29. 745
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WString.cpp
  30. 224
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WString.h
  31. 35
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/abi.cpp
  32. 534
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/binary.h
  33. 31
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/hooks.c
  34. 49
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/main.cpp
  35. 36
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/new.cpp
  36. 30
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/new.h
  37. 325
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring.c
  38. 292
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_analog.c
  39. 181
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_digital.c
  40. 71
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_private.h
  41. 85
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_pulse.c
  42. 55
    0
      ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_shift.c

+ 249
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Arduino.h View File

@@ -0,0 +1,249 @@
1
+/*
2
+  Arduino.h - Main include file for the Arduino SDK
3
+  Copyright (c) 2005-2013 Arduino Team.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef Arduino_h
21
+#define Arduino_h
22
+
23
+#include <stdlib.h>
24
+#include <stdbool.h>
25
+#include <string.h>
26
+#include <math.h>
27
+
28
+#include <avr/pgmspace.h>
29
+#include <avr/io.h>
30
+#include <avr/interrupt.h>
31
+
32
+#include "binary.h"
33
+
34
+#ifdef __cplusplus
35
+extern "C"{
36
+#endif
37
+
38
+void yield(void);
39
+
40
+#define HIGH 0x1
41
+#define LOW  0x0
42
+
43
+#define INPUT 0x0
44
+#define OUTPUT 0x1
45
+#define INPUT_PULLUP 0x2
46
+
47
+#define PI 3.1415926535897932384626433832795
48
+#define HALF_PI 1.5707963267948966192313216916398
49
+#define TWO_PI 6.283185307179586476925286766559
50
+#define DEG_TO_RAD 0.017453292519943295769236907684886
51
+#define RAD_TO_DEG 57.295779513082320876798154814105
52
+#define EULER 2.718281828459045235360287471352
53
+
54
+#define SERIAL  0x0
55
+#define DISPLAY 0x1
56
+
57
+#define LSBFIRST 0
58
+#define MSBFIRST 1
59
+
60
+#define CHANGE 1
61
+#define FALLING 2
62
+#define RISING 3
63
+
64
+#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
65
+#define DEFAULT 0
66
+#define EXTERNAL 1
67
+#define INTERNAL 2
68
+#else  
69
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
70
+#define INTERNAL1V1 2
71
+#define INTERNAL2V56 3
72
+#else
73
+#define INTERNAL 3
74
+#endif
75
+#define DEFAULT 1
76
+#define EXTERNAL 0
77
+#endif
78
+
79
+// undefine stdlib's abs if encountered
80
+#ifdef abs
81
+#undef abs
82
+#endif
83
+
84
+#define min(a,b) ((a)<(b)?(a):(b))
85
+#define max(a,b) ((a)>(b)?(a):(b))
86
+#define abs(x) ((x)>0?(x):-(x))
87
+#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
88
+#define round(x)     ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
89
+#define radians(deg) ((deg)*DEG_TO_RAD)
90
+#define degrees(rad) ((rad)*RAD_TO_DEG)
91
+#define sq(x) ((x)*(x))
92
+
93
+#define interrupts() sei()
94
+#define noInterrupts() cli()
95
+
96
+#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
97
+#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
98
+#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
99
+
100
+#define lowByte(w) ((uint8_t) ((w) & 0xff))
101
+#define highByte(w) ((uint8_t) ((w) >> 8))
102
+
103
+#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
104
+#define bitSet(value, bit) ((value) |= (1UL << (bit)))
105
+#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
106
+#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
107
+
108
+// avr-libc defines _NOP() since 1.6.2
109
+#ifndef _NOP
110
+#define _NOP() do { __asm__ volatile ("nop"); } while (0)
111
+#endif
112
+
113
+typedef unsigned int word;
114
+
115
+#define bit(b) (1UL << (b))
116
+
117
+typedef bool boolean;
118
+typedef uint8_t byte;
119
+
120
+void init(void);
121
+void initVariant(void);
122
+
123
+int atexit(void (*func)()) __attribute__((weak));
124
+
125
+void pinMode(uint8_t, uint8_t);
126
+void digitalWrite(uint8_t, uint8_t);
127
+int digitalRead(uint8_t);
128
+int analogRead(uint8_t);
129
+void analogReference(uint8_t mode);
130
+void analogWrite(uint8_t, int);
131
+
132
+unsigned long millis(void);
133
+unsigned long micros(void);
134
+void delay(unsigned long);
135
+void delayMicroseconds(unsigned int us);
136
+unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
137
+
138
+void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
139
+uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
140
+
141
+void attachInterrupt(uint8_t, void (*)(void), int mode);
142
+void detachInterrupt(uint8_t);
143
+
144
+void setup(void);
145
+void loop(void);
146
+
147
+// Get the bit location within the hardware port of the given virtual pin.
148
+// This comes from the pins_*.c file for the active board configuration.
149
+
150
+#define analogInPinToBit(P) (P)
151
+
152
+// On the ATmega1280, the addresses of some of the port registers are
153
+// greater than 255, so we can't store them in uint8_t's.
154
+extern const uint16_t PROGMEM port_to_mode_PGM[];
155
+extern const uint16_t PROGMEM port_to_input_PGM[];
156
+extern const uint16_t PROGMEM port_to_output_PGM[];
157
+
158
+extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
159
+// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
160
+extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
161
+extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
162
+
163
+// Get the bit location within the hardware port of the given virtual pin.
164
+// This comes from the pins_*.c file for the active board configuration.
165
+// 
166
+// These perform slightly better as macros compared to inline functions
167
+//
168
+#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
169
+#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
170
+#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
171
+#define analogInPinToBit(P) (P)
172
+#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
173
+#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
174
+#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
175
+
176
+#define NOT_A_PIN 0
177
+#define NOT_A_PORT 0
178
+
179
+#define NOT_AN_INTERRUPT -1
180
+
181
+#ifdef ARDUINO_MAIN
182
+#define PA 1
183
+#define PB 2
184
+#define PC 3
185
+#define PD 4
186
+#define PE 5
187
+#define PF 6
188
+#define PG 7
189
+#define PH 8
190
+#define PJ 10
191
+#define PK 11
192
+#define PL 12
193
+#endif
194
+
195
+#define NOT_ON_TIMER 0
196
+#define TIMER0A 1
197
+#define TIMER0B 2
198
+#define TIMER1A 3
199
+#define TIMER1B 4
200
+#define TIMER1C 5
201
+#define TIMER2  6
202
+#define TIMER2A 7
203
+#define TIMER2B 8
204
+
205
+#define TIMER3A 9
206
+#define TIMER3B 10
207
+#define TIMER3C 11
208
+#define TIMER4A 12
209
+#define TIMER4B 13
210
+#define TIMER4C 14
211
+#define TIMER4D 15
212
+#define TIMER5A 16
213
+#define TIMER5B 17
214
+#define TIMER5C 18
215
+
216
+#ifdef __cplusplus
217
+} // extern "C"
218
+#endif
219
+
220
+#ifdef __cplusplus
221
+#include "WCharacter.h"
222
+#include "WString.h"
223
+#include "HardwareSerial.h"
224
+#include "USBAPI.h"
225
+#if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL)
226
+#error "Targets with both UART0 and CDC serial not supported"
227
+#endif
228
+
229
+uint16_t makeWord(uint16_t w);
230
+uint16_t makeWord(byte h, byte l);
231
+
232
+#define word(...) makeWord(__VA_ARGS__)
233
+
234
+unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
235
+
236
+void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
237
+void noTone(uint8_t _pin);
238
+
239
+// WMath prototypes
240
+long random(long);
241
+long random(long, long);
242
+void randomSeed(unsigned int);
243
+long map(long, long, long, long, long);
244
+
245
+#endif
246
+
247
+#include "pins_arduino.h"
248
+
249
+#endif

+ 211
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/CDC.cpp View File

@@ -0,0 +1,211 @@
1
+
2
+
3
+/* Copyright (c) 2011, Peter Barrett  
4
+**  
5
+** Permission to use, copy, modify, and/or distribute this software for  
6
+** any purpose with or without fee is hereby granted, provided that the  
7
+** above copyright notice and this permission notice appear in all copies.  
8
+** 
9
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL  
10
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED  
11
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR  
12
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES  
13
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  
14
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  
15
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS  
16
+** SOFTWARE.  
17
+*/
18
+
19
+#include "USBAPI.h"
20
+#include <avr/wdt.h>
21
+
22
+#if defined(USBCON)
23
+#ifdef CDC_ENABLED
24
+
25
+typedef struct
26
+{
27
+	u32	dwDTERate;
28
+	u8	bCharFormat;
29
+	u8 	bParityType;
30
+	u8 	bDataBits;
31
+	u8	lineState;
32
+} LineInfo;
33
+
34
+static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
35
+
36
+#define WEAK __attribute__ ((weak))
37
+
38
+extern const CDCDescriptor _cdcInterface PROGMEM;
39
+const CDCDescriptor _cdcInterface =
40
+{
41
+	D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
42
+
43
+	//	CDC communication interface
44
+	D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
45
+	D_CDCCS(CDC_HEADER,0x10,0x01),								// Header (1.10 bcd)
46
+	D_CDCCS(CDC_CALL_MANAGEMENT,1,1),							// Device handles call management (not)
47
+	D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6),				// SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
48
+	D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE),	// Communication interface is master, data interface is slave 0
49
+	D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
50
+
51
+	//	CDC data interface
52
+	D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
53
+	D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
54
+	D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
55
+};
56
+
57
+int WEAK CDC_GetInterface(u8* interfaceNum)
58
+{
59
+	interfaceNum[0] += 2;	// uses 2
60
+	return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
61
+}
62
+
63
+bool WEAK CDC_Setup(Setup& setup)
64
+{
65
+	u8 r = setup.bRequest;
66
+	u8 requestType = setup.bmRequestType;
67
+
68
+	if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
69
+	{
70
+		if (CDC_GET_LINE_CODING == r)
71
+		{
72
+			USB_SendControl(0,(void*)&_usbLineInfo,7);
73
+			return true;
74
+		}
75
+	}
76
+
77
+	if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
78
+	{
79
+		if (CDC_SET_LINE_CODING == r)
80
+		{
81
+			USB_RecvControl((void*)&_usbLineInfo,7);
82
+		}
83
+
84
+		if (CDC_SET_CONTROL_LINE_STATE == r)
85
+		{
86
+			_usbLineInfo.lineState = setup.wValueL;
87
+		}
88
+
89
+		if (CDC_SET_LINE_CODING == r || CDC_SET_CONTROL_LINE_STATE == r)
90
+		{
91
+			// auto-reset into the bootloader is triggered when the port, already 
92
+			// open at 1200 bps, is closed.  this is the signal to start the watchdog
93
+			// with a relatively long period so it can finish housekeeping tasks
94
+			// like servicing endpoints before the sketch ends
95
+
96
+			// We check DTR state to determine if host port is open (bit 0 of lineState).
97
+			if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0)
98
+			{
99
+				*(uint16_t *)0x0800 = 0x7777;
100
+				wdt_enable(WDTO_120MS);
101
+			}
102
+			else
103
+			{
104
+				// Most OSs do some intermediate steps when configuring ports and DTR can
105
+				// twiggle more than once before stabilizing.
106
+				// To avoid spurious resets we set the watchdog to 250ms and eventually
107
+				// cancel if DTR goes back high.
108
+
109
+				wdt_disable();
110
+				wdt_reset();
111
+				*(uint16_t *)0x0800 = 0x0;
112
+			}
113
+		}
114
+		return true;
115
+	}
116
+	return false;
117
+}
118
+
119
+
120
+void Serial_::begin(unsigned long /* baud_count */)
121
+{
122
+	peek_buffer = -1;
123
+}
124
+
125
+void Serial_::begin(unsigned long /* baud_count */, byte /* config */)
126
+{
127
+	peek_buffer = -1;
128
+}
129
+
130
+void Serial_::end(void)
131
+{
132
+}
133
+
134
+int Serial_::available(void)
135
+{
136
+	if (peek_buffer >= 0) {
137
+		return 1 + USB_Available(CDC_RX);
138
+	}
139
+	return USB_Available(CDC_RX);
140
+}
141
+
142
+int Serial_::peek(void)
143
+{
144
+	if (peek_buffer < 0)
145
+		peek_buffer = USB_Recv(CDC_RX);
146
+	return peek_buffer;
147
+}
148
+
149
+int Serial_::read(void)
150
+{
151
+	if (peek_buffer >= 0) {
152
+		int c = peek_buffer;
153
+		peek_buffer = -1;
154
+		return c;
155
+	}
156
+	return USB_Recv(CDC_RX);
157
+}
158
+
159
+void Serial_::flush(void)
160
+{
161
+	USB_Flush(CDC_TX);
162
+}
163
+
164
+size_t Serial_::write(uint8_t c)
165
+{
166
+	return write(&c, 1);
167
+}
168
+
169
+size_t Serial_::write(const uint8_t *buffer, size_t size)
170
+{
171
+	/* only try to send bytes if the high-level CDC connection itself 
172
+	 is open (not just the pipe) - the OS should set lineState when the port
173
+	 is opened and clear lineState when the port is closed.
174
+	 bytes sent before the user opens the connection or after
175
+	 the connection is closed are lost - just like with a UART. */
176
+	
177
+	// TODO - ZE - check behavior on different OSes and test what happens if an
178
+	// open connection isn't broken cleanly (cable is yanked out, host dies
179
+	// or locks up, or host virtual serial port hangs)
180
+	if (_usbLineInfo.lineState > 0)	{
181
+		int r = USB_Send(CDC_TX,buffer,size);
182
+		if (r > 0) {
183
+			return r;
184
+		} else {
185
+			setWriteError();
186
+			return 0;
187
+		}
188
+	}
189
+	setWriteError();
190
+	return 0;
191
+}
192
+
193
+// This operator is a convenient way for a sketch to check whether the
194
+// port has actually been configured and opened by the host (as opposed
195
+// to just being connected to the host).  It can be used, for example, in 
196
+// setup() before printing to ensure that an application on the host is
197
+// actually ready to receive and display the data.
198
+// We add a short delay before returning to fix a bug observed by Federico
199
+// where the port is configured (lineState != 0) but not quite opened.
200
+Serial_::operator bool() {
201
+	bool result = false;
202
+	if (_usbLineInfo.lineState > 0) 
203
+		result = true;
204
+	delay(10);
205
+	return result;
206
+}
207
+
208
+Serial_ Serial;
209
+
210
+#endif
211
+#endif /* if defined(USBCON) */

+ 45
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Client.h View File

@@ -0,0 +1,45 @@
1
+/*
2
+  Client.h - Base class that provides Client
3
+  Copyright (c) 2011 Adrian McEwen.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef client_h
21
+#define client_h
22
+#include "Print.h"
23
+#include "Stream.h"
24
+#include "IPAddress.h"
25
+
26
+class Client : public Stream {
27
+
28
+public:
29
+  virtual int connect(IPAddress ip, uint16_t port) =0;
30
+  virtual int connect(const char *host, uint16_t port) =0;
31
+  virtual size_t write(uint8_t) =0;
32
+  virtual size_t write(const uint8_t *buf, size_t size) =0;
33
+  virtual int available() = 0;
34
+  virtual int read() = 0;
35
+  virtual int read(uint8_t *buf, size_t size) = 0;
36
+  virtual int peek() = 0;
37
+  virtual void flush() = 0;
38
+  virtual void stop() = 0;
39
+  virtual uint8_t connected() = 0;
40
+  virtual operator bool() = 0;
41
+protected:
42
+  uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
43
+};
44
+
45
+#endif

+ 518
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HID.cpp View File

@@ -0,0 +1,518 @@
1
+
2
+
3
+/* Copyright (c) 2011, Peter Barrett  
4
+**  
5
+** Permission to use, copy, modify, and/or distribute this software for  
6
+** any purpose with or without fee is hereby granted, provided that the  
7
+** above copyright notice and this permission notice appear in all copies.  
8
+** 
9
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL  
10
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED  
11
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR  
12
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES  
13
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  
14
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  
15
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS  
16
+** SOFTWARE.  
17
+*/
18
+
19
+#include "USBAPI.h"
20
+
21
+#if defined(USBCON)
22
+#ifdef HID_ENABLED
23
+
24
+//#define RAWHID_ENABLED
25
+
26
+//	Singletons for mouse and keyboard
27
+
28
+Mouse_ Mouse;
29
+Keyboard_ Keyboard;
30
+
31
+//================================================================================
32
+//================================================================================
33
+
34
+//	HID report descriptor
35
+
36
+#define LSB(_x) ((_x) & 0xFF)
37
+#define MSB(_x) ((_x) >> 8)
38
+
39
+#define RAWHID_USAGE_PAGE	0xFFC0
40
+#define RAWHID_USAGE		0x0C00
41
+#define RAWHID_TX_SIZE 64
42
+#define RAWHID_RX_SIZE 64
43
+
44
+extern const u8 _hidReportDescriptor[] PROGMEM;
45
+const u8 _hidReportDescriptor[] = {
46
+	
47
+	//	Mouse
48
+    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)	// 54
49
+    0x09, 0x02,                    // USAGE (Mouse)
50
+    0xa1, 0x01,                    // COLLECTION (Application)
51
+    0x09, 0x01,                    //   USAGE (Pointer)
52
+    0xa1, 0x00,                    //   COLLECTION (Physical)
53
+    0x85, 0x01,                    //     REPORT_ID (1)
54
+    0x05, 0x09,                    //     USAGE_PAGE (Button)
55
+    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
56
+    0x29, 0x03,                    //     USAGE_MAXIMUM (Button 3)
57
+    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
58
+    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
59
+    0x95, 0x03,                    //     REPORT_COUNT (3)
60
+    0x75, 0x01,                    //     REPORT_SIZE (1)
61
+    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
62
+    0x95, 0x01,                    //     REPORT_COUNT (1)
63
+    0x75, 0x05,                    //     REPORT_SIZE (5)
64
+    0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
65
+    0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
66
+    0x09, 0x30,                    //     USAGE (X)
67
+    0x09, 0x31,                    //     USAGE (Y)
68
+    0x09, 0x38,                    //     USAGE (Wheel)
69
+    0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
70
+    0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
71
+    0x75, 0x08,                    //     REPORT_SIZE (8)
72
+    0x95, 0x03,                    //     REPORT_COUNT (3)
73
+    0x81, 0x06,                    //     INPUT (Data,Var,Rel)
74
+    0xc0,                          //   END_COLLECTION
75
+    0xc0,                          // END_COLLECTION
76
+
77
+	//	Keyboard
78
+    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)	// 47
79
+    0x09, 0x06,                    // USAGE (Keyboard)
80
+    0xa1, 0x01,                    // COLLECTION (Application)
81
+    0x85, 0x02,                    //   REPORT_ID (2)
82
+    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
83
+   
84
+	0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
85
+    0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
86
+    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
87
+    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
88
+    0x75, 0x01,                    //   REPORT_SIZE (1)
89
+    
90
+	0x95, 0x08,                    //   REPORT_COUNT (8)
91
+    0x81, 0x02,                    //   INPUT (Data,Var,Abs)
92
+    0x95, 0x01,                    //   REPORT_COUNT (1)
93
+    0x75, 0x08,                    //   REPORT_SIZE (8)
94
+    0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
95
+    
96
+	0x95, 0x06,                    //   REPORT_COUNT (6)
97
+    0x75, 0x08,                    //   REPORT_SIZE (8)
98
+    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
99
+    0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
100
+    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
101
+    
102
+	0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
103
+    0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
104
+    0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
105
+    0xc0,                          // END_COLLECTION
106
+
107
+#ifdef RAWHID_ENABLED
108
+	//	RAW HID
109
+	0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE),	// 30
110
+	0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
111
+
112
+	0xA1, 0x01,				// Collection 0x01
113
+    0x85, 0x03,             // REPORT_ID (3)
114
+	0x75, 0x08,				// report size = 8 bits
115
+	0x15, 0x00,				// logical minimum = 0
116
+	0x26, 0xFF, 0x00,		// logical maximum = 255
117
+
118
+	0x95, 64,				// report count TX
119
+	0x09, 0x01,				// usage
120
+	0x81, 0x02,				// Input (array)
121
+
122
+	0x95, 64,				// report count RX
123
+	0x09, 0x02,				// usage
124
+	0x91, 0x02,				// Output (array)
125
+	0xC0					// end collection
126
+#endif
127
+};
128
+
129
+extern const HIDDescriptor _hidInterface PROGMEM;
130
+const HIDDescriptor _hidInterface =
131
+{
132
+	D_INTERFACE(HID_INTERFACE,1,3,0,0),
133
+	D_HIDREPORT(sizeof(_hidReportDescriptor)),
134
+	D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
135
+};
136
+
137
+//================================================================================
138
+//================================================================================
139
+//	Driver
140
+
141
+u8 _hid_protocol = 1;
142
+u8 _hid_idle = 1;
143
+
144
+#define WEAK __attribute__ ((weak))
145
+
146
+int WEAK HID_GetInterface(u8* interfaceNum)
147
+{
148
+	interfaceNum[0] += 1;	// uses 1
149
+	return USB_SendControl(TRANSFER_PGM,&_hidInterface,sizeof(_hidInterface));
150
+}
151
+
152
+int WEAK HID_GetDescriptor(int /* i */)
153
+{
154
+	return USB_SendControl(TRANSFER_PGM,_hidReportDescriptor,sizeof(_hidReportDescriptor));
155
+}
156
+
157
+void WEAK HID_SendReport(u8 id, const void* data, int len)
158
+{
159
+	USB_Send(HID_TX, &id, 1);
160
+	USB_Send(HID_TX | TRANSFER_RELEASE,data,len);
161
+}
162
+
163
+bool WEAK HID_Setup(Setup& setup)
164
+{
165
+	u8 r = setup.bRequest;
166
+	u8 requestType = setup.bmRequestType;
167
+	if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
168
+	{
169
+		if (HID_GET_REPORT == r)
170
+		{
171
+			//HID_GetReport();
172
+			return true;
173
+		}
174
+		if (HID_GET_PROTOCOL == r)
175
+		{
176
+			//Send8(_hid_protocol);	// TODO
177
+			return true;
178
+		}
179
+	}
180
+	
181
+	if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
182
+	{
183
+		if (HID_SET_PROTOCOL == r)
184
+		{
185
+			_hid_protocol = setup.wValueL;
186
+			return true;
187
+		}
188
+
189
+		if (HID_SET_IDLE == r)
190
+		{
191
+			_hid_idle = setup.wValueL;
192
+			return true;
193
+		}
194
+	}
195
+	return false;
196
+}
197
+
198
+//================================================================================
199
+//================================================================================
200
+//	Mouse
201
+
202
+Mouse_::Mouse_(void) : _buttons(0)
203
+{
204
+}
205
+
206
+void Mouse_::begin(void) 
207
+{
208
+}
209
+
210
+void Mouse_::end(void) 
211
+{
212
+}
213
+
214
+void Mouse_::click(uint8_t b)
215
+{
216
+	_buttons = b;
217
+	move(0,0,0);
218
+	_buttons = 0;
219
+	move(0,0,0);
220
+}
221
+
222
+void Mouse_::move(signed char x, signed char y, signed char wheel)
223
+{
224
+	u8 m[4];
225
+	m[0] = _buttons;
226
+	m[1] = x;
227
+	m[2] = y;
228
+	m[3] = wheel;
229
+	HID_SendReport(1,m,4);
230
+}
231
+
232
+void Mouse_::buttons(uint8_t b)
233
+{
234
+	if (b != _buttons)
235
+	{
236
+		_buttons = b;
237
+		move(0,0,0);
238
+	}
239
+}
240
+
241
+void Mouse_::press(uint8_t b) 
242
+{
243
+	buttons(_buttons | b);
244
+}
245
+
246
+void Mouse_::release(uint8_t b)
247
+{
248
+	buttons(_buttons & ~b);
249
+}
250
+
251
+bool Mouse_::isPressed(uint8_t b)
252
+{
253
+	if ((b & _buttons) > 0) 
254
+		return true;
255
+	return false;
256
+}
257
+
258
+//================================================================================
259
+//================================================================================
260
+//	Keyboard
261
+
262
+Keyboard_::Keyboard_(void) 
263
+{
264
+}
265
+
266
+void Keyboard_::begin(void) 
267
+{
268
+}
269
+
270
+void Keyboard_::end(void) 
271
+{
272
+}
273
+
274
+void Keyboard_::sendReport(KeyReport* keys)
275
+{
276
+	HID_SendReport(2,keys,sizeof(KeyReport));
277
+}
278
+
279
+extern
280
+const uint8_t _asciimap[128] PROGMEM;
281
+
282
+#define SHIFT 0x80
283
+const uint8_t _asciimap[128] =
284
+{
285
+	0x00,             // NUL
286
+	0x00,             // SOH
287
+	0x00,             // STX
288
+	0x00,             // ETX
289
+	0x00,             // EOT
290
+	0x00,             // ENQ
291
+	0x00,             // ACK  
292
+	0x00,             // BEL
293
+	0x2a,			// BS	Backspace
294
+	0x2b,			// TAB	Tab
295
+	0x28,			// LF	Enter
296
+	0x00,             // VT 
297
+	0x00,             // FF 
298
+	0x00,             // CR 
299
+	0x00,             // SO 
300
+	0x00,             // SI 
301
+	0x00,             // DEL
302
+	0x00,             // DC1
303
+	0x00,             // DC2
304
+	0x00,             // DC3
305
+	0x00,             // DC4
306
+	0x00,             // NAK
307
+	0x00,             // SYN
308
+	0x00,             // ETB
309
+	0x00,             // CAN
310
+	0x00,             // EM 
311
+	0x00,             // SUB
312
+	0x00,             // ESC
313
+	0x00,             // FS 
314
+	0x00,             // GS 
315
+	0x00,             // RS 
316
+	0x00,             // US 
317
+
318
+	0x2c,		   //  ' '
319
+	0x1e|SHIFT,	   // !
320
+	0x34|SHIFT,	   // "
321
+	0x20|SHIFT,    // #
322
+	0x21|SHIFT,    // $
323
+	0x22|SHIFT,    // %
324
+	0x24|SHIFT,    // &
325
+	0x34,          // '
326
+	0x26|SHIFT,    // (
327
+	0x27|SHIFT,    // )
328
+	0x25|SHIFT,    // *
329
+	0x2e|SHIFT,    // +
330
+	0x36,          // ,
331
+	0x2d,          // -
332
+	0x37,          // .
333
+	0x38,          // /
334
+	0x27,          // 0
335
+	0x1e,          // 1
336
+	0x1f,          // 2
337
+	0x20,          // 3
338
+	0x21,          // 4
339
+	0x22,          // 5
340
+	0x23,          // 6
341
+	0x24,          // 7
342
+	0x25,          // 8
343
+	0x26,          // 9
344
+	0x33|SHIFT,      // :
345
+	0x33,          // ;
346
+	0x36|SHIFT,      // <
347
+	0x2e,          // =
348
+	0x37|SHIFT,      // >
349
+	0x38|SHIFT,      // ?
350
+	0x1f|SHIFT,      // @
351
+	0x04|SHIFT,      // A
352
+	0x05|SHIFT,      // B
353
+	0x06|SHIFT,      // C
354
+	0x07|SHIFT,      // D
355
+	0x08|SHIFT,      // E
356
+	0x09|SHIFT,      // F
357
+	0x0a|SHIFT,      // G
358
+	0x0b|SHIFT,      // H
359
+	0x0c|SHIFT,      // I
360
+	0x0d|SHIFT,      // J
361
+	0x0e|SHIFT,      // K
362
+	0x0f|SHIFT,      // L
363
+	0x10|SHIFT,      // M
364
+	0x11|SHIFT,      // N
365
+	0x12|SHIFT,      // O
366
+	0x13|SHIFT,      // P
367
+	0x14|SHIFT,      // Q
368
+	0x15|SHIFT,      // R
369
+	0x16|SHIFT,      // S
370
+	0x17|SHIFT,      // T
371
+	0x18|SHIFT,      // U
372
+	0x19|SHIFT,      // V
373
+	0x1a|SHIFT,      // W
374
+	0x1b|SHIFT,      // X
375
+	0x1c|SHIFT,      // Y
376
+	0x1d|SHIFT,      // Z
377
+	0x2f,          // [
378
+	0x31,          // bslash
379
+	0x30,          // ]
380
+	0x23|SHIFT,    // ^
381
+	0x2d|SHIFT,    // _
382
+	0x35,          // `
383
+	0x04,          // a
384
+	0x05,          // b
385
+	0x06,          // c
386
+	0x07,          // d
387
+	0x08,          // e
388
+	0x09,          // f
389
+	0x0a,          // g
390
+	0x0b,          // h
391
+	0x0c,          // i
392
+	0x0d,          // j
393
+	0x0e,          // k
394
+	0x0f,          // l
395
+	0x10,          // m
396
+	0x11,          // n
397
+	0x12,          // o
398
+	0x13,          // p
399
+	0x14,          // q
400
+	0x15,          // r
401
+	0x16,          // s
402
+	0x17,          // t
403
+	0x18,          // u
404
+	0x19,          // v
405
+	0x1a,          // w
406
+	0x1b,          // x
407
+	0x1c,          // y
408
+	0x1d,          // z
409
+	0x2f|SHIFT,    // 
410
+	0x31|SHIFT,    // |
411
+	0x30|SHIFT,    // }
412
+	0x35|SHIFT,    // ~
413
+	0				// DEL
414
+};
415
+
416
+uint8_t USBPutChar(uint8_t c);
417
+
418
+// press() adds the specified key (printing, non-printing, or modifier)
419
+// to the persistent key report and sends the report.  Because of the way 
420
+// USB HID works, the host acts like the key remains pressed until we 
421
+// call release(), releaseAll(), or otherwise clear the report and resend.
422
+size_t Keyboard_::press(uint8_t k) 
423
+{
424
+	uint8_t i;
425
+	if (k >= 136) {			// it's a non-printing key (not a modifier)
426
+		k = k - 136;
427
+	} else if (k >= 128) {	// it's a modifier key
428
+		_keyReport.modifiers |= (1<<(k-128));
429
+		k = 0;
430
+	} else {				// it's a printing key
431
+		k = pgm_read_byte(_asciimap + k);
432
+		if (!k) {
433
+			setWriteError();
434
+			return 0;
435
+		}
436
+		if (k & 0x80) {						// it's a capital letter or other character reached with shift
437
+			_keyReport.modifiers |= 0x02;	// the left shift modifier
438
+			k &= 0x7F;
439
+		}
440
+	}
441
+	
442
+	// Add k to the key report only if it's not already present
443
+	// and if there is an empty slot.
444
+	if (_keyReport.keys[0] != k && _keyReport.keys[1] != k && 
445
+		_keyReport.keys[2] != k && _keyReport.keys[3] != k &&
446
+		_keyReport.keys[4] != k && _keyReport.keys[5] != k) {
447
+		
448
+		for (i=0; i<6; i++) {
449
+			if (_keyReport.keys[i] == 0x00) {
450
+				_keyReport.keys[i] = k;
451
+				break;
452
+			}
453
+		}
454
+		if (i == 6) {
455
+			setWriteError();
456
+			return 0;
457
+		}	
458
+	}
459
+	sendReport(&_keyReport);
460
+	return 1;
461
+}
462
+
463
+// release() takes the specified key out of the persistent key report and
464
+// sends the report.  This tells the OS the key is no longer pressed and that
465
+// it shouldn't be repeated any more.
466
+size_t Keyboard_::release(uint8_t k) 
467
+{
468
+	uint8_t i;
469
+	if (k >= 136) {			// it's a non-printing key (not a modifier)
470
+		k = k - 136;
471
+	} else if (k >= 128) {	// it's a modifier key
472
+		_keyReport.modifiers &= ~(1<<(k-128));
473
+		k = 0;
474
+	} else {				// it's a printing key
475
+		k = pgm_read_byte(_asciimap + k);
476
+		if (!k) {
477
+			return 0;
478
+		}
479
+		if (k & 0x80) {							// it's a capital letter or other character reached with shift
480
+			_keyReport.modifiers &= ~(0x02);	// the left shift modifier
481
+			k &= 0x7F;
482
+		}
483
+	}
484
+	
485
+	// Test the key report to see if k is present.  Clear it if it exists.
486
+	// Check all positions in case the key is present more than once (which it shouldn't be)
487
+	for (i=0; i<6; i++) {
488
+		if (0 != k && _keyReport.keys[i] == k) {
489
+			_keyReport.keys[i] = 0x00;
490
+		}
491
+	}
492
+
493
+	sendReport(&_keyReport);
494
+	return 1;
495
+}
496
+
497
+void Keyboard_::releaseAll(void)
498
+{
499
+	_keyReport.keys[0] = 0;
500
+	_keyReport.keys[1] = 0;	
501
+	_keyReport.keys[2] = 0;
502
+	_keyReport.keys[3] = 0;	
503
+	_keyReport.keys[4] = 0;
504
+	_keyReport.keys[5] = 0;	
505
+	_keyReport.modifiers = 0;
506
+	sendReport(&_keyReport);
507
+}
508
+
509
+size_t Keyboard_::write(uint8_t c)
510
+{	
511
+	uint8_t p = press(c);  // Keydown
512
+	release(c);            // Keyup
513
+	return p;              // just return the result of press() since release() almost always returns 1
514
+}
515
+
516
+#endif
517
+
518
+#endif /* if defined(USBCON) */

+ 252
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial.cpp View File

@@ -0,0 +1,252 @@
1
+/*
2
+  HardwareSerial.cpp - Hardware serial library for Wiring
3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+  
19
+  Modified 23 November 2006 by David A. Mellis
20
+  Modified 28 September 2010 by Mark Sproul
21
+  Modified 14 August 2012 by Alarus
22
+  Modified 3 December 2013 by Matthijs Kooijman
23
+*/
24
+
25
+#include <stdlib.h>
26
+#include <stdio.h>
27
+#include <string.h>
28
+#include <inttypes.h>
29
+#include "Arduino.h"
30
+
31
+#include "HardwareSerial.h"
32
+#include "HardwareSerial_private.h"
33
+
34
+// this next line disables the entire HardwareSerial.cpp, 
35
+// this is so I can support Attiny series and any other chip without a uart
36
+#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)
37
+
38
+// SerialEvent functions are weak, so when the user doesn't define them,
39
+// the linker just sets their address to 0 (which is checked below).
40
+// The Serialx_available is just a wrapper around Serialx.available(),
41
+// but we can refer to it weakly so we don't pull in the entire
42
+// HardwareSerial instance if the user doesn't also refer to it.
43
+#if defined(HAVE_HWSERIAL0)
44
+  void serialEvent() __attribute__((weak));
45
+  bool Serial0_available() __attribute__((weak));
46
+#endif
47
+
48
+#if defined(HAVE_HWSERIAL1)
49
+  void serialEvent1() __attribute__((weak));
50
+  bool Serial1_available() __attribute__((weak));
51
+#endif
52
+
53
+#if defined(HAVE_HWSERIAL2)
54
+  void serialEvent2() __attribute__((weak));
55
+  bool Serial2_available() __attribute__((weak));
56
+#endif
57
+
58
+#if defined(HAVE_HWSERIAL3)
59
+  void serialEvent3() __attribute__((weak));
60
+  bool Serial3_available() __attribute__((weak));
61
+#endif
62
+
63
+void serialEventRun(void)
64
+{
65
+#if defined(HAVE_HWSERIAL0)
66
+  if (Serial0_available && serialEvent && Serial0_available()) serialEvent();
67
+#endif
68
+#if defined(HAVE_HWSERIAL1)
69
+  if (Serial1_available && serialEvent1 && Serial1_available()) serialEvent1();
70
+#endif
71
+#if defined(HAVE_HWSERIAL2)
72
+  if (Serial2_available && serialEvent2 && Serial2_available()) serialEvent2();
73
+#endif
74
+#if defined(HAVE_HWSERIAL3)
75
+  if (Serial3_available && serialEvent3 && Serial3_available()) serialEvent3();
76
+#endif
77
+}
78
+
79
+// Actual interrupt handlers //////////////////////////////////////////////////////////////
80
+
81
+void HardwareSerial::_tx_udr_empty_irq(void)
82
+{
83
+  // If interrupts are enabled, there must be more data in the output
84
+  // buffer. Send the next byte
85
+  unsigned char c = _tx_buffer[_tx_buffer_tail];
86
+  _tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_TX_BUFFER_SIZE;
87
+
88
+  *_udr = c;
89
+
90
+  // clear the TXC bit -- "can be cleared by writing a one to its bit
91
+  // location". This makes sure flush() won't return until the bytes
92
+  // actually got written
93
+  sbi(*_ucsra, TXC0);
94
+
95
+  if (_tx_buffer_head == _tx_buffer_tail) {
96
+    // Buffer empty, so disable interrupts
97
+    cbi(*_ucsrb, UDRIE0);
98
+  }
99
+}
100
+
101
+// Public Methods //////////////////////////////////////////////////////////////
102
+
103
+void HardwareSerial::begin(unsigned long baud, byte config)
104
+{
105
+  // Try u2x mode first
106
+  uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;
107
+  *_ucsra = 1 << U2X0;
108
+
109
+  // hardcoded exception for 57600 for compatibility with the bootloader
110
+  // shipped with the Duemilanove and previous boards and the firmware
111
+  // on the 8U2 on the Uno and Mega 2560. Also, The baud_setting cannot
112
+  // be > 4095, so switch back to non-u2x mode if the baud rate is too
113
+  // low.
114
+  if (((F_CPU == 16000000UL) && (baud == 57600)) || (baud_setting >4095))
115
+  {
116
+    *_ucsra = 0;
117
+    baud_setting = (F_CPU / 8 / baud - 1) / 2;
118
+  }
119
+
120
+  // assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register)
121
+  *_ubrrh = baud_setting >> 8;
122
+  *_ubrrl = baud_setting;
123
+
124
+  _written = false;
125
+
126
+  //set the data bits, parity, and stop bits
127
+#if defined(__AVR_ATmega8__)
128
+  config |= 0x80; // select UCSRC register (shared with UBRRH)
129
+#endif
130
+  *_ucsrc = config;
131
+  
132
+  sbi(*_ucsrb, RXEN0);
133
+  sbi(*_ucsrb, TXEN0);
134
+  sbi(*_ucsrb, RXCIE0);
135
+  cbi(*_ucsrb, UDRIE0);
136
+}
137
+
138
+void HardwareSerial::end()
139
+{
140
+  // wait for transmission of outgoing data
141
+  while (_tx_buffer_head != _tx_buffer_tail)
142
+    ;
143
+
144
+  cbi(*_ucsrb, RXEN0);
145
+  cbi(*_ucsrb, TXEN0);
146
+  cbi(*_ucsrb, RXCIE0);
147
+  cbi(*_ucsrb, UDRIE0);
148
+  
149
+  // clear any received data
150
+  _rx_buffer_head = _rx_buffer_tail;
151
+}
152
+
153
+int HardwareSerial::available(void)
154
+{
155
+  return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE;
156
+}
157
+
158
+int HardwareSerial::peek(void)
159
+{
160
+  if (_rx_buffer_head == _rx_buffer_tail) {
161
+    return -1;
162
+  } else {
163
+    return _rx_buffer[_rx_buffer_tail];
164
+  }
165
+}
166
+
167
+int HardwareSerial::read(void)
168
+{
169
+  // if the head isn't ahead of the tail, we don't have any characters
170
+  if (_rx_buffer_head == _rx_buffer_tail) {
171
+    return -1;
172
+  } else {
173
+    unsigned char c = _rx_buffer[_rx_buffer_tail];
174
+    _rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
175
+    return c;
176
+  }
177
+}
178
+
179
+int HardwareSerial::availableForWrite(void)
180
+{
181
+#if (SERIAL_TX_BUFFER_SIZE>256)
182
+  uint8_t oldSREG = SREG;
183
+  cli();
184
+#endif
185
+  tx_buffer_index_t head = _tx_buffer_head;
186
+  tx_buffer_index_t tail = _tx_buffer_tail;
187
+#if (SERIAL_TX_BUFFER_SIZE>256)
188
+  SREG = oldSREG;
189
+#endif
190
+  if (head >= tail) return SERIAL_TX_BUFFER_SIZE - 1 - head + tail;
191
+  return tail - head - 1;
192
+}
193
+
194
+void HardwareSerial::flush()
195
+{
196
+  // If we have never written a byte, no need to flush. This special
197
+  // case is needed since there is no way to force the TXC (transmit
198
+  // complete) bit to 1 during initialization
199
+  if (!_written)
200
+    return;
201
+
202
+  while (bit_is_set(*_ucsrb, UDRIE0) || bit_is_clear(*_ucsra, TXC0)) {
203
+    if (bit_is_clear(SREG, SREG_I) && bit_is_set(*_ucsrb, UDRIE0))
204
+	// Interrupts are globally disabled, but the DR empty
205
+	// interrupt should be enabled, so poll the DR empty flag to
206
+	// prevent deadlock
207
+	if (bit_is_set(*_ucsra, UDRE0))
208
+	  _tx_udr_empty_irq();
209
+  }
210
+  // If we get here, nothing is queued anymore (DRIE is disabled) and
211
+  // the hardware finished tranmission (TXC is set).
212
+}
213
+
214
+size_t HardwareSerial::write(uint8_t c)
215
+{
216
+  // If the buffer and the data register is empty, just write the byte
217
+  // to the data register and be done. This shortcut helps
218
+  // significantly improve the effective datarate at high (>
219
+  // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
220
+  if (_tx_buffer_head == _tx_buffer_tail && bit_is_set(*_ucsra, UDRE0)) {
221
+    *_udr = c;
222
+    sbi(*_ucsra, TXC0);
223
+    return 1;
224
+  }
225
+  tx_buffer_index_t i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE;
226
+	
227
+  // If the output buffer is full, there's nothing for it other than to 
228
+  // wait for the interrupt handler to empty it a bit
229
+  while (i == _tx_buffer_tail) {
230
+    if (bit_is_clear(SREG, SREG_I)) {
231
+      // Interrupts are disabled, so we'll have to poll the data
232
+      // register empty flag ourselves. If it is set, pretend an
233
+      // interrupt has happened and call the handler to free up
234
+      // space for us.
235
+      if(bit_is_set(*_ucsra, UDRE0))
236
+	_tx_udr_empty_irq();
237
+    } else {
238
+      // nop, the interrupt handler will free up space for us
239
+    }
240
+  }
241
+
242
+  _tx_buffer[_tx_buffer_head] = c;
243
+  _tx_buffer_head = i;
244
+	
245
+  sbi(*_ucsrb, UDRIE0);
246
+  _written = true;
247
+  
248
+  return 1;
249
+}
250
+
251
+
252
+#endif // whole file

+ 151
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial.h View File

@@ -0,0 +1,151 @@
1
+/*
2
+  HardwareSerial.h - Hardware serial library for Wiring
3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+
19
+  Modified 28 September 2010 by Mark Sproul
20
+  Modified 14 August 2012 by Alarus
21
+  Modified 3 December 2013 by Matthijs Kooijman
22
+*/
23
+
24
+#ifndef HardwareSerial_h
25
+#define HardwareSerial_h
26
+
27
+#include <inttypes.h>
28
+
29
+#include "Stream.h"
30
+
31
+// Define constants and variables for buffering incoming serial data.  We're
32
+// using a ring buffer (I think), in which head is the index of the location
33
+// to which to write the next incoming character and tail is the index of the
34
+// location from which to read.
35
+// NOTE: a "power of 2" buffer size is reccomended to dramatically
36
+//       optimize all the modulo operations for ring buffers.
37
+#if !(defined(SERIAL_TX_BUFFER_SIZE) && defined(SERIAL_RX_BUFFER_SIZE))
38
+#if (RAMEND < 1000)
39
+#define SERIAL_TX_BUFFER_SIZE 16
40
+#define SERIAL_RX_BUFFER_SIZE 16
41
+#else
42
+#define SERIAL_TX_BUFFER_SIZE 64
43
+#define SERIAL_RX_BUFFER_SIZE 64
44
+#endif
45
+#endif
46
+#if (SERIAL_TX_BUFFER_SIZE>256)
47
+typedef uint16_t tx_buffer_index_t;
48
+#else
49
+typedef uint8_t tx_buffer_index_t;
50
+#endif
51
+#if  (SERIAL_RX_BUFFER_SIZE>256)
52
+typedef uint16_t rx_buffer_index_t;
53
+#else
54
+typedef uint8_t rx_buffer_index_t;
55
+#endif
56
+
57
+// Define config for Serial.begin(baud, config);
58
+#define SERIAL_5N1 0x00
59
+#define SERIAL_6N1 0x02
60
+#define SERIAL_7N1 0x04
61
+#define SERIAL_8N1 0x06
62
+#define SERIAL_5N2 0x08
63
+#define SERIAL_6N2 0x0A
64
+#define SERIAL_7N2 0x0C
65
+#define SERIAL_8N2 0x0E
66
+#define SERIAL_5E1 0x20
67
+#define SERIAL_6E1 0x22
68
+#define SERIAL_7E1 0x24
69
+#define SERIAL_8E1 0x26
70
+#define SERIAL_5E2 0x28
71
+#define SERIAL_6E2 0x2A
72
+#define SERIAL_7E2 0x2C
73
+#define SERIAL_8E2 0x2E
74
+#define SERIAL_5O1 0x30
75
+#define SERIAL_6O1 0x32
76
+#define SERIAL_7O1 0x34
77
+#define SERIAL_8O1 0x36
78
+#define SERIAL_5O2 0x38
79
+#define SERIAL_6O2 0x3A
80
+#define SERIAL_7O2 0x3C
81
+#define SERIAL_8O2 0x3E
82
+
83
+class HardwareSerial : public Stream
84
+{
85
+  protected:
86
+    volatile uint8_t * const _ubrrh;
87
+    volatile uint8_t * const _ubrrl;
88
+    volatile uint8_t * const _ucsra;
89
+    volatile uint8_t * const _ucsrb;
90
+    volatile uint8_t * const _ucsrc;
91
+    volatile uint8_t * const _udr;
92
+    // Has any byte been written to the UART since begin()
93
+    bool _written;
94
+
95
+    volatile rx_buffer_index_t _rx_buffer_head;
96
+    volatile rx_buffer_index_t _rx_buffer_tail;
97
+    volatile tx_buffer_index_t _tx_buffer_head;
98
+    volatile tx_buffer_index_t _tx_buffer_tail;
99
+
100
+    // Don't put any members after these buffers, since only the first
101
+    // 32 bytes of this struct can be accessed quickly using the ldd
102
+    // instruction.
103
+    unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE];
104
+    unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE];
105
+
106
+  public:
107
+    inline HardwareSerial(
108
+      volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
109
+      volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
110
+      volatile uint8_t *ucsrc, volatile uint8_t *udr);
111
+    void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
112
+    void begin(unsigned long, uint8_t);
113
+    void end();
114
+    virtual int available(void);
115
+    virtual int peek(void);
116
+    virtual int read(void);
117
+    int availableForWrite(void);
118
+    virtual void flush(void);
119
+    virtual size_t write(uint8_t);
120
+    inline size_t write(unsigned long n) { return write((uint8_t)n); }
121
+    inline size_t write(long n) { return write((uint8_t)n); }
122
+    inline size_t write(unsigned int n) { return write((uint8_t)n); }
123
+    inline size_t write(int n) { return write((uint8_t)n); }
124
+    using Print::write; // pull in write(str) and write(buf, size) from Print
125
+    operator bool() { return true; }
126
+
127
+    // Interrupt handlers - Not intended to be called externally
128
+    inline void _rx_complete_irq(void);
129
+    void _tx_udr_empty_irq(void);
130
+};
131
+
132
+#if defined(UBRRH) || defined(UBRR0H)
133
+  extern HardwareSerial Serial;
134
+  #define HAVE_HWSERIAL0
135
+#endif
136
+#if defined(UBRR1H)
137
+  extern HardwareSerial Serial1;
138
+  #define HAVE_HWSERIAL1
139
+#endif
140
+#if defined(UBRR2H)
141
+  extern HardwareSerial Serial2;
142
+  #define HAVE_HWSERIAL2
143
+#endif
144
+#if defined(UBRR3H)
145
+  extern HardwareSerial Serial3;
146
+  #define HAVE_HWSERIAL3
147
+#endif
148
+
149
+extern void serialEventRun(void) __attribute__((weak));
150
+
151
+#endif

+ 79
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial0.cpp View File

@@ -0,0 +1,79 @@
1
+/*
2
+  HardwareSerial0.cpp - Hardware serial library for Wiring
3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+
19
+  Modified 23 November 2006 by David A. Mellis
20
+  Modified 28 September 2010 by Mark Sproul
21
+  Modified 14 August 2012 by Alarus
22
+  Modified 3 December 2013 by Matthijs Kooijman
23
+*/
24
+
25
+#include "Arduino.h"
26
+#include "HardwareSerial.h"
27
+#include "HardwareSerial_private.h"
28
+
29
+// Each HardwareSerial is defined in its own file, sine the linker pulls
30
+// in the entire file when any element inside is used. --gc-sections can
31
+// additionally cause unused symbols to be dropped, but ISRs have the
32
+// "used" attribute so are never dropped and they keep the
33
+// HardwareSerial instance in as well. Putting each instance in its own
34
+// file prevents the linker from pulling in any unused instances in the
35
+// first place.
36
+
37
+#if defined(HAVE_HWSERIAL0)
38
+
39
+#if defined(USART_RX_vect)
40
+  ISR(USART_RX_vect)
41
+#elif defined(USART0_RX_vect)
42
+  ISR(USART0_RX_vect)
43
+#elif defined(USART_RXC_vect)
44
+  ISR(USART_RXC_vect) // ATmega8
45
+#else
46
+  #error "Don't know what the Data Received vector is called for Serial"
47
+#endif
48
+  {
49
+    Serial._rx_complete_irq();
50
+  }
51
+
52
+#if defined(UART0_UDRE_vect)
53
+ISR(UART0_UDRE_vect)
54
+#elif defined(UART_UDRE_vect)
55
+ISR(UART_UDRE_vect)
56
+#elif defined(USART0_UDRE_vect)
57
+ISR(USART0_UDRE_vect)
58
+#elif defined(USART_UDRE_vect)
59
+ISR(USART_UDRE_vect)
60
+#else
61
+  #error "Don't know what the Data Register Empty vector is called for Serial"
62
+#endif
63
+{
64
+  Serial._tx_udr_empty_irq();
65
+}
66
+
67
+#if defined(UBRRH) && defined(UBRRL)
68
+  HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
69
+#else
70
+  HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
71
+#endif
72
+
73
+// Function that can be weakly referenced by serialEventRun to prevent
74
+// pulling in this file if it's not otherwise used.
75
+bool Serial0_available() {
76
+  return Serial.available();
77
+}
78
+
79
+#endif // HAVE_HWSERIAL0

+ 69
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial1.cpp View File

@@ -0,0 +1,69 @@
1
+/*
2
+  HardwareSerial1.cpp - Hardware serial library for Wiring
3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+
19
+  Modified 23 November 2006 by David A. Mellis
20
+  Modified 28 September 2010 by Mark Sproul
21
+  Modified 14 August 2012 by Alarus
22
+  Modified 3 December 2013 by Matthijs Kooijman
23
+*/
24
+
25
+#include "Arduino.h"
26
+#include "HardwareSerial.h"
27
+#include "HardwareSerial_private.h"
28
+
29
+// Each HardwareSerial is defined in its own file, sine the linker pulls
30
+// in the entire file when any element inside is used. --gc-sections can
31
+// additionally cause unused symbols to be dropped, but ISRs have the
32
+// "used" attribute so are never dropped and they keep the
33
+// HardwareSerial instance in as well. Putting each instance in its own
34
+// file prevents the linker from pulling in any unused instances in the
35
+// first place.
36
+
37
+#if defined(HAVE_HWSERIAL1)
38
+
39
+#if defined(UART1_RX_vect)
40
+ISR(UART1_RX_vect)
41
+#elif defined(USART1_RX_vect)
42
+ISR(USART1_RX_vect)
43
+#else
44
+#error "Don't know what the Data Register Empty vector is called for Serial1"
45
+#endif
46
+{
47
+  Serial1._rx_complete_irq();
48
+}
49
+
50
+#if defined(UART1_UDRE_vect)
51
+ISR(UART1_UDRE_vect)
52
+#elif defined(USART1_UDRE_vect)
53
+ISR(USART1_UDRE_vect)
54
+#else
55
+#error "Don't know what the Data Register Empty vector is called for Serial1"
56
+#endif
57
+{
58
+  Serial1._tx_udr_empty_irq();
59
+}
60
+
61
+HardwareSerial Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
62
+
63
+// Function that can be weakly referenced by serialEventRun to prevent
64
+// pulling in this file if it's not otherwise used.
65
+bool Serial1_available() {
66
+  return Serial1.available();
67
+}
68
+
69
+#endif // HAVE_HWSERIAL1

+ 57
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial2.cpp View File

@@ -0,0 +1,57 @@
1
+/*
2
+  HardwareSerial2.cpp - Hardware serial library for Wiring
3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+
19
+  Modified 23 November 2006 by David A. Mellis
20
+  Modified 28 September 2010 by Mark Sproul
21
+  Modified 14 August 2012 by Alarus
22
+  Modified 3 December 2013 by Matthijs Kooijman
23
+*/
24
+
25
+#include "Arduino.h"
26
+#include "HardwareSerial.h"
27
+#include "HardwareSerial_private.h"
28
+
29
+// Each HardwareSerial is defined in its own file, sine the linker pulls
30
+// in the entire file when any element inside is used. --gc-sections can
31
+// additionally cause unused symbols to be dropped, but ISRs have the
32
+// "used" attribute so are never dropped and they keep the
33
+// HardwareSerial instance in as well. Putting each instance in its own
34
+// file prevents the linker from pulling in any unused instances in the
35
+// first place.
36
+
37
+#if defined(HAVE_HWSERIAL2)
38
+
39
+ISR(USART2_RX_vect)
40
+{
41
+  Serial2._rx_complete_irq();
42
+}
43
+
44
+ISR(USART2_UDRE_vect)
45
+{
46
+  Serial2._tx_udr_empty_irq();
47
+}
48
+
49
+HardwareSerial Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2);
50
+
51
+// Function that can be weakly referenced by serialEventRun to prevent
52
+// pulling in this file if it's not otherwise used.
53
+bool Serial2_available() {
54
+  return Serial2.available();
55
+}
56
+
57
+#endif // HAVE_HWSERIAL2

+ 57
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial3.cpp View File

@@ -0,0 +1,57 @@
1
+/*
2
+  HardwareSerial3.cpp - Hardware serial library for Wiring
3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+
19
+  Modified 23 November 2006 by David A. Mellis
20
+  Modified 28 September 2010 by Mark Sproul
21
+  Modified 14 August 2012 by Alarus
22
+  Modified 3 December 2013 by Matthijs Kooijman
23
+*/
24
+
25
+#include "Arduino.h"
26
+#include "HardwareSerial.h"
27
+#include "HardwareSerial_private.h"
28
+
29
+// Each HardwareSerial is defined in its own file, sine the linker pulls
30
+// in the entire file when any element inside is used. --gc-sections can
31
+// additionally cause unused symbols to be dropped, but ISRs have the
32
+// "used" attribute so are never dropped and they keep the
33
+// HardwareSerial instance in as well. Putting each instance in its own
34
+// file prevents the linker from pulling in any unused instances in the
35
+// first place.
36
+
37
+#if defined(HAVE_HWSERIAL3)
38
+
39
+ISR(USART3_RX_vect)
40
+{
41
+  Serial3._rx_complete_irq();
42
+}
43
+
44
+ISR(USART3_UDRE_vect)
45
+{
46
+  Serial3._tx_udr_empty_irq();
47
+}
48
+
49
+HardwareSerial Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3);
50
+
51
+// Function that can be weakly referenced by serialEventRun to prevent
52
+// pulling in this file if it's not otherwise used.
53
+bool Serial3_available() {
54
+  return Serial3.available();
55
+}
56
+
57
+#endif // HAVE_HWSERIAL3

+ 123
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/HardwareSerial_private.h View File

@@ -0,0 +1,123 @@
1
+/*
2
+  HardwareSerial_private.h - Hardware serial library for Wiring
3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+
19
+  Modified 23 November 2006 by David A. Mellis
20
+  Modified 28 September 2010 by Mark Sproul
21
+  Modified 14 August 2012 by Alarus
22
+*/
23
+
24
+#include "wiring_private.h"
25
+
26
+// this next line disables the entire HardwareSerial.cpp, 
27
+// this is so I can support Attiny series and any other chip without a uart
28
+#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)
29
+
30
+// Ensure that the various bit positions we use are available with a 0
31
+// postfix, so we can always use the values for UART0 for all UARTs. The
32
+// alternative, passing the various values for each UART to the
33
+// HardwareSerial constructor also works, but makes the code bigger and
34
+// slower.
35
+#if !defined(TXC0)
36
+#if defined(TXC)
37
+// Some chips like ATmega8 don't have UPE, only PE. The other bits are
38
+// named as expected.
39
+#if !defined(UPE) && defined(PE)
40
+#define UPE PE
41
+#endif
42
+// On ATmega8, the uart and its bits are not numbered, so there is no TXC0 etc.
43
+#define TXC0 TXC
44
+#define RXEN0 RXEN
45
+#define TXEN0 TXEN
46
+#define RXCIE0 RXCIE
47
+#define UDRIE0 UDRIE
48
+#define U2X0 U2X
49
+#define UPE0 UPE
50
+#define UDRE0 UDRE
51
+#elif defined(TXC1)
52
+// Some devices have uart1 but no uart0
53
+#define TXC0 TXC1
54
+#define RXEN0 RXEN1
55
+#define TXEN0 TXEN1
56
+#define RXCIE0 RXCIE1
57
+#define UDRIE0 UDRIE1
58
+#define U2X0 U2X1
59
+#define UPE0 UPE1
60
+#define UDRE0 UDRE1
61
+#else
62
+#error No UART found in HardwareSerial.cpp
63
+#endif
64
+#endif // !defined TXC0
65
+
66
+// Check at compiletime that it is really ok to use the bit positions of
67
+// UART0 for the other UARTs as well, in case these values ever get
68
+// changed for future hardware.
69
+#if defined(TXC1) && (TXC1 != TXC0 || RXEN1 != RXEN0 || RXCIE1 != RXCIE0 || \
70
+		      UDRIE1 != UDRIE0 || U2X1 != U2X0 || UPE1 != UPE0 || \
71
+		      UDRE1 != UDRE0)
72
+#error "Not all bit positions for UART1 are the same as for UART0"
73
+#endif
74
+#if defined(TXC2) && (TXC2 != TXC0 || RXEN2 != RXEN0 || RXCIE2 != RXCIE0 || \
75
+		      UDRIE2 != UDRIE0 || U2X2 != U2X0 || UPE2 != UPE0 || \
76
+		      UDRE2 != UDRE0)
77
+#error "Not all bit positions for UART2 are the same as for UART0"
78
+#endif
79
+#if defined(TXC3) && (TXC3 != TXC0 || RXEN3 != RXEN0 || RXCIE3 != RXCIE0 || \
80
+		      UDRIE3 != UDRIE0 || U3X3 != U3X0 || UPE3 != UPE0 || \
81
+		      UDRE3 != UDRE0)
82
+#error "Not all bit positions for UART3 are the same as for UART0"
83
+#endif
84
+
85
+// Constructors ////////////////////////////////////////////////////////////////
86
+
87
+HardwareSerial::HardwareSerial(
88
+  volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
89
+  volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
90
+  volatile uint8_t *ucsrc, volatile uint8_t *udr) :
91
+    _ubrrh(ubrrh), _ubrrl(ubrrl),
92
+    _ucsra(ucsra), _ucsrb(ucsrb), _ucsrc(ucsrc),
93
+    _udr(udr),
94
+    _rx_buffer_head(0), _rx_buffer_tail(0),
95
+    _tx_buffer_head(0), _tx_buffer_tail(0)
96
+{
97
+}
98
+
99
+// Actual interrupt handlers //////////////////////////////////////////////////////////////
100
+
101
+void HardwareSerial::_rx_complete_irq(void)
102
+{
103
+  if (bit_is_clear(*_ucsra, UPE0)) {
104
+    // No Parity error, read byte and store it in the buffer if there is
105
+    // room
106
+    unsigned char c = *_udr;
107
+    rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE;
108
+
109
+    // if we should be storing the received character into the location
110
+    // just before the tail (meaning that the head would advance to the
111
+    // current location of the tail), we're about to overflow the buffer
112
+    // and so we don't write the character or advance the head.
113
+    if (i != _rx_buffer_tail) {
114
+      _rx_buffer[_rx_buffer_head] = c;
115
+      _rx_buffer_head = i;
116
+    }
117
+  } else {
118
+    // Parity error, read byte but discard it
119
+    *_udr;
120
+  };
121
+}
122
+
123
+#endif // whole file

+ 74
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/IPAddress.cpp View File

@@ -0,0 +1,74 @@
1
+/*
2
+  IPAddress.cpp - Base class that provides IPAddress
3
+  Copyright (c) 2011 Adrian McEwen.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#include <Arduino.h>
21
+#include <IPAddress.h>
22
+
23
+IPAddress::IPAddress()
24
+{
25
+    _address.dword = 0;
26
+}
27
+
28
+IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
29
+{
30
+    _address.bytes[0] = first_octet;
31
+    _address.bytes[1] = second_octet;
32
+    _address.bytes[2] = third_octet;
33
+    _address.bytes[3] = fourth_octet;
34
+}
35
+
36
+IPAddress::IPAddress(uint32_t address)
37
+{
38
+    _address.dword = address;
39
+}
40
+
41
+IPAddress::IPAddress(const uint8_t *address)
42
+{
43
+    memcpy(_address.bytes, address, sizeof(_address.bytes));
44
+}
45
+
46
+IPAddress& IPAddress::operator=(const uint8_t *address)
47
+{
48
+    memcpy(_address.bytes, address, sizeof(_address.bytes));
49
+    return *this;
50
+}
51
+
52
+IPAddress& IPAddress::operator=(uint32_t address)
53
+{
54
+    _address.dword = address;
55
+    return *this;
56
+}
57
+
58
+bool IPAddress::operator==(const uint8_t* addr) const
59
+{
60
+    return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
61
+}
62
+
63
+size_t IPAddress::printTo(Print& p) const
64
+{
65
+    size_t n = 0;
66
+    for (int i =0; i < 3; i++)
67
+    {
68
+        n += p.print(_address.bytes[i], DEC);
69
+        n += p.print('.');
70
+    }
71
+    n += p.print(_address.bytes[3], DEC);
72
+    return n;
73
+}
74
+

+ 75
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/IPAddress.h View File

@@ -0,0 +1,75 @@
1
+/*
2
+  IPAddress.h - Base class that provides IPAddress
3
+  Copyright (c) 2011 Adrian McEwen.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef IPAddress_h
21
+#define IPAddress_h
22
+
23
+#include <stdint.h>
24
+#include <Printable.h>
25
+
26
+// A class to make it easier to handle and pass around IP addresses
27
+
28
+class IPAddress : public Printable {
29
+private:
30
+    union {
31
+	uint8_t bytes[4];  // IPv4 address
32
+	uint32_t dword;
33
+    } _address;
34
+
35
+    // Access the raw byte array containing the address.  Because this returns a pointer
36
+    // to the internal structure rather than a copy of the address this function should only
37
+    // be used when you know that the usage of the returned uint8_t* will be transient and not
38
+    // stored.
39
+    uint8_t* raw_address() { return _address.bytes; };
40
+
41
+public:
42
+    // Constructors
43
+    IPAddress();
44
+    IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
45
+    IPAddress(uint32_t address);
46
+    IPAddress(const uint8_t *address);
47
+
48
+    // Overloaded cast operator to allow IPAddress objects to be used where a pointer
49
+    // to a four-byte uint8_t array is expected
50
+    operator uint32_t() const { return _address.dword; };
51
+    bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
52
+    bool operator==(const uint8_t* addr) const;
53
+
54
+    // Overloaded index operator to allow getting and setting individual octets of the address
55
+    uint8_t operator[](int index) const { return _address.bytes[index]; };
56
+    uint8_t& operator[](int index) { return _address.bytes[index]; };
57
+
58
+    // Overloaded copy operators to allow initialisation of IPAddress objects from other types
59
+    IPAddress& operator=(const uint8_t *address);
60
+    IPAddress& operator=(uint32_t address);
61
+
62
+    virtual size_t printTo(Print& p) const;
63
+
64
+    friend class EthernetClass;
65
+    friend class UDP;
66
+    friend class Client;
67
+    friend class Server;
68
+    friend class DhcpClass;
69
+    friend class DNSClient;
70
+};
71
+
72
+const IPAddress INADDR_NONE(0,0,0,0);
73
+
74
+
75
+#endif

+ 264
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Print.cpp View File

@@ -0,0 +1,264 @@
1
+/*
2
+ Print.cpp - Base class that provides print() and println()
3
+ Copyright (c) 2008 David A. Mellis.  All right reserved.
4
+ 
5
+ This library is free software; you can redistribute it and/or
6
+ modify it under the terms of the GNU Lesser General Public
7
+ License as published by the Free Software Foundation; either
8
+ version 2.1 of the License, or (at your option) any later version.
9
+ 
10
+ This library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ Lesser General Public License for more details.
14
+ 
15
+ You should have received a copy of the GNU Lesser General Public
16
+ License along with this library; if not, write to the Free Software
17
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+ 
19
+ Modified 23 November 2006 by David A. Mellis
20
+ */
21
+
22
+#include <stdlib.h>
23
+#include <stdio.h>
24
+#include <string.h>
25
+#include <math.h>
26
+#include "Arduino.h"
27
+
28
+#include "Print.h"
29
+
30
+// Public Methods //////////////////////////////////////////////////////////////
31
+
32
+/* default implementation: may be overridden */
33
+size_t Print::write(const uint8_t *buffer, size_t size)
34
+{
35
+  size_t n = 0;
36
+  while (size--) {
37
+    n += write(*buffer++);
38
+  }
39
+  return n;
40
+}
41
+
42
+size_t Print::print(const __FlashStringHelper *ifsh)
43
+{
44
+  PGM_P p = reinterpret_cast<PGM_P>(ifsh);
45
+  size_t n = 0;
46
+  while (1) {
47
+    unsigned char c = pgm_read_byte(p++);
48
+    if (c == 0) break;
49
+    n += write(c);
50
+  }
51
+  return n;
52
+}
53
+
54
+size_t Print::print(const String &s)
55
+{
56
+  return write(s.c_str(), s.length());
57
+}
58
+
59
+size_t Print::print(const char str[])
60
+{
61
+  return write(str);
62
+}
63
+
64
+size_t Print::print(char c)
65
+{
66
+  return write(c);
67
+}
68
+
69
+size_t Print::print(unsigned char b, int base)
70
+{
71
+  return print((unsigned long) b, base);
72
+}
73
+
74
+size_t Print::print(int n, int base)
75
+{
76
+  return print((long) n, base);
77
+}
78
+
79
+size_t Print::print(unsigned int n, int base)
80
+{
81
+  return print((unsigned long) n, base);
82
+}
83
+
84
+size_t Print::print(long n, int base)
85
+{
86
+  if (base == 0) {
87
+    return write(n);
88
+  } else if (base == 10) {
89
+    if (n < 0) {
90
+      int t = print('-');
91
+      n = -n;
92
+      return printNumber(n, 10) + t;
93
+    }
94
+    return printNumber(n, 10);
95
+  } else {
96
+    return printNumber(n, base);
97
+  }
98
+}
99
+
100
+size_t Print::print(unsigned long n, int base)
101
+{
102
+  if (base == 0) return write(n);
103
+  else return printNumber(n, base);
104
+}
105
+
106
+size_t Print::print(double n, int digits)
107
+{
108
+  return printFloat(n, digits);
109
+}
110
+
111
+size_t Print::println(const __FlashStringHelper *ifsh)
112
+{
113
+  size_t n = print(ifsh);
114
+  n += println();
115
+  return n;
116
+}
117
+
118
+size_t Print::print(const Printable& x)
119
+{
120
+  return x.printTo(*this);
121
+}
122
+
123
+size_t Print::println(void)
124
+{
125
+  size_t n = print('\r');
126
+  n += print('\n');
127
+  return n;
128
+}
129
+
130
+size_t Print::println(const String &s)
131
+{
132
+  size_t n = print(s);
133
+  n += println();
134
+  return n;
135
+}
136
+
137
+size_t Print::println(const char c[])
138
+{
139
+  size_t n = print(c);
140
+  n += println();
141
+  return n;
142
+}
143
+
144
+size_t Print::println(char c)
145
+{
146
+  size_t n = print(c);
147
+  n += println();
148
+  return n;
149
+}
150
+
151
+size_t Print::println(unsigned char b, int base)
152
+{
153
+  size_t n = print(b, base);
154
+  n += println();
155
+  return n;
156
+}
157
+
158
+size_t Print::println(int num, int base)
159
+{
160
+  size_t n = print(num, base);
161
+  n += println();
162
+  return n;
163
+}
164
+
165
+size_t Print::println(unsigned int num, int base)
166
+{
167
+  size_t n = print(num, base);
168
+  n += println();
169
+  return n;
170
+}
171
+
172
+size_t Print::println(long num, int base)
173
+{
174
+  size_t n = print(num, base);
175
+  n += println();
176
+  return n;
177
+}
178
+
179
+size_t Print::println(unsigned long num, int base)
180
+{
181
+  size_t n = print(num, base);
182
+  n += println();
183
+  return n;
184
+}
185
+
186
+size_t Print::println(double num, int digits)
187
+{
188
+  size_t n = print(num, digits);
189
+  n += println();
190
+  return n;
191
+}
192
+
193
+size_t Print::println(const Printable& x)
194
+{
195
+  size_t n = print(x);
196
+  n += println();
197
+  return n;
198
+}
199
+
200
+// Private Methods /////////////////////////////////////////////////////////////
201
+
202
+size_t Print::printNumber(unsigned long n, uint8_t base) {
203
+  char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
204
+  char *str = &buf[sizeof(buf) - 1];
205
+
206
+  *str = '\0';
207
+
208
+  // prevent crash if called with base == 1
209
+  if (base < 2) base = 10;
210
+
211
+  do {
212
+    unsigned long m = n;
213
+    n /= base;
214
+    char c = m - base * n;
215
+    *--str = c < 10 ? c + '0' : c + 'A' - 10;
216
+  } while(n);
217
+
218
+  return write(str);
219
+}
220
+
221
+size_t Print::printFloat(double number, uint8_t digits) 
222
+{ 
223
+  size_t n = 0;
224
+  
225
+  if (isnan(number)) return print("nan");
226
+  if (isinf(number)) return print("inf");
227
+  if (number > 4294967040.0) return print ("ovf");  // constant determined empirically
228
+  if (number <-4294967040.0) return print ("ovf");  // constant determined empirically
229
+  
230
+  // Handle negative numbers
231
+  if (number < 0.0)
232
+  {
233
+     n += print('-');
234
+     number = -number;
235
+  }
236
+
237
+  // Round correctly so that print(1.999, 2) prints as "2.00"
238
+  double rounding = 0.5;
239
+  for (uint8_t i=0; i<digits; ++i)
240
+    rounding /= 10.0;
241
+  
242
+  number += rounding;
243
+
244
+  // Extract the integer part of the number and print it
245
+  unsigned long int_part = (unsigned long)number;
246
+  double remainder = number - (double)int_part;
247
+  n += print(int_part);
248
+
249
+  // Print the decimal point, but only if there are digits beyond
250
+  if (digits > 0) {
251
+    n += print("."); 
252
+  }
253
+
254
+  // Extract digits from the remainder one at a time
255
+  while (digits-- > 0)
256
+  {
257
+    remainder *= 10.0;
258
+    int toPrint = int(remainder);
259
+    n += print(toPrint);
260
+    remainder -= toPrint; 
261
+  } 
262
+  
263
+  return n;
264
+}

+ 84
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Print.h View File

@@ -0,0 +1,84 @@
1
+/*
2
+  Print.h - Base class that provides print() and println()
3
+  Copyright (c) 2008 David A. Mellis.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef Print_h
21
+#define Print_h
22
+
23
+#include <inttypes.h>
24
+#include <stdio.h> // for size_t
25
+
26
+#include "WString.h"
27
+#include "Printable.h"
28
+
29
+#define DEC 10
30
+#define HEX 16
31
+#define OCT 8
32
+#define BIN 2
33
+
34
+class Print
35
+{
36
+  private:
37
+    int write_error;
38
+    size_t printNumber(unsigned long, uint8_t);
39
+    size_t printFloat(double, uint8_t);
40
+  protected:
41
+    void setWriteError(int err = 1) { write_error = err; }
42
+  public:
43
+    Print() : write_error(0) {}
44
+  
45
+    int getWriteError() { return write_error; }
46
+    void clearWriteError() { setWriteError(0); }
47
+  
48
+    virtual size_t write(uint8_t) = 0;
49
+    size_t write(const char *str) {
50
+      if (str == NULL) return 0;
51
+      return write((const uint8_t *)str, strlen(str));
52
+    }
53
+    virtual size_t write(const uint8_t *buffer, size_t size);
54
+    size_t write(const char *buffer, size_t size) {
55
+      return write((const uint8_t *)buffer, size);
56
+    }
57
+    
58
+    size_t print(const __FlashStringHelper *);
59
+    size_t print(const String &);
60
+    size_t print(const char[]);
61
+    size_t print(char);
62
+    size_t print(unsigned char, int = DEC);
63
+    size_t print(int, int = DEC);
64
+    size_t print(unsigned int, int = DEC);
65
+    size_t print(long, int = DEC);
66
+    size_t print(unsigned long, int = DEC);
67
+    size_t print(double, int = 2);
68
+    size_t print(const Printable&);
69
+
70
+    size_t println(const __FlashStringHelper *);
71
+    size_t println(const String &s);
72
+    size_t println(const char[]);
73
+    size_t println(char);
74
+    size_t println(unsigned char, int = DEC);
75
+    size_t println(int, int = DEC);
76
+    size_t println(unsigned int, int = DEC);
77
+    size_t println(long, int = DEC);
78
+    size_t println(unsigned long, int = DEC);
79
+    size_t println(double, int = 2);
80
+    size_t println(const Printable&);
81
+    size_t println(void);
82
+};
83
+
84
+#endif

+ 40
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Printable.h View File

@@ -0,0 +1,40 @@
1
+/*
2
+  Printable.h - Interface class that allows printing of complex types
3
+  Copyright (c) 2011 Adrian McEwen.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef Printable_h
21
+#define Printable_h
22
+
23
+#include <stdlib.h>
24
+
25
+class Print;
26
+
27
+/** The Printable class provides a way for new classes to allow themselves to be printed.
28
+    By deriving from Printable and implementing the printTo method, it will then be possible
29
+    for users to print out instances of this class by passing them into the usual
30
+    Print::print and Print::println methods.
31
+*/
32
+
33
+class Printable
34
+{
35
+  public:
36
+    virtual size_t printTo(Print& p) const = 0;
37
+};
38
+
39
+#endif
40
+

+ 30
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Server.h View File

@@ -0,0 +1,30 @@
1
+/*
2
+  Server.h - Base class that provides Server
3
+  Copyright (c) 2011 Adrian McEwen.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef server_h
21
+#define server_h
22
+
23
+#include "Print.h"
24
+
25
+class Server : public Print {
26
+public:
27
+  virtual void begin() =0;
28
+};
29
+
30
+#endif

+ 317
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Stream.cpp View File

@@ -0,0 +1,317 @@
1
+/*
2
+ Stream.cpp - adds parsing methods to Stream class
3
+ Copyright (c) 2008 David A. Mellis.  All right reserved.
4
+
5
+ This library is free software; you can redistribute it and/or
6
+ modify it under the terms of the GNU Lesser General Public
7
+ License as published by the Free Software Foundation; either
8
+ version 2.1 of the License, or (at your option) any later version.
9
+
10
+ This library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public
16
+ License along with this library; if not, write to the Free Software
17
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+
19
+ Created July 2011
20
+ parsing functions based on TextFinder library by Michael Margolis
21
+
22
+ findMulti/findUntil routines written by Jim Leonard/Xuth
23
+ */
24
+
25
+#include "Arduino.h"
26
+#include "Stream.h"
27
+
28
+#define PARSE_TIMEOUT 1000  // default number of milli-seconds to wait
29
+#define NO_SKIP_CHAR  1  // a magic char not found in a valid ASCII numeric field
30
+
31
+// private method to read stream with timeout
32
+int Stream::timedRead()
33
+{
34
+  int c;
35
+  _startMillis = millis();
36
+  do {
37
+    c = read();
38
+    if (c >= 0) return c;
39
+  } while(millis() - _startMillis < _timeout);
40
+  return -1;     // -1 indicates timeout
41
+}
42
+
43
+// private method to peek stream with timeout
44
+int Stream::timedPeek()
45
+{
46
+  int c;
47
+  _startMillis = millis();
48
+  do {
49
+    c = peek();
50
+    if (c >= 0) return c;
51
+  } while(millis() - _startMillis < _timeout);
52
+  return -1;     // -1 indicates timeout
53
+}
54
+
55
+// returns peek of the next digit in the stream or -1 if timeout
56
+// discards non-numeric characters
57
+int Stream::peekNextDigit()
58
+{
59
+  int c;
60
+  while (1) {
61
+    c = timedPeek();
62
+    if (c < 0) return c;  // timeout
63
+    if (c == '-') return c;
64
+    if (c >= '0' && c <= '9') return c;
65
+    read();  // discard non-numeric
66
+  }
67
+}
68
+
69
+// Public Methods
70
+//////////////////////////////////////////////////////////////
71
+
72
+void Stream::setTimeout(unsigned long timeout)  // sets the maximum number of milliseconds to wait
73
+{
74
+  _timeout = timeout;
75
+}
76
+
77
+ // find returns true if the target string is found
78
+bool  Stream::find(char *target)
79
+{
80
+  return findUntil(target, strlen(target), NULL, 0);
81
+}
82
+
83
+// reads data from the stream until the target string of given length is found
84
+// returns true if target string is found, false if timed out
85
+bool Stream::find(char *target, size_t length)
86
+{
87
+  return findUntil(target, length, NULL, 0);
88
+}
89
+
90
+// as find but search ends if the terminator string is found
91
+bool  Stream::findUntil(char *target, char *terminator)
92
+{
93
+  return findUntil(target, strlen(target), terminator, strlen(terminator));
94
+}
95
+
96
+// reads data from the stream until the target string of the given length is found
97
+// search terminated if the terminator string is found
98
+// returns true if target string is found, false if terminated or timed out
99
+bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
100
+{
101
+  if (terminator == NULL) {
102
+    MultiTarget t[1] = {{target, targetLen, 0}};
103
+    return findMulti(t, 1) == 0 ? true : false;
104
+  } else {
105
+    MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}};
106
+    return findMulti(t, 2) == 0 ? true : false;
107
+  }
108
+}
109
+
110
+
111
+// returns the first valid (long) integer value from the current position.
112
+// initial characters that are not digits (or the minus sign) are skipped
113
+// function is terminated by the first character that is not a digit.
114
+long Stream::parseInt()
115
+{
116
+  return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
117
+}
118
+
119
+// as above but a given skipChar is ignored
120
+// this allows format characters (typically commas) in values to be ignored
121
+long Stream::parseInt(char skipChar)
122
+{
123
+  bool isNegative = false;
124
+  long value = 0;
125
+  int c;
126
+
127
+  c = peekNextDigit();
128
+  // ignore non numeric leading characters
129
+  if(c < 0)
130
+    return 0; // zero returned if timeout
131
+
132
+  do{
133
+    if(c == skipChar)
134
+      ; // ignore this charactor
135
+    else if(c == '-')
136
+      isNegative = true;
137
+    else if(c >= '0' && c <= '9')        // is c a digit?
138
+      value = value * 10 + c - '0';
139
+    read();  // consume the character we got with peek
140
+    c = timedPeek();
141
+  }
142
+  while( (c >= '0' && c <= '9') || c == skipChar );
143
+
144
+  if(isNegative)
145
+    value = -value;
146
+  return value;
147
+}
148
+
149
+
150
+// as parseInt but returns a floating point value
151
+float Stream::parseFloat()
152
+{
153
+  return parseFloat(NO_SKIP_CHAR);
154
+}
155
+
156
+// as above but the given skipChar is ignored
157
+// this allows format characters (typically commas) in values to be ignored
158
+float Stream::parseFloat(char skipChar){
159
+  bool isNegative = false;
160
+  bool isFraction = false;
161
+  long value = 0;
162
+  char c;
163
+  float fraction = 1.0;
164
+
165
+  c = peekNextDigit();
166
+    // ignore non numeric leading characters
167
+  if(c < 0)
168
+    return 0; // zero returned if timeout
169
+
170
+  do{
171
+    if(c == skipChar)
172
+      ; // ignore
173
+    else if(c == '-')
174
+      isNegative = true;
175
+    else if (c == '.')
176
+      isFraction = true;
177
+    else if(c >= '0' && c <= '9')  {      // is c a digit?
178
+      value = value * 10 + c - '0';
179
+      if(isFraction)
180
+         fraction *= 0.1;
181
+    }
182
+    read();  // consume the character we got with peek
183
+    c = timedPeek();
184
+  }
185
+  while( (c >= '0' && c <= '9')  || c == '.' || c == skipChar );
186
+
187
+  if(isNegative)
188
+    value = -value;
189
+  if(isFraction)
190
+    return value * fraction;
191
+  else
192
+    return value;
193
+}
194
+
195
+// read characters from stream into buffer
196
+// terminates if length characters have been read, or timeout (see setTimeout)
197
+// returns the number of characters placed in the buffer
198
+// the buffer is NOT null terminated.
199
+//
200
+size_t Stream::readBytes(char *buffer, size_t length)
201
+{
202
+  size_t count = 0;
203
+  while (count < length) {
204
+    int c = timedRead();
205
+    if (c < 0) break;
206
+    *buffer++ = (char)c;
207
+    count++;
208
+  }
209
+  return count;
210
+}
211
+
212
+
213
+// as readBytes with terminator character
214
+// terminates if length characters have been read, timeout, or if the terminator character  detected
215
+// returns the number of characters placed in the buffer (0 means no valid data found)
216
+
217
+size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
218
+{
219
+  if (length < 1) return 0;
220
+  size_t index = 0;
221
+  while (index < length) {
222
+    int c = timedRead();
223
+    if (c < 0 || c == terminator) break;
224
+    *buffer++ = (char)c;
225
+    index++;
226
+  }
227
+  return index; // return number of characters, not including null terminator
228
+}
229
+
230
+String Stream::readString()
231
+{
232
+  String ret;
233
+  int c = timedRead();
234
+  while (c >= 0)
235
+  {
236
+    ret += (char)c;
237
+    c = timedRead();
238
+  }
239
+  return ret;
240
+}
241
+
242
+String Stream::readStringUntil(char terminator)
243
+{
244
+  String ret;
245
+  int c = timedRead();
246
+  while (c >= 0 && c != terminator)
247
+  {
248
+    ret += (char)c;
249
+    c = timedRead();
250
+  }
251
+  return ret;
252
+}
253
+
254
+int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
255
+  // any zero length target string automatically matches and would make
256
+  // a mess of the rest of the algorithm.
257
+  for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
258
+    if (t->len <= 0)
259
+      return t - targets;
260
+  }
261
+
262
+  while (1) {
263
+    int c = timedRead();
264
+    if (c < 0)
265
+      return -1;
266
+
267
+    for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
268
+      // the simple case is if we match, deal with that first.
269
+      if (c == t->str[t->index]) {
270
+        if (++t->index == t->len)
271
+          return t - targets;
272
+        else
273
+          continue;
274
+      }
275
+
276
+      // if not we need to walk back and see if we could have matched further
277
+      // down the stream (ie '1112' doesn't match the first position in '11112'
278
+      // but it will match the second position so we can't just reset the current
279
+      // index to 0 when we find a mismatch.
280
+      if (t->index == 0)
281
+        continue;
282
+
283
+      int origIndex = t->index;
284
+      do {
285
+        --t->index;
286
+        // first check if current char works against the new current index
287
+        if (c != t->str[t->index])
288
+          continue;
289
+
290
+        // if it's the only char then we're good, nothing more to check
291
+        if (t->index == 0) {
292
+          t->index++;
293
+          break;
294
+        }
295
+
296
+        // otherwise we need to check the rest of the found string
297
+        int diff = origIndex - t->index;
298
+        size_t i;
299
+        for (i = 0; i < t->index; ++i) {
300
+          if (t->str[i] != t->str[i + diff])
301
+            break;
302
+        }
303
+
304
+        // if we successfully got through the previous loop then our current
305
+        // index is good.
306
+        if (i == t->index) {
307
+          t->index++;
308
+          break;
309
+        }
310
+
311
+        // otherwise we just try the next index
312
+      } while (t->index);
313
+    }
314
+  }
315
+  // unreachable
316
+  return -1;
317
+}

+ 113
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Stream.h View File

@@ -0,0 +1,113 @@
1
+/*
2
+  Stream.h - base class for character-based streams.
3
+  Copyright (c) 2010 David A. Mellis.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+
19
+  parsing functions based on TextFinder library by Michael Margolis
20
+*/
21
+
22
+#ifndef Stream_h
23
+#define Stream_h
24
+
25
+#include <inttypes.h>
26
+#include "Print.h"
27
+
28
+// compatability macros for testing
29
+/*
30
+#define   getInt()            parseInt()
31
+#define   getInt(skipChar)    parseInt(skipchar)
32
+#define   getFloat()          parseFloat()
33
+#define   getFloat(skipChar)  parseFloat(skipChar)
34
+#define   getString( pre_string, post_string, buffer, length)
35
+readBytesBetween( pre_string, terminator, buffer, length)
36
+*/
37
+
38
+class Stream : public Print
39
+{
40
+  protected:
41
+    unsigned long _timeout;      // number of milliseconds to wait for the next char before aborting timed read
42
+    unsigned long _startMillis;  // used for timeout measurement
43
+    int timedRead();    // private method to read stream with timeout
44
+    int timedPeek();    // private method to peek stream with timeout
45
+    int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
46
+
47
+  public:
48
+    virtual int available() = 0;
49
+    virtual int read() = 0;
50
+    virtual int peek() = 0;
51
+    virtual void flush() = 0;
52
+
53
+    Stream() {_timeout=1000;}
54
+
55
+// parsing methods
56
+
57
+  void setTimeout(unsigned long timeout);  // sets maximum milliseconds to wait for stream data, default is 1 second
58
+
59
+  bool find(char *target);   // reads data from the stream until the target string is found
60
+  bool find(uint8_t *target) { return find ((char *)target); }
61
+  // returns true if target string is found, false if timed out (see setTimeout)
62
+
63
+  bool find(char *target, size_t length);   // reads data from the stream until the target string of given length is found
64
+  bool find(uint8_t *target, size_t length) { return find ((char *)target, length); }
65
+  // returns true if target string is found, false if timed out
66
+
67
+  bool findUntil(char *target, char *terminator);   // as find but search ends if the terminator string is found
68
+  bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); }
69
+
70
+  bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen);   // as above but search ends if the terminate string is found
71
+  bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
72
+
73
+
74
+  long parseInt(); // returns the first valid (long) integer value from the current position.
75
+  // initial characters that are not digits (or the minus sign) are skipped
76
+  // integer is terminated by the first character that is not a digit.
77
+
78
+  float parseFloat();               // float version of parseInt
79
+
80
+  size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
81
+  size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
82
+  // terminates if length characters have been read or timeout (see setTimeout)
83
+  // returns the number of characters placed in the buffer (0 means no valid data found)
84
+
85
+  size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
86
+  size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); }
87
+  // terminates if length characters have been read, timeout, or if the terminator character  detected
88
+  // returns the number of characters placed in the buffer (0 means no valid data found)
89
+
90
+  // Arduino String functions to be added here
91
+  String readString();
92
+  String readStringUntil(char terminator);
93
+
94
+  protected:
95
+  long parseInt(char skipChar); // as above but the given skipChar is ignored
96
+  // as above but the given skipChar is ignored
97
+  // this allows format characters (typically commas) in values to be ignored
98
+
99
+  float parseFloat(char skipChar);  // as above but the given skipChar is ignored
100
+
101
+  struct MultiTarget {
102
+    const char *str;  // string you're searching for
103
+    size_t len;       // length of string you're searching for
104
+    size_t index;     // index used by the search routine.
105
+  };
106
+
107
+  // This allows you to search for an arbitrary number of strings.
108
+  // Returns index of the target that is found first or -1 if timeout occurs.
109
+  int findMulti(struct MultiTarget *targets, int tCount);
110
+};
111
+
112
+
113
+#endif

+ 618
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Tone.cpp View File

@@ -0,0 +1,618 @@
1
+/* Tone.cpp
2
+
3
+  A Tone Generator Library
4
+
5
+  Written by Brett Hagman
6
+
7
+  This library is free software; you can redistribute it and/or
8
+  modify it under the terms of the GNU Lesser General Public
9
+  License as published by the Free Software Foundation; either
10
+  version 2.1 of the License, or (at your option) any later version.
11
+
12
+  This library is distributed in the hope that it will be useful,
13
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+  Lesser General Public License for more details.
16
+
17
+  You should have received a copy of the GNU Lesser General Public
18
+  License along with this library; if not, write to the Free Software
19
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
+
21
+Version Modified By Date     Comments
22
+------- ----------- -------- --------
23
+0001    B Hagman    09/08/02 Initial coding
24
+0002    B Hagman    09/08/18 Multiple pins
25
+0003    B Hagman    09/08/18 Moved initialization from constructor to begin()
26
+0004    B Hagman    09/09/26 Fixed problems with ATmega8
27
+0005    B Hagman    09/11/23 Scanned prescalars for best fit on 8 bit timers
28
+                    09/11/25 Changed pin toggle method to XOR
29
+                    09/11/25 Fixed timer0 from being excluded
30
+0006    D Mellis    09/12/29 Replaced objects with functions
31
+0007    M Sproul    10/08/29 Changed #ifdefs from cpu to register
32
+0008    S Kanemoto  12/06/22 Fixed for Leonardo by @maris_HY
33
+0009    J Reucker   15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62)
34
+0010    jipp        15/04/13 added additional define check #2923
35
+*************************************************/
36
+
37
+#include <avr/interrupt.h>
38
+#include <avr/pgmspace.h>
39
+#include "Arduino.h"
40
+#include "pins_arduino.h"
41
+
42
+#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
43
+#define TCCR2A TCCR2
44
+#define TCCR2B TCCR2
45
+#define COM2A1 COM21
46
+#define COM2A0 COM20
47
+#define OCR2A OCR2
48
+#define TIMSK2 TIMSK
49
+#define OCIE2A OCIE2
50
+#define TIMER2_COMPA_vect TIMER2_COMP_vect
51
+#define TIMSK1 TIMSK
52
+#endif
53
+
54
+// timerx_toggle_count:
55
+//  > 0 - duration specified
56
+//  = 0 - stopped
57
+//  < 0 - infinitely (until stop() method called, or new play() called)
58
+
59
+#if !defined(__AVR_ATmega8__)
60
+volatile long timer0_toggle_count;
61
+volatile uint8_t *timer0_pin_port;
62
+volatile uint8_t timer0_pin_mask;
63
+#endif
64
+
65
+volatile long timer1_toggle_count;
66
+volatile uint8_t *timer1_pin_port;
67
+volatile uint8_t timer1_pin_mask;
68
+volatile long timer2_toggle_count;
69
+volatile uint8_t *timer2_pin_port;
70
+volatile uint8_t timer2_pin_mask;
71
+
72
+#if defined(TIMSK3)
73
+volatile long timer3_toggle_count;
74
+volatile uint8_t *timer3_pin_port;
75
+volatile uint8_t timer3_pin_mask;
76
+#endif
77
+
78
+#if defined(TIMSK4)
79
+volatile long timer4_toggle_count;
80
+volatile uint8_t *timer4_pin_port;
81
+volatile uint8_t timer4_pin_mask;
82
+#endif
83
+
84
+#if defined(TIMSK5)
85
+volatile long timer5_toggle_count;
86
+volatile uint8_t *timer5_pin_port;
87
+volatile uint8_t timer5_pin_mask;
88
+#endif
89
+
90
+
91
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
92
+
93
+#define AVAILABLE_TONE_PINS 1
94
+#define USE_TIMER2
95
+
96
+const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
97
+static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
98
+
99
+#elif defined(__AVR_ATmega8__)
100
+
101
+#define AVAILABLE_TONE_PINS 1
102
+#define USE_TIMER2
103
+
104
+const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
105
+static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
106
+
107
+#elif defined(__AVR_ATmega32U4__)
108
+ 
109
+#define AVAILABLE_TONE_PINS 1
110
+#define USE_TIMER3
111
+ 
112
+const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
113
+static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
114
+ 
115
+#else
116
+
117
+#define AVAILABLE_TONE_PINS 1
118
+#define USE_TIMER2
119
+
120
+// Leave timer 0 to last.
121
+const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
122
+static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
123
+
124
+#endif
125
+
126
+
127
+
128
+static int8_t toneBegin(uint8_t _pin)
129
+{
130
+  int8_t _timer = -1;
131
+
132
+  // if we're already using the pin, the timer should be configured.  
133
+  for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
134
+    if (tone_pins[i] == _pin) {
135
+      return pgm_read_byte(tone_pin_to_timer_PGM + i);
136
+    }
137
+  }
138
+  
139
+  // search for an unused timer.
140
+  for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
141
+    if (tone_pins[i] == 255) {
142
+      tone_pins[i] = _pin;
143
+      _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
144
+      break;
145
+    }
146
+  }
147
+  
148
+  if (_timer != -1)
149
+  {
150
+    // Set timer specific stuff
151
+    // All timers in CTC mode
152
+    // 8 bit timers will require changing prescalar values,
153
+    // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
154
+    switch (_timer)
155
+    {
156
+      #if defined(TCCR0A) && defined(TCCR0B) && defined(WGM01)
157
+      case 0:
158
+        // 8 bit timer
159
+        TCCR0A = 0;
160
+        TCCR0B = 0;
161
+        bitWrite(TCCR0A, WGM01, 1);
162
+        bitWrite(TCCR0B, CS00, 1);
163
+        timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
164
+        timer0_pin_mask = digitalPinToBitMask(_pin);
165
+        break;
166
+      #endif
167
+
168
+      #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
169
+      case 1:
170
+        // 16 bit timer
171
+        TCCR1A = 0;
172
+        TCCR1B = 0;
173
+        bitWrite(TCCR1B, WGM12, 1);
174
+        bitWrite(TCCR1B, CS10, 1);
175
+        timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
176
+        timer1_pin_mask = digitalPinToBitMask(_pin);
177
+        break;
178
+      #endif
179
+
180
+      #if defined(TCCR2A) && defined(TCCR2B)
181
+      case 2:
182
+        // 8 bit timer
183
+        TCCR2A = 0;
184
+        TCCR2B = 0;
185
+        bitWrite(TCCR2A, WGM21, 1);
186
+        bitWrite(TCCR2B, CS20, 1);
187
+        timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
188
+        timer2_pin_mask = digitalPinToBitMask(_pin);
189
+        break;
190
+      #endif
191
+
192
+      #if defined(TCCR3A) && defined(TCCR3B) &&  defined(TIMSK3)
193
+      case 3:
194
+        // 16 bit timer
195
+        TCCR3A = 0;
196
+        TCCR3B = 0;
197
+        bitWrite(TCCR3B, WGM32, 1);
198
+        bitWrite(TCCR3B, CS30, 1);
199
+        timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
200
+        timer3_pin_mask = digitalPinToBitMask(_pin);
201
+        break;
202
+      #endif
203
+
204
+      #if defined(TCCR4A) && defined(TCCR4B) &&  defined(TIMSK4)
205
+      case 4:
206
+        // 16 bit timer
207
+        TCCR4A = 0;
208
+        TCCR4B = 0;
209
+        #if defined(WGM42)
210
+          bitWrite(TCCR4B, WGM42, 1);
211
+        #elif defined(CS43)
212
+          #warning this may not be correct
213
+          // atmega32u4
214
+          bitWrite(TCCR4B, CS43, 1);
215
+        #endif
216
+        bitWrite(TCCR4B, CS40, 1);
217
+        timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
218
+        timer4_pin_mask = digitalPinToBitMask(_pin);
219
+        break;
220
+      #endif
221
+
222
+      #if defined(TCCR5A) && defined(TCCR5B) &&  defined(TIMSK5)
223
+      case 5:
224
+        // 16 bit timer
225
+        TCCR5A = 0;
226
+        TCCR5B = 0;
227
+        bitWrite(TCCR5B, WGM52, 1);
228
+        bitWrite(TCCR5B, CS50, 1);
229
+        timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
230
+        timer5_pin_mask = digitalPinToBitMask(_pin);
231
+        break;
232
+      #endif
233
+    }
234
+  }
235
+
236
+  return _timer;
237
+}
238
+
239
+
240
+
241
+// frequency (in hertz) and duration (in milliseconds).
242
+
243
+void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
244
+{
245
+  uint8_t prescalarbits = 0b001;
246
+  long toggle_count = 0;
247
+  uint32_t ocr = 0;
248
+  int8_t _timer;
249
+
250
+  _timer = toneBegin(_pin);
251
+
252
+  if (_timer >= 0)
253
+  {
254
+    // Set the pinMode as OUTPUT
255
+    pinMode(_pin, OUTPUT);
256
+    
257
+    // if we are using an 8 bit timer, scan through prescalars to find the best fit
258
+    if (_timer == 0 || _timer == 2)
259
+    {
260
+      ocr = F_CPU / frequency / 2 - 1;
261
+      prescalarbits = 0b001;  // ck/1: same for both timers
262
+      if (ocr > 255)
263
+      {
264
+        ocr = F_CPU / frequency / 2 / 8 - 1;
265
+        prescalarbits = 0b010;  // ck/8: same for both timers
266
+
267
+        if (_timer == 2 && ocr > 255)
268
+        {
269
+          ocr = F_CPU / frequency / 2 / 32 - 1;
270
+          prescalarbits = 0b011;
271
+        }
272
+
273
+        if (ocr > 255)
274
+        {
275
+          ocr = F_CPU / frequency / 2 / 64 - 1;
276
+          prescalarbits = _timer == 0 ? 0b011 : 0b100;
277
+
278
+          if (_timer == 2 && ocr > 255)
279
+          {
280
+            ocr = F_CPU / frequency / 2 / 128 - 1;
281
+            prescalarbits = 0b101;
282
+          }
283
+
284
+          if (ocr > 255)
285
+          {
286
+            ocr = F_CPU / frequency / 2 / 256 - 1;
287
+            prescalarbits = _timer == 0 ? 0b100 : 0b110;
288
+            if (ocr > 255)
289
+            {
290
+              // can't do any better than /1024
291
+              ocr = F_CPU / frequency / 2 / 1024 - 1;
292
+              prescalarbits = _timer == 0 ? 0b101 : 0b111;
293
+            }
294
+          }
295
+        }
296
+      }
297
+
298
+#if defined(TCCR0B)
299
+      if (_timer == 0)
300
+      {
301
+        TCCR0B = (TCCR0B & 0b11111000) | prescalarbits;
302
+      }
303
+      else
304
+#endif
305
+#if defined(TCCR2B)
306
+      {
307
+        TCCR2B = (TCCR2B & 0b11111000) | prescalarbits;
308
+      }
309
+#else
310
+      {
311
+        // dummy place holder to make the above ifdefs work
312
+      }
313
+#endif
314
+    }
315
+    else
316
+    {
317
+      // two choices for the 16 bit timers: ck/1 or ck/64
318
+      ocr = F_CPU / frequency / 2 - 1;
319
+
320
+      prescalarbits = 0b001;
321
+      if (ocr > 0xffff)
322
+      {
323
+        ocr = F_CPU / frequency / 2 / 64 - 1;
324
+        prescalarbits = 0b011;
325
+      }
326
+
327
+      if (_timer == 1)
328
+      {
329
+#if defined(TCCR1B)
330
+        TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
331
+#endif
332
+      }
333
+#if defined(TCCR3B)
334
+      else if (_timer == 3)
335
+        TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
336
+#endif
337
+#if defined(TCCR4B)
338
+      else if (_timer == 4)
339
+        TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
340
+#endif
341
+#if defined(TCCR5B)
342
+      else if (_timer == 5)
343
+        TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
344
+#endif
345
+
346
+    }
347
+    
348
+
349
+    // Calculate the toggle count
350
+    if (duration > 0)
351
+    {
352
+      toggle_count = 2 * frequency * duration / 1000;
353
+    }
354
+    else
355
+    {
356
+      toggle_count = -1;
357
+    }
358
+
359
+    // Set the OCR for the given timer,
360
+    // set the toggle count,
361
+    // then turn on the interrupts
362
+    switch (_timer)
363
+    {
364
+
365
+#if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
366
+      case 0:
367
+        OCR0A = ocr;
368
+        timer0_toggle_count = toggle_count;
369
+        bitWrite(TIMSK0, OCIE0A, 1);
370
+        break;
371
+#endif
372
+
373
+      case 1:
374
+#if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
375
+        OCR1A = ocr;
376
+        timer1_toggle_count = toggle_count;
377
+        bitWrite(TIMSK1, OCIE1A, 1);
378
+#elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
379
+        // this combination is for at least the ATmega32
380
+        OCR1A = ocr;
381
+        timer1_toggle_count = toggle_count;
382
+        bitWrite(TIMSK, OCIE1A, 1);
383
+#endif
384
+        break;
385
+
386
+#if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
387
+      case 2:
388
+        OCR2A = ocr;
389
+        timer2_toggle_count = toggle_count;
390
+        bitWrite(TIMSK2, OCIE2A, 1);
391
+        break;
392
+#endif
393
+
394
+#if defined(OCR3A) && defined(TIMSK3) && defined(OCIE3A)
395
+      case 3:
396
+        OCR3A = ocr;
397
+        timer3_toggle_count = toggle_count;
398
+        bitWrite(TIMSK3, OCIE3A, 1);
399
+        break;
400
+#endif
401
+
402
+#if defined(OCR4A) && defined(TIMSK4) && defined(OCIE4A)
403
+      case 4:
404
+        OCR4A = ocr;
405
+        timer4_toggle_count = toggle_count;
406
+        bitWrite(TIMSK4, OCIE4A, 1);
407
+        break;
408
+#endif
409
+
410
+#if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
411
+      case 5:
412
+        OCR5A = ocr;
413
+        timer5_toggle_count = toggle_count;
414
+        bitWrite(TIMSK5, OCIE5A, 1);
415
+        break;
416
+#endif
417
+
418
+    }
419
+  }
420
+}
421
+
422
+
423
+// XXX: this function only works properly for timer 2 (the only one we use
424
+// currently).  for the others, it should end the tone, but won't restore
425
+// proper PWM functionality for the timer.
426
+void disableTimer(uint8_t _timer)
427
+{
428
+  switch (_timer)
429
+  {
430
+    case 0:
431
+      #if defined(TIMSK0)
432
+        TIMSK0 = 0;
433
+      #elif defined(TIMSK)
434
+        TIMSK = 0; // atmega32
435
+      #endif
436
+      break;
437
+
438
+#if defined(TIMSK1) && defined(OCIE1A)
439
+    case 1:
440
+      bitWrite(TIMSK1, OCIE1A, 0);
441
+      break;
442
+#endif
443
+
444
+    case 2:
445
+      #if defined(TIMSK2) && defined(OCIE2A)
446
+        bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
447
+      #endif
448
+      #if defined(TCCR2A) && defined(WGM20)
449
+        TCCR2A = (1 << WGM20);
450
+      #endif
451
+      #if defined(TCCR2B) && defined(CS22)
452
+        TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
453
+      #endif
454
+      #if defined(OCR2A)
455
+        OCR2A = 0;
456
+      #endif
457
+      break;
458
+
459
+#if defined(TIMSK3) && defined(OCIE3A)
460
+    case 3:
461
+      bitWrite(TIMSK3, OCIE3A, 0);
462
+      break;
463
+#endif
464
+
465
+#if defined(TIMSK4) && defined(OCIE4A)
466
+    case 4:
467
+      bitWrite(TIMSK4, OCIE4A, 0);
468
+      break;
469
+#endif
470
+
471
+#if defined(TIMSK5) && defined(OCIE5A)
472
+    case 5:
473
+      bitWrite(TIMSK5, OCIE5A, 0);
474
+      break;
475
+#endif
476
+  }
477
+}
478
+
479
+
480
+void noTone(uint8_t _pin)
481
+{
482
+  int8_t _timer = -1;
483
+  
484
+  for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
485
+    if (tone_pins[i] == _pin) {
486
+      _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
487
+      tone_pins[i] = 255;
488
+    }
489
+  }
490
+  
491
+  disableTimer(_timer);
492
+
493
+  digitalWrite(_pin, 0);
494
+}
495
+
496
+#ifdef USE_TIMER0
497
+ISR(TIMER0_COMPA_vect)
498
+{
499
+  if (timer0_toggle_count != 0)
500
+  {
501
+    // toggle the pin
502
+    *timer0_pin_port ^= timer0_pin_mask;
503
+
504
+    if (timer0_toggle_count > 0)
505
+      timer0_toggle_count--;
506
+  }
507
+  else
508
+  {
509
+    disableTimer(0);
510
+    *timer0_pin_port &= ~(timer0_pin_mask);  // keep pin low after stop
511
+  }
512
+}
513
+#endif
514
+
515
+
516
+#ifdef USE_TIMER1
517
+ISR(TIMER1_COMPA_vect)
518
+{
519
+  if (timer1_toggle_count != 0)
520
+  {
521
+    // toggle the pin
522
+    *timer1_pin_port ^= timer1_pin_mask;
523
+
524
+    if (timer1_toggle_count > 0)
525
+      timer1_toggle_count--;
526
+  }
527
+  else
528
+  {
529
+    disableTimer(1);
530
+    *timer1_pin_port &= ~(timer1_pin_mask);  // keep pin low after stop
531
+  }
532
+}
533
+#endif
534
+
535
+
536
+#ifdef USE_TIMER2
537
+ISR(TIMER2_COMPA_vect)
538
+{
539
+
540
+  if (timer2_toggle_count != 0)
541
+  {
542
+    // toggle the pin
543
+    *timer2_pin_port ^= timer2_pin_mask;
544
+
545
+    if (timer2_toggle_count > 0)
546
+      timer2_toggle_count--;
547
+  }
548
+  else
549
+  {
550
+    // need to call noTone() so that the tone_pins[] entry is reset, so the
551
+    // timer gets initialized next time we call tone().
552
+    // XXX: this assumes timer 2 is always the first one used.
553
+    noTone(tone_pins[0]);
554
+//    disableTimer(2);
555
+//    *timer2_pin_port &= ~(timer2_pin_mask);  // keep pin low after stop
556
+  }
557
+}
558
+#endif
559
+
560
+
561
+#ifdef USE_TIMER3
562
+ISR(TIMER3_COMPA_vect)
563
+{
564
+  if (timer3_toggle_count != 0)
565
+  {
566
+    // toggle the pin
567
+    *timer3_pin_port ^= timer3_pin_mask;
568
+
569
+    if (timer3_toggle_count > 0)
570
+      timer3_toggle_count--;
571
+  }
572
+  else
573
+  {
574
+    disableTimer(3);
575
+    *timer3_pin_port &= ~(timer3_pin_mask);  // keep pin low after stop
576
+  }
577
+}
578
+#endif
579
+
580
+
581
+#ifdef USE_TIMER4
582
+ISR(TIMER4_COMPA_vect)
583
+{
584
+  if (timer4_toggle_count != 0)
585
+  {
586
+    // toggle the pin
587
+    *timer4_pin_port ^= timer4_pin_mask;
588
+
589
+    if (timer4_toggle_count > 0)
590
+      timer4_toggle_count--;
591
+  }
592
+  else
593
+  {
594
+    disableTimer(4);
595
+    *timer4_pin_port &= ~(timer4_pin_mask);  // keep pin low after stop
596
+  }
597
+}
598
+#endif
599
+
600
+
601
+#ifdef USE_TIMER5
602
+ISR(TIMER5_COMPA_vect)
603
+{
604
+  if (timer5_toggle_count != 0)
605
+  {
606
+    // toggle the pin
607
+    *timer5_pin_port ^= timer5_pin_mask;
608
+
609
+    if (timer5_toggle_count > 0)
610
+      timer5_toggle_count--;
611
+  }
612
+  else
613
+  {
614
+    disableTimer(5);
615
+    *timer5_pin_port &= ~(timer5_pin_mask);  // keep pin low after stop
616
+  }
617
+}
618
+#endif

+ 244
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/USBAPI.h View File

@@ -0,0 +1,244 @@
1
+/*
2
+  USBAPI.h
3
+  Copyright (c) 2005-2014 Arduino.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef __USBAPI__
21
+#define __USBAPI__
22
+
23
+#include <inttypes.h>
24
+#include <avr/pgmspace.h>
25
+#include <avr/eeprom.h>
26
+#include <avr/interrupt.h>
27
+#include <util/delay.h>
28
+
29
+typedef unsigned char u8;
30
+typedef unsigned short u16;
31
+typedef unsigned long u32;
32
+
33
+#include "Arduino.h"
34
+
35
+#if defined(USBCON)
36
+
37
+#include "USBDesc.h"
38
+#include "USBCore.h"
39
+
40
+//================================================================================
41
+//================================================================================
42
+//	USB
43
+
44
+class USBDevice_
45
+{
46
+public:
47
+	USBDevice_();
48
+	bool configured();
49
+
50
+	void attach();
51
+	void detach();	// Serial port goes down too...
52
+	void poll();
53
+};
54
+extern USBDevice_ USBDevice;
55
+
56
+//================================================================================
57
+//================================================================================
58
+//	Serial over CDC (Serial1 is the physical port)
59
+
60
+struct ring_buffer;
61
+
62
+#if (RAMEND < 1000)
63
+#define SERIAL_BUFFER_SIZE 16
64
+#else
65
+#define SERIAL_BUFFER_SIZE 64
66
+#endif
67
+
68
+class Serial_ : public Stream
69
+{
70
+private:
71
+	int peek_buffer;
72
+public:
73
+	Serial_() { peek_buffer = -1; };
74
+	void begin(unsigned long);
75
+	void begin(unsigned long, uint8_t);
76
+	void end(void);
77
+
78
+	virtual int available(void);
79
+	virtual int peek(void);
80
+	virtual int read(void);
81
+	virtual void flush(void);
82
+	virtual size_t write(uint8_t);
83
+	virtual size_t write(const uint8_t*, size_t);
84
+	using Print::write; // pull in write(str) and write(buf, size) from Print
85
+	operator bool();
86
+
87
+	volatile uint8_t _rx_buffer_head;
88
+	volatile uint8_t _rx_buffer_tail;
89
+	unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
90
+};
91
+extern Serial_ Serial;
92
+
93
+#define HAVE_CDCSERIAL
94
+
95
+//================================================================================
96
+//================================================================================
97
+//	Mouse
98
+
99
+#define MOUSE_LEFT 1
100
+#define MOUSE_RIGHT 2
101
+#define MOUSE_MIDDLE 4
102
+#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
103
+
104
+class Mouse_
105
+{
106
+private:
107
+	uint8_t _buttons;
108
+	void buttons(uint8_t b);
109
+public:
110
+	Mouse_(void);
111
+	void begin(void);
112
+	void end(void);
113
+	void click(uint8_t b = MOUSE_LEFT);
114
+	void move(signed char x, signed char y, signed char wheel = 0);	
115
+	void press(uint8_t b = MOUSE_LEFT);		// press LEFT by default
116
+	void release(uint8_t b = MOUSE_LEFT);	// release LEFT by default
117
+	bool isPressed(uint8_t b = MOUSE_LEFT);	// check LEFT by default
118
+};
119
+extern Mouse_ Mouse;
120
+
121
+//================================================================================
122
+//================================================================================
123
+//	Keyboard
124
+
125
+#define KEY_LEFT_CTRL		0x80
126
+#define KEY_LEFT_SHIFT		0x81
127
+#define KEY_LEFT_ALT		0x82
128
+#define KEY_LEFT_GUI		0x83
129
+#define KEY_RIGHT_CTRL		0x84
130
+#define KEY_RIGHT_SHIFT		0x85
131
+#define KEY_RIGHT_ALT		0x86
132
+#define KEY_RIGHT_GUI		0x87
133
+
134
+#define KEY_UP_ARROW		0xDA
135
+#define KEY_DOWN_ARROW		0xD9
136
+#define KEY_LEFT_ARROW		0xD8
137
+#define KEY_RIGHT_ARROW		0xD7
138
+#define KEY_BACKSPACE		0xB2
139
+#define KEY_TAB				0xB3
140
+#define KEY_RETURN			0xB0
141
+#define KEY_ESC				0xB1
142
+#define KEY_INSERT			0xD1
143
+#define KEY_DELETE			0xD4
144
+#define KEY_PAGE_UP			0xD3
145
+#define KEY_PAGE_DOWN		0xD6
146
+#define KEY_HOME			0xD2
147
+#define KEY_END				0xD5
148
+#define KEY_CAPS_LOCK		0xC1
149
+#define KEY_F1				0xC2
150
+#define KEY_F2				0xC3
151
+#define KEY_F3				0xC4
152
+#define KEY_F4				0xC5
153
+#define KEY_F5				0xC6
154
+#define KEY_F6				0xC7
155
+#define KEY_F7				0xC8
156
+#define KEY_F8				0xC9
157
+#define KEY_F9				0xCA
158
+#define KEY_F10				0xCB
159
+#define KEY_F11				0xCC
160
+#define KEY_F12				0xCD
161
+
162
+//	Low level key report: up to 6 keys and shift, ctrl etc at once
163
+typedef struct
164
+{
165
+	uint8_t modifiers;
166
+	uint8_t reserved;
167
+	uint8_t keys[6];
168
+} KeyReport;
169
+
170
+class Keyboard_ : public Print
171
+{
172
+private:
173
+	KeyReport _keyReport;
174
+	void sendReport(KeyReport* keys);
175
+public:
176
+	Keyboard_(void);
177
+	void begin(void);
178
+	void end(void);
179
+	virtual size_t write(uint8_t k);
180
+	virtual size_t press(uint8_t k);
181
+	virtual size_t release(uint8_t k);
182
+	virtual void releaseAll(void);
183
+};
184
+extern Keyboard_ Keyboard;
185
+
186
+//================================================================================
187
+//================================================================================
188
+//	Low level API
189
+
190
+typedef struct
191
+{
192
+	uint8_t bmRequestType;
193
+	uint8_t bRequest;
194
+	uint8_t wValueL;
195
+	uint8_t wValueH;
196
+	uint16_t wIndex;
197
+	uint16_t wLength;
198
+} Setup;
199
+
200
+//================================================================================
201
+//================================================================================
202
+//	HID 'Driver'
203
+
204
+int		HID_GetInterface(uint8_t* interfaceNum);
205
+int		HID_GetDescriptor(int i);
206
+bool	HID_Setup(Setup& setup);
207
+void	HID_SendReport(uint8_t id, const void* data, int len);
208
+
209
+//================================================================================
210
+//================================================================================
211
+//	MSC 'Driver'
212
+
213
+int		MSC_GetInterface(uint8_t* interfaceNum);
214
+int		MSC_GetDescriptor(int i);
215
+bool	MSC_Setup(Setup& setup);
216
+bool	MSC_Data(uint8_t rx,uint8_t tx);
217
+
218
+//================================================================================
219
+//================================================================================
220
+//	CSC 'Driver'
221
+
222
+int		CDC_GetInterface(uint8_t* interfaceNum);
223
+int		CDC_GetDescriptor(int i);
224
+bool	CDC_Setup(Setup& setup);
225
+
226
+//================================================================================
227
+//================================================================================
228
+
229
+#define TRANSFER_PGM		0x80
230
+#define TRANSFER_RELEASE	0x40
231
+#define TRANSFER_ZERO		0x20
232
+
233
+int USB_SendControl(uint8_t flags, const void* d, int len);
234
+int USB_RecvControl(void* d, int len);
235
+
236
+uint8_t	USB_Available(uint8_t ep);
237
+int USB_Send(uint8_t ep, const void* data, int len);	// blocking
238
+int USB_Recv(uint8_t ep, void* data, int len);		// non-blocking
239
+int USB_Recv(uint8_t ep);							// non-blocking
240
+void USB_Flush(uint8_t ep);
241
+
242
+#endif
243
+
244
+#endif /* if defined(USBCON) */

+ 699
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/USBCore.cpp View File

@@ -0,0 +1,699 @@
1
+
2
+
3
+/* Copyright (c) 2010, Peter Barrett  
4
+**  
5
+** Permission to use, copy, modify, and/or distribute this software for  
6
+** any purpose with or without fee is hereby granted, provided that the  
7
+** above copyright notice and this permission notice appear in all copies.  
8
+** 
9
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL  
10
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED  
11
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR  
12
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES  
13
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  
14
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  
15
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS  
16
+** SOFTWARE.  
17
+*/
18
+
19
+#include "USBAPI.h"
20
+
21
+#if defined(USBCON)
22
+
23
+#define EP_TYPE_CONTROL				0x00
24
+#define EP_TYPE_BULK_IN				0x81
25
+#define EP_TYPE_BULK_OUT			0x80
26
+#define EP_TYPE_INTERRUPT_IN		0xC1
27
+#define EP_TYPE_INTERRUPT_OUT		0xC0
28
+#define EP_TYPE_ISOCHRONOUS_IN		0x41
29
+#define EP_TYPE_ISOCHRONOUS_OUT		0x40
30
+
31
+/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
32
+#define TX_RX_LED_PULSE_MS 100
33
+volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
34
+volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
35
+
36
+//==================================================================
37
+//==================================================================
38
+
39
+extern const u16 STRING_LANGUAGE[] PROGMEM;
40
+extern const u8 STRING_PRODUCT[] PROGMEM;
41
+extern const u8 STRING_MANUFACTURER[] PROGMEM;
42
+extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM;
43
+extern const DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
44
+
45
+const u16 STRING_LANGUAGE[2] = {
46
+	(3<<8) | (2+2),
47
+	0x0409	// English
48
+};
49
+
50
+#ifndef USB_PRODUCT
51
+// If no product is provided, use USB IO Board
52
+#define USB_PRODUCT     "USB IO Board"
53
+#endif
54
+
55
+const u8 STRING_PRODUCT[] PROGMEM = USB_PRODUCT;
56
+
57
+#if USB_VID == 0x2341
58
+#  if defined(USB_MANUFACTURER)
59
+#    undef USB_MANUFACTURER
60
+#  endif
61
+#  define USB_MANUFACTURER "Arduino LLC"
62
+#elif USB_VID == 0x1b4f
63
+#  if defined(USB_MANUFACTURER)
64
+#    undef USB_MANUFACTURER
65
+#  endif
66
+#  define USB_MANUFACTURER "SparkFun"
67
+#elif !defined(USB_MANUFACTURER)
68
+// Fall through to unknown if no manufacturer name was provided in a macro
69
+#  define USB_MANUFACTURER "Unknown"
70
+#endif
71
+
72
+const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER;
73
+
74
+
75
+#ifdef CDC_ENABLED
76
+#define DEVICE_CLASS 0x02
77
+#else
78
+#define DEVICE_CLASS 0x00
79
+#endif
80
+
81
+//	DEVICE DESCRIPTOR
82
+const DeviceDescriptor USB_DeviceDescriptor =
83
+	D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
84
+
85
+const DeviceDescriptor USB_DeviceDescriptorA =
86
+	D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
87
+
88
+//==================================================================
89
+//==================================================================
90
+
91
+volatile u8 _usbConfiguration = 0;
92
+
93
+static inline void WaitIN(void)
94
+{
95
+	while (!(UEINTX & (1<<TXINI)))
96
+		;
97
+}
98
+
99
+static inline void ClearIN(void)
100
+{
101
+	UEINTX = ~(1<<TXINI);
102
+}
103
+
104
+static inline void WaitOUT(void)
105
+{
106
+	while (!(UEINTX & (1<<RXOUTI)))
107
+		;
108
+}
109
+
110
+static inline u8 WaitForINOrOUT()
111
+{
112
+	while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
113
+		;
114
+	return (UEINTX & (1<<RXOUTI)) == 0;
115
+}
116
+
117
+static inline void ClearOUT(void)
118
+{
119
+	UEINTX = ~(1<<RXOUTI);
120
+}
121
+
122
+void Recv(volatile u8* data, u8 count)
123
+{
124
+	while (count--)
125
+		*data++ = UEDATX;
126
+	
127
+	RXLED1;					// light the RX LED
128
+	RxLEDPulse = TX_RX_LED_PULSE_MS;	
129
+}
130
+
131
+static inline u8 Recv8()
132
+{
133
+	RXLED1;					// light the RX LED
134
+	RxLEDPulse = TX_RX_LED_PULSE_MS;
135
+
136
+	return UEDATX;	
137
+}
138
+
139
+static inline void Send8(u8 d)
140
+{
141
+	UEDATX = d;
142
+}
143
+
144
+static inline void SetEP(u8 ep)
145
+{
146
+	UENUM = ep;
147
+}
148
+
149
+static inline u8 FifoByteCount()
150
+{
151
+	return UEBCLX;
152
+}
153
+
154
+static inline u8 ReceivedSetupInt()
155
+{
156
+	return UEINTX & (1<<RXSTPI);
157
+}
158
+
159
+static inline void ClearSetupInt()
160
+{
161
+	UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
162
+}
163
+
164
+static inline void Stall()
165
+{
166
+	UECONX = (1<<STALLRQ) | (1<<EPEN);
167
+}
168
+
169
+static inline u8 ReadWriteAllowed()
170
+{
171
+	return UEINTX & (1<<RWAL);
172
+}
173
+
174
+static inline u8 Stalled()
175
+{
176
+	return UEINTX & (1<<STALLEDI);
177
+}
178
+
179
+static inline u8 FifoFree()
180
+{
181
+	return UEINTX & (1<<FIFOCON);
182
+}
183
+
184
+static inline void ReleaseRX()
185
+{
186
+	UEINTX = 0x6B;	// FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
187
+}
188
+
189
+static inline void ReleaseTX()
190
+{
191
+	UEINTX = 0x3A;	// FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
192
+}
193
+
194
+static inline u8 FrameNumber()
195
+{
196
+	return UDFNUML;
197
+}
198
+
199
+//==================================================================
200
+//==================================================================
201
+
202
+u8 USBGetConfiguration(void)
203
+{
204
+	return _usbConfiguration;
205
+}
206
+
207
+#define USB_RECV_TIMEOUT
208
+class LockEP
209
+{
210
+	u8 _sreg;
211
+public:
212
+	LockEP(u8 ep) : _sreg(SREG)
213
+	{
214
+		cli();
215
+		SetEP(ep & 7);
216
+	}
217
+	~LockEP()
218
+	{
219
+		SREG = _sreg;
220
+	}
221
+};
222
+
223
+//	Number of bytes, assumes a rx endpoint
224
+u8 USB_Available(u8 ep)
225
+{
226
+	LockEP lock(ep);
227
+	return FifoByteCount();
228
+}
229
+
230
+//	Non Blocking receive
231
+//	Return number of bytes read
232
+int USB_Recv(u8 ep, void* d, int len)
233
+{
234
+	if (!_usbConfiguration || len < 0)
235
+		return -1;
236
+	
237
+	LockEP lock(ep);
238
+	u8 n = FifoByteCount();
239
+	len = min(n,len);
240
+	n = len;
241
+	u8* dst = (u8*)d;
242
+	while (n--)
243
+		*dst++ = Recv8();
244
+	if (len && !FifoByteCount())	// release empty buffer
245
+		ReleaseRX();
246
+	
247
+	return len;
248
+}
249
+
250
+//	Recv 1 byte if ready
251
+int USB_Recv(u8 ep)
252
+{
253
+	u8 c;
254
+	if (USB_Recv(ep,&c,1) != 1)
255
+		return -1;
256
+	return c;
257
+}
258
+
259
+//	Space in send EP
260
+u8 USB_SendSpace(u8 ep)
261
+{
262
+	LockEP lock(ep);
263
+	if (!ReadWriteAllowed())
264
+		return 0;
265
+	return 64 - FifoByteCount();
266
+}
267
+
268
+//	Blocking Send of data to an endpoint
269
+int USB_Send(u8 ep, const void* d, int len)
270
+{
271
+	if (!_usbConfiguration)
272
+		return -1;
273
+
274
+	int r = len;
275
+	const u8* data = (const u8*)d;
276
+	u8 timeout = 250;		// 250ms timeout on send? TODO
277
+	while (len)
278
+	{
279
+		u8 n = USB_SendSpace(ep);
280
+		if (n == 0)
281
+		{
282
+			if (!(--timeout))
283
+				return -1;
284
+			delay(1);
285
+			continue;
286
+		}
287
+
288
+		if (n > len)
289
+			n = len;
290
+		{
291
+			LockEP lock(ep);
292
+			// Frame may have been released by the SOF interrupt handler
293
+			if (!ReadWriteAllowed())
294
+				continue;
295
+			len -= n;
296
+			if (ep & TRANSFER_ZERO)
297
+			{
298
+				while (n--)
299
+					Send8(0);
300
+			}
301
+			else if (ep & TRANSFER_PGM)
302
+			{
303
+				while (n--)
304
+					Send8(pgm_read_byte(data++));
305
+			}
306
+			else
307
+			{
308
+				while (n--)
309
+					Send8(*data++);
310
+			}
311
+			if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE)))	// Release full buffer
312
+				ReleaseTX();
313
+		}
314
+	}
315
+	TXLED1;					// light the TX LED
316
+	TxLEDPulse = TX_RX_LED_PULSE_MS;
317
+	return r;
318
+}
319
+
320
+extern const u8 _initEndpoints[] PROGMEM;
321
+const u8 _initEndpoints[] = 
322
+{
323
+	0,
324
+	
325
+#ifdef CDC_ENABLED
326
+	EP_TYPE_INTERRUPT_IN,		// CDC_ENDPOINT_ACM
327
+	EP_TYPE_BULK_OUT,			// CDC_ENDPOINT_OUT
328
+	EP_TYPE_BULK_IN,			// CDC_ENDPOINT_IN
329
+#endif
330
+
331
+#ifdef HID_ENABLED
332
+	EP_TYPE_INTERRUPT_IN		// HID_ENDPOINT_INT
333
+#endif
334
+};
335
+
336
+#define EP_SINGLE_64 0x32	// EP0
337
+#define EP_DOUBLE_64 0x36	// Other endpoints
338
+
339
+static
340
+void InitEP(u8 index, u8 type, u8 size)
341
+{
342
+	UENUM = index;
343
+	UECONX = 1;
344
+	UECFG0X = type;
345
+	UECFG1X = size;
346
+}
347
+
348
+static
349
+void InitEndpoints()
350
+{
351
+	for (u8 i = 1; i < sizeof(_initEndpoints); i++)
352
+	{
353
+		UENUM = i;
354
+		UECONX = 1;
355
+		UECFG0X = pgm_read_byte(_initEndpoints+i);
356
+		UECFG1X = EP_DOUBLE_64;
357
+	}
358
+	UERST = 0x7E;	// And reset them
359
+	UERST = 0;
360
+}
361
+
362
+//	Handle CLASS_INTERFACE requests
363
+static
364
+bool ClassInterfaceRequest(Setup& setup)
365
+{
366
+	u8 i = setup.wIndex;
367
+
368
+#ifdef CDC_ENABLED
369
+	if (CDC_ACM_INTERFACE == i)
370
+		return CDC_Setup(setup);
371
+#endif
372
+
373
+#ifdef HID_ENABLED
374
+	if (HID_INTERFACE == i)
375
+		return HID_Setup(setup);
376
+#endif
377
+	return false;
378
+}
379
+
380
+int _cmark;
381
+int _cend;
382
+void InitControl(int end)
383
+{
384
+	SetEP(0);
385
+	_cmark = 0;
386
+	_cend = end;
387
+}
388
+
389
+static
390
+bool SendControl(u8 d)
391
+{
392
+	if (_cmark < _cend)
393
+	{
394
+		if (!WaitForINOrOUT())
395
+			return false;
396
+		Send8(d);
397
+		if (!((_cmark + 1) & 0x3F))
398
+			ClearIN();	// Fifo is full, release this packet
399
+	}
400
+	_cmark++;
401
+	return true;
402
+};
403
+
404
+//	Clipped by _cmark/_cend
405
+int USB_SendControl(u8 flags, const void* d, int len)
406
+{
407
+	int sent = len;
408
+	const u8* data = (const u8*)d;
409
+	bool pgm = flags & TRANSFER_PGM;
410
+	while (len--)
411
+	{
412
+		u8 c = pgm ? pgm_read_byte(data++) : *data++;
413
+		if (!SendControl(c))
414
+			return -1;
415
+	}
416
+	return sent;
417
+}
418
+
419
+// Send a USB descriptor string. The string is stored in PROGMEM as a
420
+// plain ASCII string but is sent out as UTF-16 with the correct 2-byte
421
+// prefix
422
+static bool USB_SendStringDescriptor(const u8*string_P, u8 string_len) {
423
+        SendControl(2 + string_len * 2);
424
+        SendControl(3);
425
+        for(u8 i = 0; i < string_len; i++) {
426
+                bool r = SendControl(pgm_read_byte(&string_P[i]));
427
+                r &= SendControl(0); // high byte
428
+                if(!r) {
429
+                        return false;
430
+                }
431
+        }
432
+        return true;
433
+}
434
+
435
+//	Does not timeout or cross fifo boundaries
436
+//	Will only work for transfers <= 64 bytes
437
+//	TODO
438
+int USB_RecvControl(void* d, int len)
439
+{
440
+	WaitOUT();
441
+	Recv((u8*)d,len);
442
+	ClearOUT();
443
+	return len;
444
+}
445
+
446
+int SendInterfaces()
447
+{
448
+	int total = 0;
449
+	u8 interfaces = 0;
450
+
451
+#ifdef CDC_ENABLED
452
+	total = CDC_GetInterface(&interfaces);
453
+#endif
454
+
455
+#ifdef HID_ENABLED
456
+	total += HID_GetInterface(&interfaces);
457
+#endif
458
+
459
+	return interfaces;
460
+}
461
+
462
+//	Construct a dynamic configuration descriptor
463
+//	This really needs dynamic endpoint allocation etc
464
+//	TODO
465
+static
466
+bool SendConfiguration(int maxlen)
467
+{
468
+	//	Count and measure interfaces
469
+	InitControl(0);	
470
+	int interfaces = SendInterfaces();
471
+	ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
472
+
473
+	//	Now send them
474
+	InitControl(maxlen);
475
+	USB_SendControl(0,&config,sizeof(ConfigDescriptor));
476
+	SendInterfaces();
477
+	return true;
478
+}
479
+
480
+u8 _cdcComposite = 0;
481
+
482
+static
483
+bool SendDescriptor(Setup& setup)
484
+{
485
+	u8 t = setup.wValueH;
486
+	if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
487
+		return SendConfiguration(setup.wLength);
488
+
489
+	InitControl(setup.wLength);
490
+#ifdef HID_ENABLED
491
+	if (HID_REPORT_DESCRIPTOR_TYPE == t)
492
+		return HID_GetDescriptor(t);
493
+#endif
494
+
495
+	const u8* desc_addr = 0;
496
+	if (USB_DEVICE_DESCRIPTOR_TYPE == t)
497
+	{
498
+		if (setup.wLength == 8)
499
+			_cdcComposite = 1;
500
+		desc_addr = _cdcComposite ?  (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor;
501
+	}
502
+	else if (USB_STRING_DESCRIPTOR_TYPE == t)
503
+	{
504
+		if (setup.wValueL == 0) {
505
+			desc_addr = (const u8*)&STRING_LANGUAGE;
506
+		}
507
+		else if (setup.wValueL == IPRODUCT) {
508
+			return USB_SendStringDescriptor(STRING_PRODUCT, strlen(USB_PRODUCT));
509
+		}
510
+		else if (setup.wValueL == IMANUFACTURER) {
511
+			return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER));
512
+		}
513
+		else
514
+			return false;
515
+	}
516
+
517
+	if (desc_addr == 0)
518
+		return false;
519
+	u8 desc_length = pgm_read_byte(desc_addr);
520
+
521
+	USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
522
+	return true;
523
+}
524
+
525
+//	Endpoint 0 interrupt
526
+ISR(USB_COM_vect)
527
+{
528
+    SetEP(0);
529
+	if (!ReceivedSetupInt())
530
+		return;
531
+
532
+	Setup setup;
533
+	Recv((u8*)&setup,8);
534
+	ClearSetupInt();
535
+
536
+	u8 requestType = setup.bmRequestType;
537
+	if (requestType & REQUEST_DEVICETOHOST)
538
+		WaitIN();
539
+	else
540
+		ClearIN();
541
+
542
+    bool ok = true;
543
+	if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
544
+	{
545
+		//	Standard Requests
546
+		u8 r = setup.bRequest;
547
+		if (GET_STATUS == r)
548
+		{
549
+			Send8(0);		// TODO
550
+			Send8(0);
551
+		}
552
+		else if (CLEAR_FEATURE == r)
553
+		{
554
+		}
555
+		else if (SET_FEATURE == r)
556
+		{
557
+		}
558
+		else if (SET_ADDRESS == r)
559
+		{
560
+			WaitIN();
561
+			UDADDR = setup.wValueL | (1<<ADDEN);
562
+		}
563
+		else if (GET_DESCRIPTOR == r)
564
+		{
565
+			ok = SendDescriptor(setup);
566
+		}
567
+		else if (SET_DESCRIPTOR == r)
568
+		{
569
+			ok = false;
570
+		}
571
+		else if (GET_CONFIGURATION == r)
572
+		{
573
+			Send8(1);
574
+		}
575
+		else if (SET_CONFIGURATION == r)
576
+		{
577
+			if (REQUEST_DEVICE == (requestType & REQUEST_RECIPIENT))
578
+			{
579
+				InitEndpoints();
580
+				_usbConfiguration = setup.wValueL;
581
+			} else
582
+				ok = false;
583
+		}
584
+		else if (GET_INTERFACE == r)
585
+		{
586
+		}
587
+		else if (SET_INTERFACE == r)
588
+		{
589
+		}
590
+	}
591
+	else
592
+	{
593
+		InitControl(setup.wLength);		//	Max length of transfer
594
+		ok = ClassInterfaceRequest(setup);
595
+	}
596
+
597
+	if (ok)
598
+		ClearIN();
599
+	else
600
+	{
601
+		Stall();
602
+	}
603
+}
604
+
605
+void USB_Flush(u8 ep)
606
+{
607
+	SetEP(ep);
608
+	if (FifoByteCount())
609
+		ReleaseTX();
610
+}
611
+
612
+//	General interrupt
613
+ISR(USB_GEN_vect)
614
+{
615
+	u8 udint = UDINT;
616
+	UDINT = 0;
617
+
618
+	//	End of Reset
619
+	if (udint & (1<<EORSTI))
620
+	{
621
+		InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64);	// init ep0
622
+		_usbConfiguration = 0;			// not configured yet
623
+		UEIENX = 1 << RXSTPE;			// Enable interrupts for ep0
624
+	}
625
+
626
+	//	Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
627
+	if (udint & (1<<SOFI))
628
+	{
629
+#ifdef CDC_ENABLED
630
+		USB_Flush(CDC_TX);				// Send a tx frame if found
631
+#endif
632
+		
633
+		// check whether the one-shot period has elapsed.  if so, turn off the LED
634
+		if (TxLEDPulse && !(--TxLEDPulse))
635
+			TXLED0;
636
+		if (RxLEDPulse && !(--RxLEDPulse))
637
+			RXLED0;
638
+	}
639
+}
640
+
641
+//	VBUS or counting frames
642
+//	Any frame counting?
643
+u8 USBConnected()
644
+{
645
+	u8 f = UDFNUML;
646
+	delay(3);
647
+	return f != UDFNUML;
648
+}
649
+
650
+//=======================================================================
651
+//=======================================================================
652
+
653
+USBDevice_ USBDevice;
654
+
655
+USBDevice_::USBDevice_()
656
+{
657
+}
658
+
659
+void USBDevice_::attach()
660
+{
661
+	_usbConfiguration = 0;
662
+	UHWCON = 0x01;						// power internal reg
663
+	USBCON = (1<<USBE)|(1<<FRZCLK);		// clock frozen, usb enabled
664
+#if F_CPU == 16000000UL
665
+	PLLCSR = 0x12;						// Need 16 MHz xtal
666
+#elif F_CPU == 8000000UL
667
+	PLLCSR = 0x02;						// Need 8 MHz xtal
668
+#endif
669
+	while (!(PLLCSR & (1<<PLOCK)))		// wait for lock pll
670
+		;
671
+
672
+	// Some tests on specific versions of macosx (10.7.3), reported some
673
+	// strange behaviuors when the board is reset using the serial
674
+	// port touch at 1200 bps. This delay fixes this behaviour.
675
+	delay(1);
676
+
677
+	USBCON = ((1<<USBE)|(1<<OTGPADE));	// start USB clock
678
+	UDIEN = (1<<EORSTE)|(1<<SOFE);		// Enable interrupts for EOR (End of Reset) and SOF (start of frame)
679
+	UDCON = 0;							// enable attach resistor
680
+	
681
+	TX_RX_LED_INIT;
682
+}
683
+
684
+void USBDevice_::detach()
685
+{
686
+}
687
+
688
+//	Check for interrupts
689
+//	TODO: VBUS detection
690
+bool USBDevice_::configured()
691
+{
692
+	return _usbConfiguration;
693
+}
694
+
695
+void USBDevice_::poll()
696
+{
697
+}
698
+
699
+#endif /* if defined(USBCON) */

+ 303
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/USBCore.h View File

@@ -0,0 +1,303 @@
1
+
2
+// Copyright (c) 2010, Peter Barrett 
3
+/*
4
+** Permission to use, copy, modify, and/or distribute this software for  
5
+** any purpose with or without fee is hereby granted, provided that the  
6
+** above copyright notice and this permission notice appear in all copies.  
7
+**  
8
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL  
9
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED  
10
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR  
11
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES  
12
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  
13
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  
14
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS  
15
+** SOFTWARE.  
16
+*/
17
+
18
+#ifndef __USBCORE_H__
19
+#define __USBCORE_H__
20
+
21
+//	Standard requests
22
+#define GET_STATUS			0
23
+#define CLEAR_FEATURE		1
24
+#define SET_FEATURE			3
25
+#define SET_ADDRESS			5
26
+#define GET_DESCRIPTOR		6
27
+#define SET_DESCRIPTOR		7
28
+#define GET_CONFIGURATION	8
29
+#define SET_CONFIGURATION	9
30
+#define GET_INTERFACE		10
31
+#define SET_INTERFACE		11
32
+
33
+
34
+// bmRequestType
35
+#define REQUEST_HOSTTODEVICE	0x00
36
+#define REQUEST_DEVICETOHOST	0x80
37
+#define REQUEST_DIRECTION		0x80
38
+
39
+#define REQUEST_STANDARD		0x00
40
+#define REQUEST_CLASS			0x20
41
+#define REQUEST_VENDOR			0x40
42
+#define REQUEST_TYPE			0x60
43
+
44
+#define REQUEST_DEVICE			0x00
45
+#define REQUEST_INTERFACE		0x01
46
+#define REQUEST_ENDPOINT		0x02
47
+#define REQUEST_OTHER			0x03
48
+#define REQUEST_RECIPIENT		0x03
49
+
50
+#define REQUEST_DEVICETOHOST_CLASS_INTERFACE  (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE)
51
+#define REQUEST_HOSTTODEVICE_CLASS_INTERFACE  (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE)
52
+
53
+//	Class requests
54
+
55
+#define CDC_SET_LINE_CODING			0x20
56
+#define CDC_GET_LINE_CODING			0x21
57
+#define CDC_SET_CONTROL_LINE_STATE	0x22
58
+
59
+#define MSC_RESET					0xFF
60
+#define MSC_GET_MAX_LUN				0xFE
61
+
62
+#define HID_GET_REPORT				0x01
63
+#define HID_GET_IDLE				0x02
64
+#define HID_GET_PROTOCOL			0x03
65
+#define HID_SET_REPORT				0x09
66
+#define HID_SET_IDLE				0x0A
67
+#define HID_SET_PROTOCOL			0x0B
68
+
69
+//	Descriptors
70
+
71
+#define USB_DEVICE_DESC_SIZE 18
72
+#define USB_CONFIGUARTION_DESC_SIZE 9
73
+#define USB_INTERFACE_DESC_SIZE 9
74
+#define USB_ENDPOINT_DESC_SIZE 7
75
+
76
+#define USB_DEVICE_DESCRIPTOR_TYPE             1
77
+#define USB_CONFIGURATION_DESCRIPTOR_TYPE      2
78
+#define USB_STRING_DESCRIPTOR_TYPE             3
79
+#define USB_INTERFACE_DESCRIPTOR_TYPE          4
80
+#define USB_ENDPOINT_DESCRIPTOR_TYPE           5
81
+
82
+#define USB_DEVICE_CLASS_COMMUNICATIONS        0x02
83
+#define USB_DEVICE_CLASS_HUMAN_INTERFACE       0x03
84
+#define USB_DEVICE_CLASS_STORAGE               0x08
85
+#define USB_DEVICE_CLASS_VENDOR_SPECIFIC       0xFF
86
+
87
+#define USB_CONFIG_POWERED_MASK                0x40
88
+#define USB_CONFIG_BUS_POWERED                 0x80
89
+#define USB_CONFIG_SELF_POWERED                0xC0
90
+#define USB_CONFIG_REMOTE_WAKEUP               0x20
91
+
92
+// bMaxPower in Configuration Descriptor
93
+#define USB_CONFIG_POWER_MA(mA)                ((mA)/2)
94
+
95
+// bEndpointAddress in Endpoint Descriptor
96
+#define USB_ENDPOINT_DIRECTION_MASK            0x80
97
+#define USB_ENDPOINT_OUT(addr)                 ((addr) | 0x00)
98
+#define USB_ENDPOINT_IN(addr)                  ((addr) | 0x80)
99
+
100
+#define USB_ENDPOINT_TYPE_MASK                 0x03
101
+#define USB_ENDPOINT_TYPE_CONTROL              0x00
102
+#define USB_ENDPOINT_TYPE_ISOCHRONOUS          0x01
103
+#define USB_ENDPOINT_TYPE_BULK                 0x02
104
+#define USB_ENDPOINT_TYPE_INTERRUPT            0x03
105
+
106
+#define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
107
+
108
+#define CDC_V1_10                               0x0110
109
+#define CDC_COMMUNICATION_INTERFACE_CLASS       0x02
110
+
111
+#define CDC_CALL_MANAGEMENT                     0x01
112
+#define CDC_ABSTRACT_CONTROL_MODEL              0x02
113
+#define CDC_HEADER                              0x00
114
+#define CDC_ABSTRACT_CONTROL_MANAGEMENT         0x02
115
+#define CDC_UNION                               0x06
116
+#define CDC_CS_INTERFACE                        0x24
117
+#define CDC_CS_ENDPOINT                         0x25
118
+#define CDC_DATA_INTERFACE_CLASS                0x0A
119
+
120
+#define MSC_SUBCLASS_SCSI						0x06 
121
+#define MSC_PROTOCOL_BULK_ONLY					0x50 
122
+
123
+#define HID_HID_DESCRIPTOR_TYPE					0x21
124
+#define HID_REPORT_DESCRIPTOR_TYPE				0x22
125
+#define HID_PHYSICAL_DESCRIPTOR_TYPE			0x23
126
+
127
+
128
+//	Device
129
+typedef struct {
130
+	u8 len;				// 18
131
+	u8 dtype;			// 1 USB_DEVICE_DESCRIPTOR_TYPE
132
+	u16 usbVersion;		// 0x200
133
+	u8	deviceClass;
134
+	u8	deviceSubClass;
135
+	u8	deviceProtocol;
136
+	u8	packetSize0;	// Packet 0
137
+	u16	idVendor;
138
+	u16	idProduct;
139
+	u16	deviceVersion;	// 0x100
140
+	u8	iManufacturer;
141
+	u8	iProduct;
142
+	u8	iSerialNumber;
143
+	u8	bNumConfigurations;
144
+} DeviceDescriptor;
145
+
146
+//	Config
147
+typedef struct {
148
+	u8	len;			// 9
149
+	u8	dtype;			// 2
150
+	u16 clen;			// total length
151
+	u8	numInterfaces;
152
+	u8	config;
153
+	u8	iconfig;
154
+	u8	attributes;
155
+	u8	maxPower;
156
+} ConfigDescriptor;
157
+
158
+//	String
159
+
160
+//	Interface
161
+typedef struct
162
+{
163
+	u8 len;		// 9
164
+	u8 dtype;	// 4
165
+	u8 number;
166
+	u8 alternate;
167
+	u8 numEndpoints;
168
+	u8 interfaceClass;
169
+	u8 interfaceSubClass;
170
+	u8 protocol;
171
+	u8 iInterface;
172
+} InterfaceDescriptor;
173
+
174
+//	Endpoint
175
+typedef struct
176
+{
177
+	u8 len;		// 7
178
+	u8 dtype;	// 5
179
+	u8 addr;
180
+	u8 attr;
181
+	u16 packetSize;
182
+	u8 interval;
183
+} EndpointDescriptor;
184
+
185
+// Interface Association Descriptor
186
+// Used to bind 2 interfaces together in CDC compostite device
187
+typedef struct
188
+{
189
+	u8 len;				// 8
190
+	u8 dtype;			// 11
191
+	u8 firstInterface;
192
+	u8 interfaceCount;
193
+	u8 functionClass;
194
+	u8 funtionSubClass;
195
+	u8 functionProtocol;
196
+	u8 iInterface;
197
+} IADDescriptor;
198
+
199
+//	CDC CS interface descriptor
200
+typedef struct
201
+{
202
+	u8 len;		// 5
203
+	u8 dtype;	// 0x24
204
+	u8 subtype;
205
+	u8 d0;
206
+	u8 d1;
207
+} CDCCSInterfaceDescriptor;
208
+
209
+typedef struct
210
+{
211
+	u8 len;		// 4
212
+	u8 dtype;	// 0x24
213
+	u8 subtype;
214
+	u8 d0;
215
+} CDCCSInterfaceDescriptor4;
216
+
217
+typedef struct 
218
+{
219
+    u8	len;
220
+    u8 	dtype;		// 0x24
221
+    u8 	subtype;	// 1
222
+    u8 	bmCapabilities;
223
+    u8 	bDataInterface;
224
+} CMFunctionalDescriptor;
225
+	
226
+typedef struct 
227
+{
228
+    u8	len;
229
+    u8 	dtype;		// 0x24
230
+    u8 	subtype;	// 1
231
+    u8 	bmCapabilities;
232
+} ACMFunctionalDescriptor;
233
+
234
+typedef struct 
235
+{
236
+	//	IAD
237
+	IADDescriptor				iad;	// Only needed on compound device
238
+
239
+	//	Control
240
+	InterfaceDescriptor			cif;	// 
241
+	CDCCSInterfaceDescriptor	header;
242
+	CMFunctionalDescriptor		callManagement;			// Call Management
243
+	ACMFunctionalDescriptor		controlManagement;		// ACM
244
+	CDCCSInterfaceDescriptor	functionalDescriptor;	// CDC_UNION
245
+	EndpointDescriptor			cifin;
246
+
247
+	//	Data
248
+	InterfaceDescriptor			dif;
249
+	EndpointDescriptor			in;
250
+	EndpointDescriptor			out;
251
+} CDCDescriptor;
252
+
253
+typedef struct 
254
+{
255
+	InterfaceDescriptor			msc;
256
+	EndpointDescriptor			in;
257
+	EndpointDescriptor			out;
258
+} MSCDescriptor;
259
+
260
+typedef struct
261
+{
262
+	u8 len;			// 9
263
+	u8 dtype;		// 0x21
264
+	u8 addr;
265
+	u8	versionL;	// 0x101
266
+	u8	versionH;	// 0x101
267
+	u8	country;
268
+	u8	desctype;	// 0x22 report
269
+	u8	descLenL;
270
+	u8	descLenH;
271
+} HIDDescDescriptor;
272
+
273
+typedef struct 
274
+{
275
+	InterfaceDescriptor			hid;
276
+	HIDDescDescriptor			desc;
277
+	EndpointDescriptor			in;
278
+} HIDDescriptor;
279
+
280
+
281
+#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
282
+	{ 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
283
+
284
+#define D_CONFIG(_totalLength,_interfaces) \
285
+	{ 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) }
286
+
287
+#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
288
+	{ 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
289
+
290
+#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
291
+	{ 7, 5, _addr,_attr,_packetSize, _interval }
292
+
293
+#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
294
+	{ 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
295
+
296
+#define D_HIDREPORT(_descriptorLength) \
297
+	{ 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
298
+
299
+#define D_CDCCS(_subtype,_d0,_d1)	{ 5, 0x24, _subtype, _d0, _d1 }
300
+#define D_CDCCS4(_subtype,_d0)		{ 4, 0x24, _subtype, _d0 }
301
+
302
+
303
+#endif

+ 63
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/USBDesc.h View File

@@ -0,0 +1,63 @@
1
+
2
+
3
+/* Copyright (c) 2011, Peter Barrett  
4
+**  
5
+** Permission to use, copy, modify, and/or distribute this software for  
6
+** any purpose with or without fee is hereby granted, provided that the  
7
+** above copyright notice and this permission notice appear in all copies.  
8
+** 
9
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL  
10
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED  
11
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR  
12
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES  
13
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  
14
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  
15
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS  
16
+** SOFTWARE.  
17
+*/
18
+
19
+#define CDC_ENABLED
20
+#define HID_ENABLED
21
+
22
+
23
+#ifdef CDC_ENABLED
24
+#define CDC_INTERFACE_COUNT	2
25
+#define CDC_ENPOINT_COUNT	3
26
+#else
27
+#define CDC_INTERFACE_COUNT	0
28
+#define CDC_ENPOINT_COUNT	0
29
+#endif
30
+
31
+#ifdef HID_ENABLED
32
+#define HID_INTERFACE_COUNT	1
33
+#define HID_ENPOINT_COUNT	1
34
+#else
35
+#define HID_INTERFACE_COUNT	0
36
+#define HID_ENPOINT_COUNT	0
37
+#endif
38
+
39
+#define CDC_ACM_INTERFACE	0	// CDC ACM
40
+#define CDC_DATA_INTERFACE	1	// CDC Data
41
+#define CDC_FIRST_ENDPOINT	1
42
+#define CDC_ENDPOINT_ACM	(CDC_FIRST_ENDPOINT)							// CDC First
43
+#define CDC_ENDPOINT_OUT	(CDC_FIRST_ENDPOINT+1)
44
+#define CDC_ENDPOINT_IN		(CDC_FIRST_ENDPOINT+2)
45
+
46
+#define HID_INTERFACE		(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT)		// HID Interface
47
+#define HID_FIRST_ENDPOINT	(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT)
48
+#define HID_ENDPOINT_INT	(HID_FIRST_ENDPOINT)
49
+
50
+#define INTERFACE_COUNT		(MSC_INTERFACE + MSC_INTERFACE_COUNT)
51
+
52
+#ifdef CDC_ENABLED
53
+#define CDC_RX CDC_ENDPOINT_OUT
54
+#define CDC_TX CDC_ENDPOINT_IN
55
+#endif
56
+
57
+#ifdef HID_ENABLED
58
+#define HID_TX HID_ENDPOINT_INT
59
+#endif
60
+
61
+#define IMANUFACTURER	1
62
+#define IPRODUCT		2
63
+

+ 88
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/Udp.h View File

@@ -0,0 +1,88 @@
1
+/*
2
+ *  Udp.cpp: Library to send/receive UDP packets.
3
+ *
4
+ * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
5
+ * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
6
+ * might not happen often in practice, but in larger network topologies, a UDP
7
+ * packet can be received out of sequence. 
8
+ * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
9
+ * aware of it. Again, this may not be a concern in practice on small local networks.
10
+ * For more information, see http://www.cafeaulait.org/course/week12/35.html
11
+ *
12
+ * MIT License:
13
+ * Copyright (c) 2008 Bjoern Hartmann
14
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ * of this software and associated documentation files (the "Software"), to deal
16
+ * in the Software without restriction, including without limitation the rights
17
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ * copies of the Software, and to permit persons to whom the Software is
19
+ * furnished to do so, subject to the following conditions:
20
+ * 
21
+ * The above copyright notice and this permission notice shall be included in
22
+ * all copies or substantial portions of the Software.
23
+ * 
24
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
+ * THE SOFTWARE.
31
+ *
32
+ * bjoern@cs.stanford.edu 12/30/2008
33
+ */
34
+
35
+#ifndef udp_h
36
+#define udp_h
37
+
38
+#include <Stream.h>
39
+#include <IPAddress.h>
40
+
41
+class UDP : public Stream {
42
+
43
+public:
44
+  virtual uint8_t begin(uint16_t) =0;	// initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
45
+  virtual void stop() =0;  // Finish with the UDP socket
46
+
47
+  // Sending UDP packets
48
+  
49
+  // Start building up a packet to send to the remote host specific in ip and port
50
+  // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
51
+  virtual int beginPacket(IPAddress ip, uint16_t port) =0;
52
+  // Start building up a packet to send to the remote host specific in host and port
53
+  // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
54
+  virtual int beginPacket(const char *host, uint16_t port) =0;
55
+  // Finish off this packet and send it
56
+  // Returns 1 if the packet was sent successfully, 0 if there was an error
57
+  virtual int endPacket() =0;
58
+  // Write a single byte into the packet
59
+  virtual size_t write(uint8_t) =0;
60
+  // Write size bytes from buffer into the packet
61
+  virtual size_t write(const uint8_t *buffer, size_t size) =0;
62
+
63
+  // Start processing the next available incoming packet
64
+  // Returns the size of the packet in bytes, or 0 if no packets are available
65
+  virtual int parsePacket() =0;
66
+  // Number of bytes remaining in the current packet
67
+  virtual int available() =0;
68
+  // Read a single byte from the current packet
69
+  virtual int read() =0;
70
+  // Read up to len bytes from the current packet and place them into buffer
71
+  // Returns the number of bytes read, or 0 if none are available
72
+  virtual int read(unsigned char* buffer, size_t len) =0;
73
+  // Read up to len characters from the current packet and place them into buffer
74
+  // Returns the number of characters read, or 0 if none are available
75
+  virtual int read(char* buffer, size_t len) =0;
76
+  // Return the next byte from the current packet without moving on to the next byte
77
+  virtual int peek() =0;
78
+  virtual void flush() =0;	// Finish reading the current packet
79
+
80
+  // Return the IP address of the host who sent the current incoming packet
81
+  virtual IPAddress remoteIP() =0;
82
+  // Return the port of the host who sent the current incoming packet
83
+  virtual uint16_t remotePort() =0;
84
+protected:
85
+  uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
86
+};
87
+
88
+#endif

+ 168
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WCharacter.h View File

@@ -0,0 +1,168 @@
1
+/*
2
+ WCharacter.h - Character utility functions for Wiring & Arduino
3
+ Copyright (c) 2010 Hernando Barragan.  All right reserved.
4
+ 
5
+ This library is free software; you can redistribute it and/or
6
+ modify it under the terms of the GNU Lesser General Public
7
+ License as published by the Free Software Foundation; either
8
+ version 2.1 of the License, or (at your option) any later version.
9
+ 
10
+ This library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ Lesser General Public License for more details.
14
+ 
15
+ You should have received a copy of the GNU Lesser General Public
16
+ License along with this library; if not, write to the Free Software
17
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+ */
19
+
20
+#ifndef Character_h
21
+#define Character_h
22
+
23
+#include <ctype.h>
24
+
25
+// WCharacter.h prototypes
26
+inline boolean isAlphaNumeric(int c) __attribute__((always_inline));
27
+inline boolean isAlpha(int c) __attribute__((always_inline));
28
+inline boolean isAscii(int c) __attribute__((always_inline));
29
+inline boolean isWhitespace(int c) __attribute__((always_inline));
30
+inline boolean isControl(int c) __attribute__((always_inline));
31
+inline boolean isDigit(int c) __attribute__((always_inline));
32
+inline boolean isGraph(int c) __attribute__((always_inline));
33
+inline boolean isLowerCase(int c) __attribute__((always_inline));
34
+inline boolean isPrintable(int c) __attribute__((always_inline));
35
+inline boolean isPunct(int c) __attribute__((always_inline));
36
+inline boolean isSpace(int c) __attribute__((always_inline));
37
+inline boolean isUpperCase(int c) __attribute__((always_inline));
38
+inline boolean isHexadecimalDigit(int c) __attribute__((always_inline));
39
+inline int toAscii(int c) __attribute__((always_inline));
40
+inline int toLowerCase(int c) __attribute__((always_inline));
41
+inline int toUpperCase(int c)__attribute__((always_inline));
42
+
43
+
44
+// Checks for an alphanumeric character. 
45
+// It is equivalent to (isalpha(c) || isdigit(c)).
46
+inline boolean isAlphaNumeric(int c) 
47
+{
48
+  return ( isalnum(c) == 0 ? false : true);
49
+}
50
+
51
+
52
+// Checks for an alphabetic character. 
53
+// It is equivalent to (isupper(c) || islower(c)).
54
+inline boolean isAlpha(int c)
55
+{
56
+  return ( isalpha(c) == 0 ? false : true);
57
+}
58
+
59
+
60
+// Checks whether c is a 7-bit unsigned char value 
61
+// that fits into the ASCII character set.
62
+inline boolean isAscii(int c)
63
+{
64
+  return ( isascii (c) == 0 ? false : true);
65
+}
66
+
67
+
68
+// Checks for a blank character, that is, a space or a tab.
69
+inline boolean isWhitespace(int c)
70
+{
71
+  return ( isblank (c) == 0 ? false : true);
72
+}
73
+
74
+
75
+// Checks for a control character.
76
+inline boolean isControl(int c)
77
+{
78
+  return ( iscntrl (c) == 0 ? false : true);
79
+}
80
+
81
+
82
+// Checks for a digit (0 through 9).
83
+inline boolean isDigit(int c)
84
+{
85
+  return ( isdigit (c) == 0 ? false : true);
86
+}
87
+
88
+
89
+// Checks for any printable character except space.
90
+inline boolean isGraph(int c)
91
+{
92
+  return ( isgraph (c) == 0 ? false : true);
93
+}
94
+
95
+
96
+// Checks for a lower-case character.
97
+inline boolean isLowerCase(int c)
98
+{
99
+  return (islower (c) == 0 ? false : true);
100
+}
101
+
102
+
103
+// Checks for any printable character including space.
104
+inline boolean isPrintable(int c)
105
+{
106
+  return ( isprint (c) == 0 ? false : true);
107
+}
108
+
109
+
110
+// Checks for any printable character which is not a space 
111
+// or an alphanumeric character.
112
+inline boolean isPunct(int c)
113
+{
114
+  return ( ispunct (c) == 0 ? false : true);
115
+}
116
+
117
+
118
+// Checks for white-space characters. For the avr-libc library, 
119
+// these are: space, formfeed ('\f'), newline ('\n'), carriage 
120
+// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
121
+inline boolean isSpace(int c)
122
+{
123
+  return ( isspace (c) == 0 ? false : true);
124
+}
125
+
126
+
127
+// Checks for an uppercase letter.
128
+inline boolean isUpperCase(int c)
129
+{
130
+  return ( isupper (c) == 0 ? false : true);
131
+}
132
+
133
+
134
+// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 
135
+// 8 9 a b c d e f A B C D E F.
136
+inline boolean isHexadecimalDigit(int c)
137
+{
138
+  return ( isxdigit (c) == 0 ? false : true);
139
+}
140
+
141
+
142
+// Converts c to a 7-bit unsigned char value that fits into the 
143
+// ASCII character set, by clearing the high-order bits.
144
+inline int toAscii(int c)
145
+{
146
+  return toascii (c);
147
+}
148
+
149
+
150
+// Warning:
151
+// Many people will be unhappy if you use this function. 
152
+// This function will convert accented letters into random 
153
+// characters.
154
+
155
+// Converts the letter c to lower case, if possible.
156
+inline int toLowerCase(int c)
157
+{
158
+  return tolower (c);
159
+}
160
+
161
+
162
+// Converts the letter c to upper case, if possible.
163
+inline int toUpperCase(int c)
164
+{
165
+  return toupper (c);
166
+}
167
+
168
+#endif

+ 334
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WInterrupts.c View File

@@ -0,0 +1,334 @@
1
+/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
3
+/*
4
+  Part of the Wiring project - http://wiring.uniandes.edu.co
5
+
6
+  Copyright (c) 2004-05 Hernando Barragan
7
+
8
+  This library is free software; you can redistribute it and/or
9
+  modify it under the terms of the GNU Lesser General Public
10
+  License as published by the Free Software Foundation; either
11
+  version 2.1 of the License, or (at your option) any later version.
12
+
13
+  This library is distributed in the hope that it will be useful,
14
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
+  Lesser General Public License for more details.
17
+
18
+  You should have received a copy of the GNU Lesser General
19
+  Public License along with this library; if not, write to the
20
+  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21
+  Boston, MA  02111-1307  USA
22
+  
23
+  Modified 24 November 2006 by David A. Mellis
24
+  Modified 1 August 2010 by Mark Sproul
25
+*/
26
+
27
+#include <inttypes.h>
28
+#include <avr/io.h>
29
+#include <avr/interrupt.h>
30
+#include <avr/pgmspace.h>
31
+#include <stdio.h>
32
+
33
+#include "wiring_private.h"
34
+
35
+static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
36
+// volatile static voidFuncPtr twiIntFunc;
37
+
38
+void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
39
+  if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
40
+    intFunc[interruptNum] = userFunc;
41
+    
42
+    // Configure the interrupt mode (trigger on low input, any change, rising
43
+    // edge, or falling edge).  The mode constants were chosen to correspond
44
+    // to the configuration bits in the hardware register, so we simply shift
45
+    // the mode into place.
46
+      
47
+    // Enable the interrupt.
48
+      
49
+    switch (interruptNum) {
50
+#if defined(__AVR_ATmega32U4__)
51
+	// I hate doing this, but the register assignment differs between the 1280/2560
52
+	// and the 32U4.  Since avrlib defines registers PCMSK1 and PCMSK2 that aren't 
53
+	// even present on the 32U4 this is the only way to distinguish between them.
54
+    case 0:
55
+	EICRA = (EICRA & ~((1<<ISC00) | (1<<ISC01))) | (mode << ISC00);
56
+	EIMSK |= (1<<INT0);
57
+	break;
58
+    case 1:
59
+	EICRA = (EICRA & ~((1<<ISC10) | (1<<ISC11))) | (mode << ISC10);
60
+	EIMSK |= (1<<INT1);
61
+	break;	
62
+    case 2:
63
+        EICRA = (EICRA & ~((1<<ISC20) | (1<<ISC21))) | (mode << ISC20);
64
+        EIMSK |= (1<<INT2);
65
+        break;
66
+    case 3:
67
+        EICRA = (EICRA & ~((1<<ISC30) | (1<<ISC31))) | (mode << ISC30);
68
+        EIMSK |= (1<<INT3);
69
+        break;
70
+    case 4:
71
+        EICRB = (EICRB & ~((1<<ISC60) | (1<<ISC61))) | (mode << ISC60);
72
+        EIMSK |= (1<<INT6);
73
+        break;
74
+#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
75
+    case 2:
76
+      EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
77
+      EIMSK |= (1 << INT0);
78
+      break;
79
+    case 3:
80
+      EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
81
+      EIMSK |= (1 << INT1);
82
+      break;
83
+    case 4:
84
+      EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
85
+      EIMSK |= (1 << INT2);
86
+      break;
87
+    case 5:
88
+      EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
89
+      EIMSK |= (1 << INT3);
90
+      break;
91
+    case 0:
92
+      EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
93
+      EIMSK |= (1 << INT4);
94
+      break;
95
+    case 1:
96
+      EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
97
+      EIMSK |= (1 << INT5);
98
+      break;
99
+    case 6:
100
+      EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
101
+      EIMSK |= (1 << INT6);
102
+      break;
103
+    case 7:
104
+      EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
105
+      EIMSK |= (1 << INT7);
106
+      break;
107
+#else		
108
+    case 0:
109
+    #if defined(EICRA) && defined(ISC00) && defined(EIMSK)
110
+      EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
111
+      EIMSK |= (1 << INT0);
112
+    #elif defined(MCUCR) && defined(ISC00) && defined(GICR)
113
+      MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
114
+      GICR |= (1 << INT0);
115
+    #elif defined(MCUCR) && defined(ISC00) && defined(GIMSK)
116
+      MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
117
+      GIMSK |= (1 << INT0);
118
+    #else
119
+      #error attachInterrupt not finished for this CPU (case 0)
120
+    #endif
121
+      break;
122
+
123
+    case 1:
124
+    #if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK)
125
+      EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
126
+      EIMSK |= (1 << INT1);
127
+    #elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR)
128
+      MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
129
+      GICR |= (1 << INT1);
130
+    #elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK)
131
+      MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
132
+      GIMSK |= (1 << INT1);
133
+    #else
134
+      #warning attachInterrupt may need some more work for this cpu (case 1)
135
+    #endif
136
+      break;
137
+    
138
+    case 2:
139
+    #if defined(EICRA) && defined(ISC20) && defined(ISC21) && defined(EIMSK)
140
+      EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
141
+      EIMSK |= (1 << INT2);
142
+    #elif defined(MCUCR) && defined(ISC20) && defined(ISC21) && defined(GICR)
143
+      MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
144
+      GICR |= (1 << INT2);
145
+    #elif defined(MCUCR) && defined(ISC20) && defined(GIMSK) && defined(GIMSK)
146
+      MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
147
+      GIMSK |= (1 << INT2);
148
+    #endif
149
+      break;
150
+#endif
151
+    }
152
+  }
153
+}
154
+
155
+void detachInterrupt(uint8_t interruptNum) {
156
+  if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
157
+    // Disable the interrupt.  (We can't assume that interruptNum is equal
158
+    // to the number of the EIMSK bit to clear, as this isn't true on the 
159
+    // ATmega8.  There, INT0 is 6 and INT1 is 7.)
160
+    switch (interruptNum) {
161
+#if defined(__AVR_ATmega32U4__)
162
+    case 0:
163
+        EIMSK &= ~(1<<INT0);
164
+        break;
165
+    case 1:
166
+        EIMSK &= ~(1<<INT1);
167
+        break;
168
+    case 2:
169
+        EIMSK &= ~(1<<INT2);
170
+        break;
171
+    case 3:
172
+        EIMSK &= ~(1<<INT3);
173
+        break;	
174
+    case 4:
175
+        EIMSK &= ~(1<<INT6);
176
+        break;	
177
+#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
178
+    case 2:
179
+      EIMSK &= ~(1 << INT0);
180
+      break;
181
+    case 3:
182
+      EIMSK &= ~(1 << INT1);
183
+      break;
184
+    case 4:
185
+      EIMSK &= ~(1 << INT2);
186
+      break;
187
+    case 5:
188
+      EIMSK &= ~(1 << INT3);
189
+      break;
190
+    case 0:
191
+      EIMSK &= ~(1 << INT4);
192
+      break;
193
+    case 1:
194
+      EIMSK &= ~(1 << INT5);
195
+      break;
196
+    case 6:
197
+      EIMSK &= ~(1 << INT6);
198
+      break;
199
+    case 7:
200
+      EIMSK &= ~(1 << INT7);
201
+      break;
202
+#else
203
+    case 0:
204
+    #if defined(EIMSK) && defined(INT0)
205
+      EIMSK &= ~(1 << INT0);
206
+    #elif defined(GICR) && defined(ISC00)
207
+      GICR &= ~(1 << INT0); // atmega32
208
+    #elif defined(GIMSK) && defined(INT0)
209
+      GIMSK &= ~(1 << INT0);
210
+    #else
211
+      #error detachInterrupt not finished for this cpu
212
+    #endif
213
+      break;
214
+
215
+    case 1:
216
+    #if defined(EIMSK) && defined(INT1)
217
+      EIMSK &= ~(1 << INT1);
218
+    #elif defined(GICR) && defined(INT1)
219
+      GICR &= ~(1 << INT1); // atmega32
220
+    #elif defined(GIMSK) && defined(INT1)
221
+      GIMSK &= ~(1 << INT1);
222
+    #else
223
+      #warning detachInterrupt may need some more work for this cpu (case 1)
224
+    #endif
225
+      break;
226
+#endif
227
+    }
228
+      
229
+    intFunc[interruptNum] = 0;
230
+  }
231
+}
232
+
233
+/*
234
+void attachInterruptTwi(void (*userFunc)(void) ) {
235
+  twiIntFunc = userFunc;
236
+}
237
+*/
238
+
239
+#if defined(__AVR_ATmega32U4__)
240
+ISR(INT0_vect) {
241
+	if(intFunc[EXTERNAL_INT_0])
242
+		intFunc[EXTERNAL_INT_0]();
243
+}
244
+
245
+ISR(INT1_vect) {
246
+	if(intFunc[EXTERNAL_INT_1])
247
+		intFunc[EXTERNAL_INT_1]();
248
+}
249
+
250
+ISR(INT2_vect) {
251
+    if(intFunc[EXTERNAL_INT_2])
252
+		intFunc[EXTERNAL_INT_2]();
253
+}
254
+
255
+ISR(INT3_vect) {
256
+    if(intFunc[EXTERNAL_INT_3])
257
+		intFunc[EXTERNAL_INT_3]();
258
+}
259
+
260
+ISR(INT6_vect) {
261
+    if(intFunc[EXTERNAL_INT_4])
262
+		intFunc[EXTERNAL_INT_4]();
263
+}
264
+
265
+#elif defined(EICRA) && defined(EICRB)
266
+
267
+ISR(INT0_vect) {
268
+  if(intFunc[EXTERNAL_INT_2])
269
+    intFunc[EXTERNAL_INT_2]();
270
+}
271
+
272
+ISR(INT1_vect) {
273
+  if(intFunc[EXTERNAL_INT_3])
274
+    intFunc[EXTERNAL_INT_3]();
275
+}
276
+
277
+ISR(INT2_vect) {
278
+  if(intFunc[EXTERNAL_INT_4])
279
+    intFunc[EXTERNAL_INT_4]();
280
+}
281
+
282
+ISR(INT3_vect) {
283
+  if(intFunc[EXTERNAL_INT_5])
284
+    intFunc[EXTERNAL_INT_5]();
285
+}
286
+
287
+ISR(INT4_vect) {
288
+  if(intFunc[EXTERNAL_INT_0])
289
+    intFunc[EXTERNAL_INT_0]();
290
+}
291
+
292
+ISR(INT5_vect) {
293
+  if(intFunc[EXTERNAL_INT_1])
294
+    intFunc[EXTERNAL_INT_1]();
295
+}
296
+
297
+ISR(INT6_vect) {
298
+  if(intFunc[EXTERNAL_INT_6])
299
+    intFunc[EXTERNAL_INT_6]();
300
+}
301
+
302
+ISR(INT7_vect) {
303
+  if(intFunc[EXTERNAL_INT_7])
304
+    intFunc[EXTERNAL_INT_7]();
305
+}
306
+
307
+#else
308
+
309
+ISR(INT0_vect) {
310
+  if(intFunc[EXTERNAL_INT_0])
311
+    intFunc[EXTERNAL_INT_0]();
312
+}
313
+
314
+ISR(INT1_vect) {
315
+  if(intFunc[EXTERNAL_INT_1])
316
+    intFunc[EXTERNAL_INT_1]();
317
+}
318
+
319
+#if defined(EICRA) && defined(ISC20)
320
+ISR(INT2_vect) {
321
+  if(intFunc[EXTERNAL_INT_2])
322
+    intFunc[EXTERNAL_INT_2]();
323
+}
324
+#endif
325
+
326
+#endif
327
+
328
+/*
329
+ISR(TWI_vect) {
330
+  if(twiIntFunc)
331
+    twiIntFunc();
332
+}
333
+*/
334
+

+ 60
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WMath.cpp View File

@@ -0,0 +1,60 @@
1
+/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
3
+/*
4
+  Part of the Wiring project - http://wiring.org.co
5
+  Copyright (c) 2004-06 Hernando Barragan
6
+  Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
7
+  
8
+  This library is free software; you can redistribute it and/or
9
+  modify it under the terms of the GNU Lesser General Public
10
+  License as published by the Free Software Foundation; either
11
+  version 2.1 of the License, or (at your option) any later version.
12
+
13
+  This library is distributed in the hope that it will be useful,
14
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
+  Lesser General Public License for more details.
17
+
18
+  You should have received a copy of the GNU Lesser General
19
+  Public License along with this library; if not, write to the
20
+  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21
+  Boston, MA  02111-1307  USA
22
+  
23
+  $Id$
24
+*/
25
+
26
+extern "C" {
27
+  #include "stdlib.h"
28
+}
29
+
30
+void randomSeed(unsigned int seed)
31
+{
32
+  if (seed != 0) {
33
+    srandom(seed);
34
+  }
35
+}
36
+
37
+long random(long howbig)
38
+{
39
+  if (howbig == 0) {
40
+    return 0;
41
+  }
42
+  return random() % howbig;
43
+}
44
+
45
+long random(long howsmall, long howbig)
46
+{
47
+  if (howsmall >= howbig) {
48
+    return howsmall;
49
+  }
50
+  long diff = howbig - howsmall;
51
+  return random(diff) + howsmall;
52
+}
53
+
54
+long map(long x, long in_min, long in_max, long out_min, long out_max)
55
+{
56
+  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
57
+}
58
+
59
+unsigned int makeWord(unsigned int w) { return w; }
60
+unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }

+ 745
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WString.cpp View File

@@ -0,0 +1,745 @@
1
+/*
2
+  WString.cpp - String library for Wiring & Arduino
3
+  ...mostly rewritten by Paul Stoffregen...
4
+  Copyright (c) 2009-10 Hernando Barragan.  All rights reserved.
5
+  Copyright 2011, Paul Stoffregen, paul@pjrc.com
6
+
7
+  This library is free software; you can redistribute it and/or
8
+  modify it under the terms of the GNU Lesser General Public
9
+  License as published by the Free Software Foundation; either
10
+  version 2.1 of the License, or (at your option) any later version.
11
+
12
+  This library is distributed in the hope that it will be useful,
13
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+  Lesser General Public License for more details.
16
+
17
+  You should have received a copy of the GNU Lesser General Public
18
+  License along with this library; if not, write to the Free Software
19
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
+*/
21
+
22
+#include "WString.h"
23
+
24
+/*********************************************/
25
+/*  Constructors                             */
26
+/*********************************************/
27
+
28
+String::String(const char *cstr)
29
+{
30
+	init();
31
+	if (cstr) copy(cstr, strlen(cstr));
32
+}
33
+
34
+String::String(const String &value)
35
+{
36
+	init();
37
+	*this = value;
38
+}
39
+
40
+String::String(const __FlashStringHelper *pstr)
41
+{
42
+	init();
43
+	*this = pstr;
44
+}
45
+
46
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
47
+String::String(String &&rval)
48
+{
49
+	init();
50
+	move(rval);
51
+}
52
+String::String(StringSumHelper &&rval)
53
+{
54
+	init();
55
+	move(rval);
56
+}
57
+#endif
58
+
59
+String::String(char c)
60
+{
61
+	init();
62
+	char buf[2];
63
+	buf[0] = c;
64
+	buf[1] = 0;
65
+	*this = buf;
66
+}
67
+
68
+String::String(unsigned char value, unsigned char base)
69
+{
70
+	init();
71
+	char buf[1 + 8 * sizeof(unsigned char)];
72
+	utoa(value, buf, base);
73
+	*this = buf;
74
+}
75
+
76
+String::String(int value, unsigned char base)
77
+{
78
+	init();
79
+	char buf[2 + 8 * sizeof(int)];
80
+	itoa(value, buf, base);
81
+	*this = buf;
82
+}
83
+
84
+String::String(unsigned int value, unsigned char base)
85
+{
86
+	init();
87
+	char buf[1 + 8 * sizeof(unsigned int)];
88
+	utoa(value, buf, base);
89
+	*this = buf;
90
+}
91
+
92
+String::String(long value, unsigned char base)
93
+{
94
+	init();
95
+	char buf[2 + 8 * sizeof(long)];
96
+	ltoa(value, buf, base);
97
+	*this = buf;
98
+}
99
+
100
+String::String(unsigned long value, unsigned char base)
101
+{
102
+	init();
103
+	char buf[1 + 8 * sizeof(unsigned long)];
104
+	ultoa(value, buf, base);
105
+	*this = buf;
106
+}
107
+
108
+String::String(float value, unsigned char decimalPlaces)
109
+{
110
+	init();
111
+	char buf[33];
112
+	*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
113
+}
114
+
115
+String::String(double value, unsigned char decimalPlaces)
116
+{
117
+	init();
118
+	char buf[33];
119
+	*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
120
+}
121
+
122
+String::~String()
123
+{
124
+	free(buffer);
125
+}
126
+
127
+/*********************************************/
128
+/*  Memory Management                        */
129
+/*********************************************/
130
+
131
+inline void String::init(void)
132
+{
133
+	buffer = NULL;
134
+	capacity = 0;
135
+	len = 0;
136
+}
137
+
138
+void String::invalidate(void)
139
+{
140
+	if (buffer) free(buffer);
141
+	buffer = NULL;
142
+	capacity = len = 0;
143
+}
144
+
145
+unsigned char String::reserve(unsigned int size)
146
+{
147
+	if (buffer && capacity >= size) return 1;
148
+	if (changeBuffer(size)) {
149
+		if (len == 0) buffer[0] = 0;
150
+		return 1;
151
+	}
152
+	return 0;
153
+}
154
+
155
+unsigned char String::changeBuffer(unsigned int maxStrLen)
156
+{
157
+	char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
158
+	if (newbuffer) {
159
+		buffer = newbuffer;
160
+		capacity = maxStrLen;
161
+		return 1;
162
+	}
163
+	return 0;
164
+}
165
+
166
+/*********************************************/
167
+/*  Copy and Move                            */
168
+/*********************************************/
169
+
170
+String & String::copy(const char *cstr, unsigned int length)
171
+{
172
+	if (!reserve(length)) {
173
+		invalidate();
174
+		return *this;
175
+	}
176
+	len = length;
177
+	strcpy(buffer, cstr);
178
+	return *this;
179
+}
180
+
181
+String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
182
+{
183
+	if (!reserve(length)) {
184
+		invalidate();
185
+		return *this;
186
+	}
187
+	len = length;
188
+	strcpy_P(buffer, (PGM_P)pstr);
189
+	return *this;
190
+}
191
+
192
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
193
+void String::move(String &rhs)
194
+{
195
+	if (buffer) {
196
+		if (capacity >= rhs.len) {
197
+			strcpy(buffer, rhs.buffer);
198
+			len = rhs.len;
199
+			rhs.len = 0;
200
+			return;
201
+		} else {
202
+			free(buffer);
203
+		}
204
+	}
205
+	buffer = rhs.buffer;
206
+	capacity = rhs.capacity;
207
+	len = rhs.len;
208
+	rhs.buffer = NULL;
209
+	rhs.capacity = 0;
210
+	rhs.len = 0;
211
+}
212
+#endif
213
+
214
+String & String::operator = (const String &rhs)
215
+{
216
+	if (this == &rhs) return *this;
217
+	
218
+	if (rhs.buffer) copy(rhs.buffer, rhs.len);
219
+	else invalidate();
220
+	
221
+	return *this;
222
+}
223
+
224
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
225
+String & String::operator = (String &&rval)
226
+{
227
+	if (this != &rval) move(rval);
228
+	return *this;
229
+}
230
+
231
+String & String::operator = (StringSumHelper &&rval)
232
+{
233
+	if (this != &rval) move(rval);
234
+	return *this;
235
+}
236
+#endif
237
+
238
+String & String::operator = (const char *cstr)
239
+{
240
+	if (cstr) copy(cstr, strlen(cstr));
241
+	else invalidate();
242
+	
243
+	return *this;
244
+}
245
+
246
+String & String::operator = (const __FlashStringHelper *pstr)
247
+{
248
+	if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
249
+	else invalidate();
250
+
251
+	return *this;
252
+}
253
+
254
+/*********************************************/
255
+/*  concat                                   */
256
+/*********************************************/
257
+
258
+unsigned char String::concat(const String &s)
259
+{
260
+	return concat(s.buffer, s.len);
261
+}
262
+
263
+unsigned char String::concat(const char *cstr, unsigned int length)
264
+{
265
+	unsigned int newlen = len + length;
266
+	if (!cstr) return 0;
267
+	if (length == 0) return 1;
268
+	if (!reserve(newlen)) return 0;
269
+	strcpy(buffer + len, cstr);
270
+	len = newlen;
271
+	return 1;
272
+}
273
+
274
+unsigned char String::concat(const char *cstr)
275
+{
276
+	if (!cstr) return 0;
277
+	return concat(cstr, strlen(cstr));
278
+}
279
+
280
+unsigned char String::concat(char c)
281
+{
282
+	char buf[2];
283
+	buf[0] = c;
284
+	buf[1] = 0;
285
+	return concat(buf, 1);
286
+}
287
+
288
+unsigned char String::concat(unsigned char num)
289
+{
290
+	char buf[1 + 3 * sizeof(unsigned char)];
291
+	itoa(num, buf, 10);
292
+	return concat(buf, strlen(buf));
293
+}
294
+
295
+unsigned char String::concat(int num)
296
+{
297
+	char buf[2 + 3 * sizeof(int)];
298
+	itoa(num, buf, 10);
299
+	return concat(buf, strlen(buf));
300
+}
301
+
302
+unsigned char String::concat(unsigned int num)
303
+{
304
+	char buf[1 + 3 * sizeof(unsigned int)];
305
+	utoa(num, buf, 10);
306
+	return concat(buf, strlen(buf));
307
+}
308
+
309
+unsigned char String::concat(long num)
310
+{
311
+	char buf[2 + 3 * sizeof(long)];
312
+	ltoa(num, buf, 10);
313
+	return concat(buf, strlen(buf));
314
+}
315
+
316
+unsigned char String::concat(unsigned long num)
317
+{
318
+	char buf[1 + 3 * sizeof(unsigned long)];
319
+	ultoa(num, buf, 10);
320
+	return concat(buf, strlen(buf));
321
+}
322
+
323
+unsigned char String::concat(float num)
324
+{
325
+	char buf[20];
326
+	char* string = dtostrf(num, 4, 2, buf);
327
+	return concat(string, strlen(string));
328
+}
329
+
330
+unsigned char String::concat(double num)
331
+{
332
+	char buf[20];
333
+	char* string = dtostrf(num, 4, 2, buf);
334
+	return concat(string, strlen(string));
335
+}
336
+
337
+unsigned char String::concat(const __FlashStringHelper * str)
338
+{
339
+	if (!str) return 0;
340
+	int length = strlen_P((const char *) str);
341
+	if (length == 0) return 1;
342
+	unsigned int newlen = len + length;
343
+	if (!reserve(newlen)) return 0;
344
+	strcpy_P(buffer + len, (const char *) str);
345
+	len = newlen;
346
+	return 1;
347
+}
348
+
349
+/*********************************************/
350
+/*  Concatenate                              */
351
+/*********************************************/
352
+
353
+StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
354
+{
355
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
356
+	if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
357
+	return a;
358
+}
359
+
360
+StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
361
+{
362
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
363
+	if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
364
+	return a;
365
+}
366
+
367
+StringSumHelper & operator + (const StringSumHelper &lhs, char c)
368
+{
369
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
370
+	if (!a.concat(c)) a.invalidate();
371
+	return a;
372
+}
373
+
374
+StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
375
+{
376
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
377
+	if (!a.concat(num)) a.invalidate();
378
+	return a;
379
+}
380
+
381
+StringSumHelper & operator + (const StringSumHelper &lhs, int num)
382
+{
383
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
384
+	if (!a.concat(num)) a.invalidate();
385
+	return a;
386
+}
387
+
388
+StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
389
+{
390
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
391
+	if (!a.concat(num)) a.invalidate();
392
+	return a;
393
+}
394
+
395
+StringSumHelper & operator + (const StringSumHelper &lhs, long num)
396
+{
397
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
398
+	if (!a.concat(num)) a.invalidate();
399
+	return a;
400
+}
401
+
402
+StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
403
+{
404
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
405
+	if (!a.concat(num)) a.invalidate();
406
+	return a;
407
+}
408
+
409
+StringSumHelper & operator + (const StringSumHelper &lhs, float num)
410
+{
411
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
412
+	if (!a.concat(num)) a.invalidate();
413
+	return a;
414
+}
415
+
416
+StringSumHelper & operator + (const StringSumHelper &lhs, double num)
417
+{
418
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
419
+	if (!a.concat(num)) a.invalidate();
420
+	return a;
421
+}
422
+
423
+StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
424
+{
425
+	StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
426
+	if (!a.concat(rhs))	a.invalidate();
427
+	return a;
428
+}
429
+
430
+/*********************************************/
431
+/*  Comparison                               */
432
+/*********************************************/
433
+
434
+int String::compareTo(const String &s) const
435
+{
436
+	if (!buffer || !s.buffer) {
437
+		if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
438
+		if (buffer && len > 0) return *(unsigned char *)buffer;
439
+		return 0;
440
+	}
441
+	return strcmp(buffer, s.buffer);
442
+}
443
+
444
+unsigned char String::equals(const String &s2) const
445
+{
446
+	return (len == s2.len && compareTo(s2) == 0);
447
+}
448
+
449
+unsigned char String::equals(const char *cstr) const
450
+{
451
+	if (len == 0) return (cstr == NULL || *cstr == 0);
452
+	if (cstr == NULL) return buffer[0] == 0;
453
+	return strcmp(buffer, cstr) == 0;
454
+}
455
+
456
+unsigned char String::operator<(const String &rhs) const
457
+{
458
+	return compareTo(rhs) < 0;
459
+}
460
+
461
+unsigned char String::operator>(const String &rhs) const
462
+{
463
+	return compareTo(rhs) > 0;
464
+}
465
+
466
+unsigned char String::operator<=(const String &rhs) const
467
+{
468
+	return compareTo(rhs) <= 0;
469
+}
470
+
471
+unsigned char String::operator>=(const String &rhs) const
472
+{
473
+	return compareTo(rhs) >= 0;
474
+}
475
+
476
+unsigned char String::equalsIgnoreCase( const String &s2 ) const
477
+{
478
+	if (this == &s2) return 1;
479
+	if (len != s2.len) return 0;
480
+	if (len == 0) return 1;
481
+	const char *p1 = buffer;
482
+	const char *p2 = s2.buffer;
483
+	while (*p1) {
484
+		if (tolower(*p1++) != tolower(*p2++)) return 0;
485
+	} 
486
+	return 1;
487
+}
488
+
489
+unsigned char String::startsWith( const String &s2 ) const
490
+{
491
+	if (len < s2.len) return 0;
492
+	return startsWith(s2, 0);
493
+}
494
+
495
+unsigned char String::startsWith( const String &s2, unsigned int offset ) const
496
+{
497
+	if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
498
+	return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
499
+}
500
+
501
+unsigned char String::endsWith( const String &s2 ) const
502
+{
503
+	if ( len < s2.len || !buffer || !s2.buffer) return 0;
504
+	return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
505
+}
506
+
507
+/*********************************************/
508
+/*  Character Access                         */
509
+/*********************************************/
510
+
511
+char String::charAt(unsigned int loc) const
512
+{
513
+	return operator[](loc);
514
+}
515
+
516
+void String::setCharAt(unsigned int loc, char c) 
517
+{
518
+	if (loc < len) buffer[loc] = c;
519
+}
520
+
521
+char & String::operator[](unsigned int index)
522
+{
523
+	static char dummy_writable_char;
524
+	if (index >= len || !buffer) {
525
+		dummy_writable_char = 0;
526
+		return dummy_writable_char;
527
+	}
528
+	return buffer[index];
529
+}
530
+
531
+char String::operator[]( unsigned int index ) const
532
+{
533
+	if (index >= len || !buffer) return 0;
534
+	return buffer[index];
535
+}
536
+
537
+void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
538
+{
539
+	if (!bufsize || !buf) return;
540
+	if (index >= len) {
541
+		buf[0] = 0;
542
+		return;
543
+	}
544
+	unsigned int n = bufsize - 1;
545
+	if (n > len - index) n = len - index;
546
+	strncpy((char *)buf, buffer + index, n);
547
+	buf[n] = 0;
548
+}
549
+
550
+/*********************************************/
551
+/*  Search                                   */
552
+/*********************************************/
553
+
554
+int String::indexOf(char c) const
555
+{
556
+	return indexOf(c, 0);
557
+}
558
+
559
+int String::indexOf( char ch, unsigned int fromIndex ) const
560
+{
561
+	if (fromIndex >= len) return -1;
562
+	const char* temp = strchr(buffer + fromIndex, ch);
563
+	if (temp == NULL) return -1;
564
+	return temp - buffer;
565
+}
566
+
567
+int String::indexOf(const String &s2) const
568
+{
569
+	return indexOf(s2, 0);
570
+}
571
+
572
+int String::indexOf(const String &s2, unsigned int fromIndex) const
573
+{
574
+	if (fromIndex >= len) return -1;
575
+	const char *found = strstr(buffer + fromIndex, s2.buffer);
576
+	if (found == NULL) return -1;
577
+	return found - buffer;
578
+}
579
+
580
+int String::lastIndexOf( char theChar ) const
581
+{
582
+	return lastIndexOf(theChar, len - 1);
583
+}
584
+
585
+int String::lastIndexOf(char ch, unsigned int fromIndex) const
586
+{
587
+	if (fromIndex >= len) return -1;
588
+	char tempchar = buffer[fromIndex + 1];
589
+	buffer[fromIndex + 1] = '\0';
590
+	char* temp = strrchr( buffer, ch );
591
+	buffer[fromIndex + 1] = tempchar;
592
+	if (temp == NULL) return -1;
593
+	return temp - buffer;
594
+}
595
+
596
+int String::lastIndexOf(const String &s2) const
597
+{
598
+	return lastIndexOf(s2, len - s2.len);
599
+}
600
+
601
+int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
602
+{
603
+  	if (s2.len == 0 || len == 0 || s2.len > len) return -1;
604
+	if (fromIndex >= len) fromIndex = len - 1;
605
+	int found = -1;
606
+	for (char *p = buffer; p <= buffer + fromIndex; p++) {
607
+		p = strstr(p, s2.buffer);
608
+		if (!p) break;
609
+		if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
610
+	}
611
+	return found;
612
+}
613
+
614
+String String::substring(unsigned int left, unsigned int right) const
615
+{
616
+	if (left > right) {
617
+		unsigned int temp = right;
618
+		right = left;
619
+		left = temp;
620
+	}
621
+	String out;
622
+	if (left >= len) return out;
623
+	if (right > len) right = len;
624
+	char temp = buffer[right];  // save the replaced character
625
+	buffer[right] = '\0';	
626
+	out = buffer + left;  // pointer arithmetic
627
+	buffer[right] = temp;  //restore character
628
+	return out;
629
+}
630
+
631
+/*********************************************/
632
+/*  Modification                             */
633
+/*********************************************/
634
+
635
+void String::replace(char find, char replace)
636
+{
637
+	if (!buffer) return;
638
+	for (char *p = buffer; *p; p++) {
639
+		if (*p == find) *p = replace;
640
+	}
641
+}
642
+
643
+void String::replace(const String& find, const String& replace)
644
+{
645
+	if (len == 0 || find.len == 0) return;
646
+	int diff = replace.len - find.len;
647
+	char *readFrom = buffer;
648
+	char *foundAt;
649
+	if (diff == 0) {
650
+		while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
651
+			memcpy(foundAt, replace.buffer, replace.len);
652
+			readFrom = foundAt + replace.len;
653
+		}
654
+	} else if (diff < 0) {
655
+		char *writeTo = buffer;
656
+		while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
657
+			unsigned int n = foundAt - readFrom;
658
+			memcpy(writeTo, readFrom, n);
659
+			writeTo += n;
660
+			memcpy(writeTo, replace.buffer, replace.len);
661
+			writeTo += replace.len;
662
+			readFrom = foundAt + find.len;
663
+			len += diff;
664
+		}
665
+		strcpy(writeTo, readFrom);
666
+	} else {
667
+		unsigned int size = len; // compute size needed for result
668
+		while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
669
+			readFrom = foundAt + find.len;
670
+			size += diff;
671
+		}
672
+		if (size == len) return;
673
+		if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
674
+		int index = len - 1;
675
+		while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
676
+			readFrom = buffer + index + find.len;
677
+			memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
678
+			len += diff;
679
+			buffer[len] = 0;
680
+			memcpy(buffer + index, replace.buffer, replace.len);
681
+			index--;
682
+		}
683
+	}
684
+}
685
+
686
+void String::remove(unsigned int index){
687
+	// Pass the biggest integer as the count. The remove method
688
+	// below will take care of truncating it at the end of the
689
+	// string.
690
+	remove(index, (unsigned int)-1);
691
+}
692
+
693
+void String::remove(unsigned int index, unsigned int count){
694
+	if (index >= len) { return; }
695
+	if (count <= 0) { return; }
696
+	if (count > len - index) { count = len - index; }
697
+	char *writeTo = buffer + index;
698
+	len = len - count;
699
+	strncpy(writeTo, buffer + index + count,len - index);
700
+	buffer[len] = 0;
701
+}
702
+
703
+void String::toLowerCase(void)
704
+{
705
+	if (!buffer) return;
706
+	for (char *p = buffer; *p; p++) {
707
+		*p = tolower(*p);
708
+	}
709
+}
710
+
711
+void String::toUpperCase(void)
712
+{
713
+	if (!buffer) return;
714
+	for (char *p = buffer; *p; p++) {
715
+		*p = toupper(*p);
716
+	}
717
+}
718
+
719
+void String::trim(void)
720
+{
721
+	if (!buffer || len == 0) return;
722
+	char *begin = buffer;
723
+	while (isspace(*begin)) begin++;
724
+	char *end = buffer + len - 1;
725
+	while (isspace(*end) && end >= begin) end--;
726
+	len = end + 1 - begin;
727
+	if (begin > buffer) memcpy(buffer, begin, len);
728
+	buffer[len] = 0;
729
+}
730
+
731
+/*********************************************/
732
+/*  Parsing / Conversion                     */
733
+/*********************************************/
734
+
735
+long String::toInt(void) const
736
+{
737
+	if (buffer) return atol(buffer);
738
+	return 0;
739
+}
740
+
741
+float String::toFloat(void) const
742
+{
743
+	if (buffer) return float(atof(buffer));
744
+	return 0;
745
+}

+ 224
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/WString.h View File

@@ -0,0 +1,224 @@
1
+/*
2
+  WString.h - String library for Wiring & Arduino
3
+  ...mostly rewritten by Paul Stoffregen...
4
+  Copyright (c) 2009-10 Hernando Barragan.  All right reserved.
5
+  Copyright 2011, Paul Stoffregen, paul@pjrc.com
6
+
7
+  This library is free software; you can redistribute it and/or
8
+  modify it under the terms of the GNU Lesser General Public
9
+  License as published by the Free Software Foundation; either
10
+  version 2.1 of the License, or (at your option) any later version.
11
+
12
+  This library is distributed in the hope that it will be useful,
13
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+  Lesser General Public License for more details.
16
+
17
+  You should have received a copy of the GNU Lesser General Public
18
+  License along with this library; if not, write to the Free Software
19
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
+*/
21
+
22
+#ifndef String_class_h
23
+#define String_class_h
24
+#ifdef __cplusplus
25
+
26
+#include <stdlib.h>
27
+#include <string.h>
28
+#include <ctype.h>
29
+#include <avr/pgmspace.h>
30
+
31
+// When compiling programs with this class, the following gcc parameters
32
+// dramatically increase performance and memory (RAM) efficiency, typically
33
+// with little or no increase in code size.
34
+//     -felide-constructors
35
+//     -std=c++0x
36
+
37
+class __FlashStringHelper;
38
+#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
39
+
40
+// An inherited class for holding the result of a concatenation.  These
41
+// result objects are assumed to be writable by subsequent concatenations.
42
+class StringSumHelper;
43
+
44
+// The string class
45
+class String
46
+{
47
+	// use a function pointer to allow for "if (s)" without the
48
+	// complications of an operator bool(). for more information, see:
49
+	// http://www.artima.com/cppsource/safebool.html
50
+	typedef void (String::*StringIfHelperType)() const;
51
+	void StringIfHelper() const {}
52
+
53
+public:
54
+	// constructors
55
+	// creates a copy of the initial value.
56
+	// if the initial value is null or invalid, or if memory allocation
57
+	// fails, the string will be marked as invalid (i.e. "if (s)" will
58
+	// be false).
59
+	String(const char *cstr = "");
60
+	String(const String &str);
61
+	String(const __FlashStringHelper *str);
62
+	#ifdef __GXX_EXPERIMENTAL_CXX0X__
63
+	String(String &&rval);
64
+	String(StringSumHelper &&rval);
65
+	#endif
66
+	explicit String(char c);
67
+	explicit String(unsigned char, unsigned char base=10);
68
+	explicit String(int, unsigned char base=10);
69
+	explicit String(unsigned int, unsigned char base=10);
70
+	explicit String(long, unsigned char base=10);
71
+	explicit String(unsigned long, unsigned char base=10);
72
+	explicit String(float, unsigned char decimalPlaces=2);
73
+	explicit String(double, unsigned char decimalPlaces=2);
74
+	~String(void);
75
+
76
+	// memory management
77
+	// return true on success, false on failure (in which case, the string
78
+	// is left unchanged).  reserve(0), if successful, will validate an
79
+	// invalid string (i.e., "if (s)" will be true afterwards)
80
+	unsigned char reserve(unsigned int size);
81
+	inline unsigned int length(void) const {return len;}
82
+
83
+	// creates a copy of the assigned value.  if the value is null or
84
+	// invalid, or if the memory allocation fails, the string will be 
85
+	// marked as invalid ("if (s)" will be false).
86
+	String & operator = (const String &rhs);
87
+	String & operator = (const char *cstr);
88
+	String & operator = (const __FlashStringHelper *str);
89
+	#ifdef __GXX_EXPERIMENTAL_CXX0X__
90
+	String & operator = (String &&rval);
91
+	String & operator = (StringSumHelper &&rval);
92
+	#endif
93
+
94
+	// concatenate (works w/ built-in types)
95
+	
96
+	// returns true on success, false on failure (in which case, the string
97
+	// is left unchanged).  if the argument is null or invalid, the 
98
+	// concatenation is considered unsucessful.  
99
+	unsigned char concat(const String &str);
100
+	unsigned char concat(const char *cstr);
101
+	unsigned char concat(char c);
102
+	unsigned char concat(unsigned char c);
103
+	unsigned char concat(int num);
104
+	unsigned char concat(unsigned int num);
105
+	unsigned char concat(long num);
106
+	unsigned char concat(unsigned long num);
107
+	unsigned char concat(float num);
108
+	unsigned char concat(double num);
109
+	unsigned char concat(const __FlashStringHelper * str);
110
+	
111
+	// if there's not enough memory for the concatenated value, the string
112
+	// will be left unchanged (but this isn't signalled in any way)
113
+	String & operator += (const String &rhs)	{concat(rhs); return (*this);}
114
+	String & operator += (const char *cstr)		{concat(cstr); return (*this);}
115
+	String & operator += (char c)			{concat(c); return (*this);}
116
+	String & operator += (unsigned char num)		{concat(num); return (*this);}
117
+	String & operator += (int num)			{concat(num); return (*this);}
118
+	String & operator += (unsigned int num)		{concat(num); return (*this);}
119
+	String & operator += (long num)			{concat(num); return (*this);}
120
+	String & operator += (unsigned long num)	{concat(num); return (*this);}
121
+	String & operator += (float num)		{concat(num); return (*this);}
122
+	String & operator += (double num)		{concat(num); return (*this);}
123
+	String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
124
+
125
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
126
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
127
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
128
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
129
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
130
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
131
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
132
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
133
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
134
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
135
+	friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
136
+
137
+	// comparison (only works w/ Strings and "strings")
138
+	operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
139
+	int compareTo(const String &s) const;
140
+	unsigned char equals(const String &s) const;
141
+	unsigned char equals(const char *cstr) const;
142
+	unsigned char operator == (const String &rhs) const {return equals(rhs);}
143
+	unsigned char operator == (const char *cstr) const {return equals(cstr);}
144
+	unsigned char operator != (const String &rhs) const {return !equals(rhs);}
145
+	unsigned char operator != (const char *cstr) const {return !equals(cstr);}
146
+	unsigned char operator <  (const String &rhs) const;
147
+	unsigned char operator >  (const String &rhs) const;
148
+	unsigned char operator <= (const String &rhs) const;
149
+	unsigned char operator >= (const String &rhs) const;
150
+	unsigned char equalsIgnoreCase(const String &s) const;
151
+	unsigned char startsWith( const String &prefix) const;
152
+	unsigned char startsWith(const String &prefix, unsigned int offset) const;
153
+	unsigned char endsWith(const String &suffix) const;
154
+
155
+	// character acccess
156
+	char charAt(unsigned int index) const;
157
+	void setCharAt(unsigned int index, char c);
158
+	char operator [] (unsigned int index) const;
159
+	char& operator [] (unsigned int index);
160
+	void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
161
+	void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
162
+		{getBytes((unsigned char *)buf, bufsize, index);}
163
+	const char * c_str() const { return buffer; }
164
+
165
+	// search
166
+	int indexOf( char ch ) const;
167
+	int indexOf( char ch, unsigned int fromIndex ) const;
168
+	int indexOf( const String &str ) const;
169
+	int indexOf( const String &str, unsigned int fromIndex ) const;
170
+	int lastIndexOf( char ch ) const;
171
+	int lastIndexOf( char ch, unsigned int fromIndex ) const;
172
+	int lastIndexOf( const String &str ) const;
173
+	int lastIndexOf( const String &str, unsigned int fromIndex ) const;
174
+	String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
175
+	String substring( unsigned int beginIndex, unsigned int endIndex ) const;
176
+
177
+	// modification
178
+	void replace(char find, char replace);
179
+	void replace(const String& find, const String& replace);
180
+	void remove(unsigned int index);
181
+	void remove(unsigned int index, unsigned int count);
182
+	void toLowerCase(void);
183
+	void toUpperCase(void);
184
+	void trim(void);
185
+
186
+	// parsing/conversion
187
+	long toInt(void) const;
188
+	float toFloat(void) const;
189
+
190
+protected:
191
+	char *buffer;	        // the actual char array
192
+	unsigned int capacity;  // the array length minus one (for the '\0')
193
+	unsigned int len;       // the String length (not counting the '\0')
194
+protected:
195
+	void init(void);
196
+	void invalidate(void);
197
+	unsigned char changeBuffer(unsigned int maxStrLen);
198
+	unsigned char concat(const char *cstr, unsigned int length);
199
+
200
+	// copy and move
201
+	String & copy(const char *cstr, unsigned int length);
202
+	String & copy(const __FlashStringHelper *pstr, unsigned int length);
203
+	#ifdef __GXX_EXPERIMENTAL_CXX0X__
204
+	void move(String &rhs);
205
+	#endif
206
+};
207
+
208
+class StringSumHelper : public String
209
+{
210
+public:
211
+	StringSumHelper(const String &s) : String(s) {}
212
+	StringSumHelper(const char *p) : String(p) {}
213
+	StringSumHelper(char c) : String(c) {}
214
+	StringSumHelper(unsigned char num) : String(num) {}
215
+	StringSumHelper(int num) : String(num) {}
216
+	StringSumHelper(unsigned int num) : String(num) {}
217
+	StringSumHelper(long num) : String(num) {}
218
+	StringSumHelper(unsigned long num) : String(num) {}
219
+	StringSumHelper(float num) : String(num) {}
220
+	StringSumHelper(double num) : String(num) {}
221
+};
222
+
223
+#endif  // __cplusplus
224
+#endif  // String_class_h

+ 35
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/abi.cpp View File

@@ -0,0 +1,35 @@
1
+/*
2
+  Copyright (c) 2014 Arduino.  All right reserved.
3
+
4
+  This library is free software; you can redistribute it and/or
5
+  modify it under the terms of the GNU Lesser General Public
6
+  License as published by the Free Software Foundation; either
7
+  version 2.1 of the License, or (at your option) any later version.
8
+
9
+  This library 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.
12
+  See the GNU Lesser General Public License for more details.
13
+
14
+  You should have received a copy of the GNU Lesser General Public
15
+  License along with this library; if not, write to the Free Software
16
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
+*/
18
+
19
+#include <stdlib.h>
20
+
21
+extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
22
+extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
23
+
24
+void __cxa_pure_virtual(void) {
25
+  // We might want to write some diagnostics to uart in this case
26
+  //std::terminate();
27
+  abort();
28
+}
29
+
30
+void __cxa_deleted_virtual(void) {
31
+  // We might want to write some diagnostics to uart in this case
32
+  //std::terminate();
33
+  abort();
34
+}
35
+

+ 534
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/binary.h View File

@@ -0,0 +1,534 @@
1
+/*
2
+  binary.h - Definitions for binary constants
3
+  Copyright (c) 2006 David A. Mellis.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef Binary_h
21
+#define Binary_h
22
+
23
+#define B0 0
24
+#define B00 0
25
+#define B000 0
26
+#define B0000 0
27
+#define B00000 0
28
+#define B000000 0
29
+#define B0000000 0
30
+#define B00000000 0
31
+#define B1 1
32
+#define B01 1
33
+#define B001 1
34
+#define B0001 1
35
+#define B00001 1
36
+#define B000001 1
37
+#define B0000001 1
38
+#define B00000001 1
39
+#define B10 2
40
+#define B010 2
41
+#define B0010 2
42
+#define B00010 2
43
+#define B000010 2
44
+#define B0000010 2
45
+#define B00000010 2
46
+#define B11 3
47
+#define B011 3
48
+#define B0011 3
49
+#define B00011 3
50
+#define B000011 3
51
+#define B0000011 3
52
+#define B00000011 3
53
+#define B100 4
54
+#define B0100 4
55
+#define B00100 4
56
+#define B000100 4
57
+#define B0000100 4
58
+#define B00000100 4
59
+#define B101 5
60
+#define B0101 5
61
+#define B00101 5
62
+#define B000101 5
63
+#define B0000101 5
64
+#define B00000101 5
65
+#define B110 6
66
+#define B0110 6
67
+#define B00110 6
68
+#define B000110 6
69
+#define B0000110 6
70
+#define B00000110 6
71
+#define B111 7
72
+#define B0111 7
73
+#define B00111 7
74
+#define B000111 7
75
+#define B0000111 7
76
+#define B00000111 7
77
+#define B1000 8
78
+#define B01000 8
79
+#define B001000 8
80
+#define B0001000 8
81
+#define B00001000 8
82
+#define B1001 9
83
+#define B01001 9
84
+#define B001001 9
85
+#define B0001001 9
86
+#define B00001001 9
87
+#define B1010 10
88
+#define B01010 10
89
+#define B001010 10
90
+#define B0001010 10
91
+#define B00001010 10
92
+#define B1011 11
93
+#define B01011 11
94
+#define B001011 11
95
+#define B0001011 11
96
+#define B00001011 11
97
+#define B1100 12
98
+#define B01100 12
99
+#define B001100 12
100
+#define B0001100 12
101
+#define B00001100 12
102
+#define B1101 13
103
+#define B01101 13
104
+#define B001101 13
105
+#define B0001101 13
106
+#define B00001101 13
107
+#define B1110 14
108
+#define B01110 14
109
+#define B001110 14
110
+#define B0001110 14
111
+#define B00001110 14
112
+#define B1111 15
113
+#define B01111 15
114
+#define B001111 15
115
+#define B0001111 15
116
+#define B00001111 15
117
+#define B10000 16
118
+#define B010000 16
119
+#define B0010000 16
120
+#define B00010000 16
121
+#define B10001 17
122
+#define B010001 17
123
+#define B0010001 17
124
+#define B00010001 17
125
+#define B10010 18
126
+#define B010010 18
127
+#define B0010010 18
128
+#define B00010010 18
129
+#define B10011 19
130
+#define B010011 19
131
+#define B0010011 19
132
+#define B00010011 19
133
+#define B10100 20
134
+#define B010100 20
135
+#define B0010100 20
136
+#define B00010100 20
137
+#define B10101 21
138
+#define B010101 21
139
+#define B0010101 21
140
+#define B00010101 21
141
+#define B10110 22
142
+#define B010110 22
143
+#define B0010110 22
144
+#define B00010110 22
145
+#define B10111 23
146
+#define B010111 23
147
+#define B0010111 23
148
+#define B00010111 23
149
+#define B11000 24
150
+#define B011000 24
151
+#define B0011000 24
152
+#define B00011000 24
153
+#define B11001 25
154
+#define B011001 25
155
+#define B0011001 25
156
+#define B00011001 25
157
+#define B11010 26
158
+#define B011010 26
159
+#define B0011010 26
160
+#define B00011010 26
161
+#define B11011 27
162
+#define B011011 27
163
+#define B0011011 27
164
+#define B00011011 27
165
+#define B11100 28
166
+#define B011100 28
167
+#define B0011100 28
168
+#define B00011100 28
169
+#define B11101 29
170
+#define B011101 29
171
+#define B0011101 29
172
+#define B00011101 29
173
+#define B11110 30
174
+#define B011110 30
175
+#define B0011110 30
176
+#define B00011110 30
177
+#define B11111 31
178
+#define B011111 31
179
+#define B0011111 31
180
+#define B00011111 31
181
+#define B100000 32
182
+#define B0100000 32
183
+#define B00100000 32
184
+#define B100001 33
185
+#define B0100001 33
186
+#define B00100001 33
187
+#define B100010 34
188
+#define B0100010 34
189
+#define B00100010 34
190
+#define B100011 35
191
+#define B0100011 35
192
+#define B00100011 35
193
+#define B100100 36
194
+#define B0100100 36
195
+#define B00100100 36
196
+#define B100101 37
197
+#define B0100101 37
198
+#define B00100101 37
199
+#define B100110 38
200
+#define B0100110 38
201
+#define B00100110 38
202
+#define B100111 39
203
+#define B0100111 39
204
+#define B00100111 39
205
+#define B101000 40
206
+#define B0101000 40
207
+#define B00101000 40
208
+#define B101001 41
209
+#define B0101001 41
210
+#define B00101001 41
211
+#define B101010 42
212
+#define B0101010 42
213
+#define B00101010 42
214
+#define B101011 43
215
+#define B0101011 43
216
+#define B00101011 43
217
+#define B101100 44
218
+#define B0101100 44
219
+#define B00101100 44
220
+#define B101101 45
221
+#define B0101101 45
222
+#define B00101101 45
223
+#define B101110 46
224
+#define B0101110 46
225
+#define B00101110 46
226
+#define B101111 47
227
+#define B0101111 47
228
+#define B00101111 47
229
+#define B110000 48
230
+#define B0110000 48
231
+#define B00110000 48
232
+#define B110001 49
233
+#define B0110001 49
234
+#define B00110001 49
235
+#define B110010 50
236
+#define B0110010 50
237
+#define B00110010 50
238
+#define B110011 51
239
+#define B0110011 51
240
+#define B00110011 51
241
+#define B110100 52
242
+#define B0110100 52
243
+#define B00110100 52
244
+#define B110101 53
245
+#define B0110101 53
246
+#define B00110101 53
247
+#define B110110 54
248
+#define B0110110 54
249
+#define B00110110 54
250
+#define B110111 55
251
+#define B0110111 55
252
+#define B00110111 55
253
+#define B111000 56
254
+#define B0111000 56
255
+#define B00111000 56
256
+#define B111001 57
257
+#define B0111001 57
258
+#define B00111001 57
259
+#define B111010 58
260
+#define B0111010 58
261
+#define B00111010 58
262
+#define B111011 59
263
+#define B0111011 59
264
+#define B00111011 59
265
+#define B111100 60
266
+#define B0111100 60
267
+#define B00111100 60
268
+#define B111101 61
269
+#define B0111101 61
270
+#define B00111101 61
271
+#define B111110 62
272
+#define B0111110 62
273
+#define B00111110 62
274
+#define B111111 63
275
+#define B0111111 63
276
+#define B00111111 63
277
+#define B1000000 64
278
+#define B01000000 64
279
+#define B1000001 65
280
+#define B01000001 65
281
+#define B1000010 66
282
+#define B01000010 66
283
+#define B1000011 67
284
+#define B01000011 67
285
+#define B1000100 68
286
+#define B01000100 68
287
+#define B1000101 69
288
+#define B01000101 69
289
+#define B1000110 70
290
+#define B01000110 70
291
+#define B1000111 71
292
+#define B01000111 71
293
+#define B1001000 72
294
+#define B01001000 72
295
+#define B1001001 73
296
+#define B01001001 73
297
+#define B1001010 74
298
+#define B01001010 74
299
+#define B1001011 75
300
+#define B01001011 75
301
+#define B1001100 76
302
+#define B01001100 76
303
+#define B1001101 77
304
+#define B01001101 77
305
+#define B1001110 78
306
+#define B01001110 78
307
+#define B1001111 79
308
+#define B01001111 79
309
+#define B1010000 80
310
+#define B01010000 80
311
+#define B1010001 81
312
+#define B01010001 81
313
+#define B1010010 82
314
+#define B01010010 82
315
+#define B1010011 83
316
+#define B01010011 83
317
+#define B1010100 84
318
+#define B01010100 84
319
+#define B1010101 85
320
+#define B01010101 85
321
+#define B1010110 86
322
+#define B01010110 86
323
+#define B1010111 87
324
+#define B01010111 87
325
+#define B1011000 88
326
+#define B01011000 88
327
+#define B1011001 89
328
+#define B01011001 89
329
+#define B1011010 90
330
+#define B01011010 90
331
+#define B1011011 91
332
+#define B01011011 91
333
+#define B1011100 92
334
+#define B01011100 92
335
+#define B1011101 93
336
+#define B01011101 93
337
+#define B1011110 94
338
+#define B01011110 94
339
+#define B1011111 95
340
+#define B01011111 95
341
+#define B1100000 96
342
+#define B01100000 96
343
+#define B1100001 97
344
+#define B01100001 97
345
+#define B1100010 98
346
+#define B01100010 98
347
+#define B1100011 99
348
+#define B01100011 99
349
+#define B1100100 100
350
+#define B01100100 100
351
+#define B1100101 101
352
+#define B01100101 101
353
+#define B1100110 102
354
+#define B01100110 102
355
+#define B1100111 103
356
+#define B01100111 103
357
+#define B1101000 104
358
+#define B01101000 104
359
+#define B1101001 105
360
+#define B01101001 105
361
+#define B1101010 106
362
+#define B01101010 106
363
+#define B1101011 107
364
+#define B01101011 107
365
+#define B1101100 108
366
+#define B01101100 108
367
+#define B1101101 109
368
+#define B01101101 109
369
+#define B1101110 110
370
+#define B01101110 110
371
+#define B1101111 111
372
+#define B01101111 111
373
+#define B1110000 112
374
+#define B01110000 112
375
+#define B1110001 113
376
+#define B01110001 113
377
+#define B1110010 114
378
+#define B01110010 114
379
+#define B1110011 115
380
+#define B01110011 115
381
+#define B1110100 116
382
+#define B01110100 116
383
+#define B1110101 117
384
+#define B01110101 117
385
+#define B1110110 118
386
+#define B01110110 118
387
+#define B1110111 119
388
+#define B01110111 119
389
+#define B1111000 120
390
+#define B01111000 120
391
+#define B1111001 121
392
+#define B01111001 121
393
+#define B1111010 122
394
+#define B01111010 122
395
+#define B1111011 123
396
+#define B01111011 123
397
+#define B1111100 124
398
+#define B01111100 124
399
+#define B1111101 125
400
+#define B01111101 125
401
+#define B1111110 126
402
+#define B01111110 126
403
+#define B1111111 127
404
+#define B01111111 127
405
+#define B10000000 128
406
+#define B10000001 129
407
+#define B10000010 130
408
+#define B10000011 131
409
+#define B10000100 132
410
+#define B10000101 133
411
+#define B10000110 134
412
+#define B10000111 135
413
+#define B10001000 136
414
+#define B10001001 137
415
+#define B10001010 138
416
+#define B10001011 139
417
+#define B10001100 140
418
+#define B10001101 141
419
+#define B10001110 142
420
+#define B10001111 143
421
+#define B10010000 144
422
+#define B10010001 145
423
+#define B10010010 146
424
+#define B10010011 147
425
+#define B10010100 148
426
+#define B10010101 149
427
+#define B10010110 150
428
+#define B10010111 151
429
+#define B10011000 152
430
+#define B10011001 153
431
+#define B10011010 154
432
+#define B10011011 155
433
+#define B10011100 156
434
+#define B10011101 157
435
+#define B10011110 158
436
+#define B10011111 159
437
+#define B10100000 160
438
+#define B10100001 161
439
+#define B10100010 162
440
+#define B10100011 163
441
+#define B10100100 164
442
+#define B10100101 165
443
+#define B10100110 166
444
+#define B10100111 167
445
+#define B10101000 168
446
+#define B10101001 169
447
+#define B10101010 170
448
+#define B10101011 171
449
+#define B10101100 172
450
+#define B10101101 173
451
+#define B10101110 174
452
+#define B10101111 175
453
+#define B10110000 176
454
+#define B10110001 177
455
+#define B10110010 178
456
+#define B10110011 179
457
+#define B10110100 180
458
+#define B10110101 181
459
+#define B10110110 182
460
+#define B10110111 183
461
+#define B10111000 184
462
+#define B10111001 185
463
+#define B10111010 186
464
+#define B10111011 187
465
+#define B10111100 188
466
+#define B10111101 189
467
+#define B10111110 190
468
+#define B10111111 191
469
+#define B11000000 192
470
+#define B11000001 193
471
+#define B11000010 194
472
+#define B11000011 195
473
+#define B11000100 196
474
+#define B11000101 197
475
+#define B11000110 198
476
+#define B11000111 199
477
+#define B11001000 200
478
+#define B11001001 201
479
+#define B11001010 202
480
+#define B11001011 203
481
+#define B11001100 204
482
+#define B11001101 205
483
+#define B11001110 206
484
+#define B11001111 207
485
+#define B11010000 208
486
+#define B11010001 209
487
+#define B11010010 210
488
+#define B11010011 211
489
+#define B11010100 212
490
+#define B11010101 213
491
+#define B11010110 214
492
+#define B11010111 215
493
+#define B11011000 216
494
+#define B11011001 217
495
+#define B11011010 218
496
+#define B11011011 219
497
+#define B11011100 220
498
+#define B11011101 221
499
+#define B11011110 222
500
+#define B11011111 223
501
+#define B11100000 224
502
+#define B11100001 225
503
+#define B11100010 226
504
+#define B11100011 227
505
+#define B11100100 228
506
+#define B11100101 229
507
+#define B11100110 230
508
+#define B11100111 231
509
+#define B11101000 232
510
+#define B11101001 233
511
+#define B11101010 234
512
+#define B11101011 235
513
+#define B11101100 236
514
+#define B11101101 237
515
+#define B11101110 238
516
+#define B11101111 239
517
+#define B11110000 240
518
+#define B11110001 241
519
+#define B11110010 242
520
+#define B11110011 243
521
+#define B11110100 244
522
+#define B11110101 245
523
+#define B11110110 246
524
+#define B11110111 247
525
+#define B11111000 248
526
+#define B11111001 249
527
+#define B11111010 250
528
+#define B11111011 251
529
+#define B11111100 252
530
+#define B11111101 253
531
+#define B11111110 254
532
+#define B11111111 255
533
+
534
+#endif

+ 31
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/hooks.c View File

@@ -0,0 +1,31 @@
1
+/*
2
+  Copyright (c) 2012 Arduino.  All right reserved.
3
+
4
+  This library is free software; you can redistribute it and/or
5
+  modify it under the terms of the GNU Lesser General Public
6
+  License as published by the Free Software Foundation; either
7
+  version 2.1 of the License, or (at your option) any later version.
8
+
9
+  This library 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.
12
+  See the GNU Lesser General Public License for more details.
13
+
14
+  You should have received a copy of the GNU Lesser General Public
15
+  License along with this library; if not, write to the Free Software
16
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
+*/
18
+
19
+/**
20
+ * Empty yield() hook.
21
+ *
22
+ * This function is intended to be used by library writers to build
23
+ * libraries or sketches that supports cooperative threads.
24
+ *
25
+ * Its defined as a weak symbol and it can be redefined to implement a
26
+ * real cooperative scheduler.
27
+ */
28
+static void __empty() {
29
+	// Empty
30
+}
31
+void yield(void) __attribute__ ((weak, alias("__empty")));

+ 49
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/main.cpp View File

@@ -0,0 +1,49 @@
1
+/*
2
+  main.cpp - Main loop for Arduino sketches
3
+  Copyright (c) 2005-2013 Arduino Team.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#include <Arduino.h>
21
+
22
+//Declared weak in Arduino.h to allow user redefinitions.
23
+int atexit(void (*func)()) { return 0; }
24
+
25
+// Weak empty variant initialization function.
26
+// May be redefined by variant files.
27
+void initVariant() __attribute__((weak));
28
+void initVariant() { }
29
+
30
+int main(void)
31
+{
32
+	init();
33
+
34
+	initVariant();
35
+
36
+#if defined(USBCON)
37
+	USBDevice.attach();
38
+#endif
39
+	
40
+	setup();
41
+    
42
+	for (;;) {
43
+		loop();
44
+		if (serialEventRun) serialEventRun();
45
+	}
46
+        
47
+	return 0;
48
+}
49
+

+ 36
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/new.cpp View File

@@ -0,0 +1,36 @@
1
+/*
2
+  Copyright (c) 2014 Arduino.  All right reserved.
3
+
4
+  This library is free software; you can redistribute it and/or
5
+  modify it under the terms of the GNU Lesser General Public
6
+  License as published by the Free Software Foundation; either
7
+  version 2.1 of the License, or (at your option) any later version.
8
+
9
+  This library 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.
12
+  See the GNU Lesser General Public License for more details.
13
+
14
+  You should have received a copy of the GNU Lesser General Public
15
+  License along with this library; if not, write to the Free Software
16
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
+*/
18
+
19
+#include <stdlib.h>
20
+
21
+void *operator new(size_t size) {
22
+  return malloc(size);
23
+}
24
+
25
+void *operator new[](size_t size) {
26
+  return malloc(size);
27
+}
28
+
29
+void operator delete(void * ptr) {
30
+  free(ptr);
31
+}
32
+
33
+void operator delete[](void * ptr) {
34
+  free(ptr);
35
+}
36
+

+ 30
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/new.h View File

@@ -0,0 +1,30 @@
1
+/*
2
+  Copyright (c) 2014 Arduino.  All right reserved.
3
+
4
+  This library is free software; you can redistribute it and/or
5
+  modify it under the terms of the GNU Lesser General Public
6
+  License as published by the Free Software Foundation; either
7
+  version 2.1 of the License, or (at your option) any later version.
8
+
9
+  This library 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.
12
+  See the GNU Lesser General Public License for more details.
13
+
14
+  You should have received a copy of the GNU Lesser General Public
15
+  License along with this library; if not, write to the Free Software
16
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
+*/
18
+
19
+#ifndef NEW_H
20
+#define NEW_H
21
+
22
+#include <stdlib.h>
23
+
24
+void * operator new(size_t size);
25
+void * operator new[](size_t size);
26
+void operator delete(void * ptr);
27
+void operator delete[](void * ptr);
28
+
29
+#endif
30
+

+ 325
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring.c View File

@@ -0,0 +1,325 @@
1
+/*
2
+  wiring.c - Partial implementation of the Wiring API for the ATmega8.
3
+  Part of Arduino - http://www.arduino.cc/
4
+
5
+  Copyright (c) 2005-2006 David A. Mellis
6
+
7
+  This library is free software; you can redistribute it and/or
8
+  modify it under the terms of the GNU Lesser General Public
9
+  License as published by the Free Software Foundation; either
10
+  version 2.1 of the License, or (at your option) any later version.
11
+
12
+  This library is distributed in the hope that it will be useful,
13
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+  Lesser General Public License for more details.
16
+
17
+  You should have received a copy of the GNU Lesser General
18
+  Public License along with this library; if not, write to the
19
+  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
+  Boston, MA  02111-1307  USA
21
+
22
+  $Id$
23
+*/
24
+
25
+#include "wiring_private.h"
26
+
27
+// the prescaler is set so that timer0 ticks every 64 clock cycles, and the
28
+// the overflow handler is called every 256 ticks.
29
+#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
30
+
31
+// the whole number of milliseconds per timer0 overflow
32
+#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
33
+
34
+// the fractional number of milliseconds per timer0 overflow. we shift right
35
+// by three to fit these numbers into a byte. (for the clock speeds we care
36
+// about - 8 and 16 MHz - this doesn't lose precision.)
37
+#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
38
+#define FRACT_MAX (1000 >> 3)
39
+
40
+volatile unsigned long timer0_overflow_count = 0;
41
+volatile unsigned long timer0_millis = 0;
42
+static unsigned char timer0_fract = 0;
43
+
44
+#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
45
+ISR(TIM0_OVF_vect)
46
+#else
47
+ISR(TIMER0_OVF_vect)
48
+#endif
49
+{
50
+	// copy these to local variables so they can be stored in registers
51
+	// (volatile variables must be read from memory on every access)
52
+	unsigned long m = timer0_millis;
53
+	unsigned char f = timer0_fract;
54
+
55
+	m += MILLIS_INC;
56
+	f += FRACT_INC;
57
+	if (f >= FRACT_MAX) {
58
+		f -= FRACT_MAX;
59
+		m += 1;
60
+	}
61
+
62
+	timer0_fract = f;
63
+	timer0_millis = m;
64
+	timer0_overflow_count++;
65
+}
66
+
67
+unsigned long millis()
68
+{
69
+	unsigned long m;
70
+	uint8_t oldSREG = SREG;
71
+
72
+	// disable interrupts while we read timer0_millis or we might get an
73
+	// inconsistent value (e.g. in the middle of a write to timer0_millis)
74
+	cli();
75
+	m = timer0_millis;
76
+	SREG = oldSREG;
77
+
78
+	return m;
79
+}
80
+
81
+unsigned long micros() {
82
+	unsigned long m;
83
+	uint8_t oldSREG = SREG, t;
84
+	
85
+	cli();
86
+	m = timer0_overflow_count;
87
+#if defined(TCNT0)
88
+	t = TCNT0;
89
+#elif defined(TCNT0L)
90
+	t = TCNT0L;
91
+#else
92
+	#error TIMER 0 not defined
93
+#endif
94
+
95
+  
96
+#ifdef TIFR0
97
+	if ((TIFR0 & _BV(TOV0)) && (t < 255))
98
+		m++;
99
+#else
100
+	if ((TIFR & _BV(TOV0)) && (t < 255))
101
+		m++;
102
+#endif
103
+
104
+	SREG = oldSREG;
105
+	
106
+	return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
107
+}
108
+
109
+void delay(unsigned long ms)
110
+{
111
+	uint16_t start = (uint16_t)micros();
112
+
113
+	while (ms > 0) {
114
+		yield();
115
+		if (((uint16_t)micros() - start) >= 1000) {
116
+			ms--;
117
+			start += 1000;
118
+		}
119
+	}
120
+}
121
+
122
+/* Delay for the given number of microseconds.  Assumes a 8 or 16 MHz clock. */
123
+void delayMicroseconds(unsigned int us)
124
+{
125
+	// calling avrlib's delay_us() function with low values (e.g. 1 or
126
+	// 2 microseconds) gives delays longer than desired.
127
+	//delay_us(us);
128
+#if F_CPU >= 20000000L
129
+	// for the 20 MHz clock on rare Arduino boards
130
+
131
+	// for a one-microsecond delay, simply wait 2 cycle and return. The overhead
132
+	// of the function call yields a delay of exactly a one microsecond.
133
+	__asm__ __volatile__ (
134
+		"nop" "\n\t"
135
+		"nop"); //just waiting 2 cycle
136
+	if (--us == 0)
137
+		return;
138
+
139
+	// the following loop takes a 1/5 of a microsecond (4 cycles)
140
+	// per iteration, so execute it five times for each microsecond of
141
+	// delay requested.
142
+	us = (us<<2) + us; // x5 us
143
+
144
+	// account for the time taken in the preceeding commands.
145
+	us -= 2;
146
+
147
+#elif F_CPU >= 16000000L
148
+	// for the 16 MHz clock on most Arduino boards
149
+
150
+	// for a one-microsecond delay, simply return.  the overhead
151
+	// of the function call yields a delay of approximately 1 1/8 us.
152
+	if (--us == 0)
153
+		return;
154
+
155
+	// the following loop takes a quarter of a microsecond (4 cycles)
156
+	// per iteration, so execute it four times for each microsecond of
157
+	// delay requested.
158
+	us <<= 2;
159
+
160
+	// account for the time taken in the preceeding commands.
161
+	us -= 2;
162
+#else
163
+	// for the 8 MHz internal clock on the ATmega168
164
+
165
+	// for a one- or two-microsecond delay, simply return.  the overhead of
166
+	// the function calls takes more than two microseconds.  can't just
167
+	// subtract two, since us is unsigned; we'd overflow.
168
+	if (--us == 0)
169
+		return;
170
+	if (--us == 0)
171
+		return;
172
+
173
+	// the following loop takes half of a microsecond (4 cycles)
174
+	// per iteration, so execute it twice for each microsecond of
175
+	// delay requested.
176
+	us <<= 1;
177
+    
178
+	// partially compensate for the time taken by the preceeding commands.
179
+	// we can't subtract any more than this or we'd overflow w/ small delays.
180
+	us--;
181
+#endif
182
+
183
+	// busy wait
184
+	__asm__ __volatile__ (
185
+		"1: sbiw %0,1" "\n\t" // 2 cycles
186
+		"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
187
+	);
188
+}
189
+
190
+void init()
191
+{
192
+	// this needs to be called before setup() or some functions won't
193
+	// work there
194
+	sei();
195
+	
196
+	// on the ATmega168, timer 0 is also used for fast hardware pwm
197
+	// (using phase-correct PWM would mean that timer 0 overflowed half as often
198
+	// resulting in different millis() behavior on the ATmega8 and ATmega168)
199
+#if defined(TCCR0A) && defined(WGM01)
200
+	sbi(TCCR0A, WGM01);
201
+	sbi(TCCR0A, WGM00);
202
+#endif  
203
+
204
+	// set timer 0 prescale factor to 64
205
+#if defined(__AVR_ATmega128__)
206
+	// CPU specific: different values for the ATmega128
207
+	sbi(TCCR0, CS02);
208
+#elif defined(TCCR0) && defined(CS01) && defined(CS00)
209
+	// this combination is for the standard atmega8
210
+	sbi(TCCR0, CS01);
211
+	sbi(TCCR0, CS00);
212
+#elif defined(TCCR0B) && defined(CS01) && defined(CS00)
213
+	// this combination is for the standard 168/328/1280/2560
214
+	sbi(TCCR0B, CS01);
215
+	sbi(TCCR0B, CS00);
216
+#elif defined(TCCR0A) && defined(CS01) && defined(CS00)
217
+	// this combination is for the __AVR_ATmega645__ series
218
+	sbi(TCCR0A, CS01);
219
+	sbi(TCCR0A, CS00);
220
+#else
221
+	#error Timer 0 prescale factor 64 not set correctly
222
+#endif
223
+
224
+	// enable timer 0 overflow interrupt
225
+#if defined(TIMSK) && defined(TOIE0)
226
+	sbi(TIMSK, TOIE0);
227
+#elif defined(TIMSK0) && defined(TOIE0)
228
+	sbi(TIMSK0, TOIE0);
229
+#else
230
+	#error	Timer 0 overflow interrupt not set correctly
231
+#endif
232
+
233
+	// timers 1 and 2 are used for phase-correct hardware pwm
234
+	// this is better for motors as it ensures an even waveform
235
+	// note, however, that fast pwm mode can achieve a frequency of up
236
+	// 8 MHz (with a 16 MHz clock) at 50% duty cycle
237
+
238
+#if defined(TCCR1B) && defined(CS11) && defined(CS10)
239
+	TCCR1B = 0;
240
+
241
+	// set timer 1 prescale factor to 64
242
+	sbi(TCCR1B, CS11);
243
+#if F_CPU >= 8000000L
244
+	sbi(TCCR1B, CS10);
245
+#endif
246
+#elif defined(TCCR1) && defined(CS11) && defined(CS10)
247
+	sbi(TCCR1, CS11);
248
+#if F_CPU >= 8000000L
249
+	sbi(TCCR1, CS10);
250
+#endif
251
+#endif
252
+	// put timer 1 in 8-bit phase correct pwm mode
253
+#if defined(TCCR1A) && defined(WGM10)
254
+	sbi(TCCR1A, WGM10);
255
+#elif defined(TCCR1)
256
+	#warning this needs to be finished
257
+#endif
258
+
259
+	// set timer 2 prescale factor to 64
260
+#if defined(TCCR2) && defined(CS22)
261
+	sbi(TCCR2, CS22);
262
+#elif defined(TCCR2B) && defined(CS22)
263
+	sbi(TCCR2B, CS22);
264
+#else
265
+	#warning Timer 2 not finished (may not be present on this CPU)
266
+#endif
267
+
268
+	// configure timer 2 for phase correct pwm (8-bit)
269
+#if defined(TCCR2) && defined(WGM20)
270
+	sbi(TCCR2, WGM20);
271
+#elif defined(TCCR2A) && defined(WGM20)
272
+	sbi(TCCR2A, WGM20);
273
+#else
274
+	#warning Timer 2 not finished (may not be present on this CPU)
275
+#endif
276
+
277
+#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
278
+	sbi(TCCR3B, CS31);		// set timer 3 prescale factor to 64
279
+	sbi(TCCR3B, CS30);
280
+	sbi(TCCR3A, WGM30);		// put timer 3 in 8-bit phase correct pwm mode
281
+#endif
282
+
283
+#if defined(TCCR4A) && defined(TCCR4B) && defined(TCCR4D) /* beginning of timer4 block for 32U4 and similar */
284
+	sbi(TCCR4B, CS42);		// set timer4 prescale factor to 64
285
+	sbi(TCCR4B, CS41);
286
+	sbi(TCCR4B, CS40);
287
+	sbi(TCCR4D, WGM40);		// put timer 4 in phase- and frequency-correct PWM mode	
288
+	sbi(TCCR4A, PWM4A);		// enable PWM mode for comparator OCR4A
289
+	sbi(TCCR4C, PWM4D);		// enable PWM mode for comparator OCR4D
290
+#else /* beginning of timer4 block for ATMEGA1280 and ATMEGA2560 */
291
+#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
292
+	sbi(TCCR4B, CS41);		// set timer 4 prescale factor to 64
293
+	sbi(TCCR4B, CS40);
294
+	sbi(TCCR4A, WGM40);		// put timer 4 in 8-bit phase correct pwm mode
295
+#endif
296
+#endif /* end timer4 block for ATMEGA1280/2560 and similar */	
297
+
298
+#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
299
+	sbi(TCCR5B, CS51);		// set timer 5 prescale factor to 64
300
+	sbi(TCCR5B, CS50);
301
+	sbi(TCCR5A, WGM50);		// put timer 5 in 8-bit phase correct pwm mode
302
+#endif
303
+
304
+#if defined(ADCSRA)
305
+	// set a2d prescale factor to 128
306
+	// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
307
+	// XXX: this will not work properly for other clock speeds, and
308
+	// this code should use F_CPU to determine the prescale factor.
309
+	sbi(ADCSRA, ADPS2);
310
+	sbi(ADCSRA, ADPS1);
311
+	sbi(ADCSRA, ADPS0);
312
+
313
+	// enable a2d conversions
314
+	sbi(ADCSRA, ADEN);
315
+#endif
316
+
317
+	// the bootloader connects pins 0 and 1 to the USART; disconnect them
318
+	// here so they can be used as normal digital i/o; they will be
319
+	// reconnected in Serial.begin()
320
+#if defined(UCSRB)
321
+	UCSRB = 0;
322
+#elif defined(UCSR0B)
323
+	UCSR0B = 0;
324
+#endif
325
+}

+ 292
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_analog.c View File

@@ -0,0 +1,292 @@
1
+/*
2
+  wiring_analog.c - analog input and output
3
+  Part of Arduino - http://www.arduino.cc/
4
+
5
+  Copyright (c) 2005-2006 David A. Mellis
6
+
7
+  This library is free software; you can redistribute it and/or
8
+  modify it under the terms of the GNU Lesser General Public
9
+  License as published by the Free Software Foundation; either
10
+  version 2.1 of the License, or (at your option) any later version.
11
+
12
+  This library is distributed in the hope that it will be useful,
13
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+  Lesser General Public License for more details.
16
+
17
+  You should have received a copy of the GNU Lesser General
18
+  Public License along with this library; if not, write to the
19
+  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
+  Boston, MA  02111-1307  USA
21
+
22
+  Modified 28 September 2010 by Mark Sproul
23
+
24
+  $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
25
+*/
26
+
27
+#include "wiring_private.h"
28
+#include "pins_arduino.h"
29
+
30
+uint8_t analog_reference = DEFAULT;
31
+
32
+void analogReference(uint8_t mode)
33
+{
34
+	// can't actually set the register here because the default setting
35
+	// will connect AVCC and the AREF pin, which would cause a short if
36
+	// there's something connected to AREF.
37
+	analog_reference = mode;
38
+}
39
+
40
+int analogRead(uint8_t pin)
41
+{
42
+	uint8_t low, high;
43
+
44
+#if defined(analogPinToChannel)
45
+#if defined(__AVR_ATmega32U4__)
46
+	if (pin >= 18) pin -= 18; // allow for channel or pin numbers
47
+#endif
48
+	pin = analogPinToChannel(pin);
49
+#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
50
+	if (pin >= 54) pin -= 54; // allow for channel or pin numbers
51
+#elif defined(__AVR_ATmega32U4__)
52
+	if (pin >= 18) pin -= 18; // allow for channel or pin numbers
53
+#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
54
+	if (pin >= 24) pin -= 24; // allow for channel or pin numbers
55
+#else
56
+	if (pin >= 14) pin -= 14; // allow for channel or pin numbers
57
+#endif
58
+
59
+#if defined(ADCSRB) && defined(MUX5)
60
+	// the MUX5 bit of ADCSRB selects whether we're reading from channels
61
+	// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
62
+	ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
63
+#endif
64
+  
65
+	// set the analog reference (high two bits of ADMUX) and select the
66
+	// channel (low 4 bits).  this also sets ADLAR (left-adjust result)
67
+	// to 0 (the default).
68
+#if defined(ADMUX)
69
+	ADMUX = (analog_reference << 6) | (pin & 0x07);
70
+#endif
71
+
72
+	// without a delay, we seem to read from the wrong channel
73
+	//delay(1);
74
+
75
+#if defined(ADCSRA) && defined(ADCL)
76
+	// start the conversion
77
+	sbi(ADCSRA, ADSC);
78
+
79
+	// ADSC is cleared when the conversion finishes
80
+	while (bit_is_set(ADCSRA, ADSC));
81
+
82
+	// we have to read ADCL first; doing so locks both ADCL
83
+	// and ADCH until ADCH is read.  reading ADCL second would
84
+	// cause the results of each conversion to be discarded,
85
+	// as ADCL and ADCH would be locked when it completed.
86
+	low  = ADCL;
87
+	high = ADCH;
88
+#else
89
+	// we dont have an ADC, return 0
90
+	low  = 0;
91
+	high = 0;
92
+#endif
93
+
94
+	// combine the two bytes
95
+	return (high << 8) | low;
96
+}
97
+
98
+// Right now, PWM output only works on the pins with
99
+// hardware support.  These are defined in the appropriate
100
+// pins_*.c file.  For the rest of the pins, we default
101
+// to digital output.
102
+void analogWrite(uint8_t pin, int val)
103
+{
104
+	// We need to make sure the PWM output is enabled for those pins
105
+	// that support it, as we turn it off when digitally reading or
106
+	// writing with them.  Also, make sure the pin is in output mode
107
+	// for consistenty with Wiring, which doesn't require a pinMode
108
+	// call for the analog output pins.
109
+	pinMode(pin, OUTPUT);
110
+	if (val == 0)
111
+	{
112
+		digitalWrite(pin, LOW);
113
+	}
114
+	else if (val == 255)
115
+	{
116
+		digitalWrite(pin, HIGH);
117
+	}
118
+	else
119
+	{
120
+		switch(digitalPinToTimer(pin))
121
+		{
122
+			// XXX fix needed for atmega8
123
+			#if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
124
+			case TIMER0A:
125
+				// connect pwm to pin on timer 0
126
+				sbi(TCCR0, COM00);
127
+				OCR0 = val; // set pwm duty
128
+				break;
129
+			#endif
130
+
131
+			#if defined(TCCR0A) && defined(COM0A1)
132
+			case TIMER0A:
133
+				// connect pwm to pin on timer 0, channel A
134
+				sbi(TCCR0A, COM0A1);
135
+				OCR0A = val; // set pwm duty
136
+				break;
137
+			#endif
138
+
139
+			#if defined(TCCR0A) && defined(COM0B1)
140
+			case TIMER0B:
141
+				// connect pwm to pin on timer 0, channel B
142
+				sbi(TCCR0A, COM0B1);
143
+				OCR0B = val; // set pwm duty
144
+				break;
145
+			#endif
146
+
147
+			#if defined(TCCR1A) && defined(COM1A1)
148
+			case TIMER1A:
149
+				// connect pwm to pin on timer 1, channel A
150
+				sbi(TCCR1A, COM1A1);
151
+				OCR1A = val; // set pwm duty
152
+				break;
153
+			#endif
154
+
155
+			#if defined(TCCR1A) && defined(COM1B1)
156
+			case TIMER1B:
157
+				// connect pwm to pin on timer 1, channel B
158
+				sbi(TCCR1A, COM1B1);
159
+				OCR1B = val; // set pwm duty
160
+				break;
161
+			#endif
162
+
163
+			#if defined(TCCR1A) && defined(COM1C1)
164
+			case TIMER1C:
165
+				// connect pwm to pin on timer 1, channel B
166
+				sbi(TCCR1A, COM1C1);
167
+				OCR1C = val; // set pwm duty
168
+				break;
169
+			#endif
170
+
171
+			#if defined(TCCR2) && defined(COM21)
172
+			case TIMER2:
173
+				// connect pwm to pin on timer 2
174
+				sbi(TCCR2, COM21);
175
+				OCR2 = val; // set pwm duty
176
+				break;
177
+			#endif
178
+
179
+			#if defined(TCCR2A) && defined(COM2A1)
180
+			case TIMER2A:
181
+				// connect pwm to pin on timer 2, channel A
182
+				sbi(TCCR2A, COM2A1);
183
+				OCR2A = val; // set pwm duty
184
+				break;
185
+			#endif
186
+
187
+			#if defined(TCCR2A) && defined(COM2B1)
188
+			case TIMER2B:
189
+				// connect pwm to pin on timer 2, channel B
190
+				sbi(TCCR2A, COM2B1);
191
+				OCR2B = val; // set pwm duty
192
+				break;
193
+			#endif
194
+
195
+			#if defined(TCCR3A) && defined(COM3A1)
196
+			case TIMER3A:
197
+				// connect pwm to pin on timer 3, channel A
198
+				sbi(TCCR3A, COM3A1);
199
+				OCR3A = val; // set pwm duty
200
+				break;
201
+			#endif
202
+
203
+			#if defined(TCCR3A) && defined(COM3B1)
204
+			case TIMER3B:
205
+				// connect pwm to pin on timer 3, channel B
206
+				sbi(TCCR3A, COM3B1);
207
+				OCR3B = val; // set pwm duty
208
+				break;
209
+			#endif
210
+
211
+			#if defined(TCCR3A) && defined(COM3C1)
212
+			case TIMER3C:
213
+				// connect pwm to pin on timer 3, channel C
214
+				sbi(TCCR3A, COM3C1);
215
+				OCR3C = val; // set pwm duty
216
+				break;
217
+			#endif
218
+
219
+			#if defined(TCCR4A)
220
+			case TIMER4A:
221
+				//connect pwm to pin on timer 4, channel A
222
+				sbi(TCCR4A, COM4A1);
223
+				#if defined(COM4A0)		// only used on 32U4
224
+				cbi(TCCR4A, COM4A0);
225
+				#endif
226
+				OCR4A = val;	// set pwm duty
227
+				break;
228
+			#endif
229
+			
230
+			#if defined(TCCR4A) && defined(COM4B1)
231
+			case TIMER4B:
232
+				// connect pwm to pin on timer 4, channel B
233
+				sbi(TCCR4A, COM4B1);
234
+				OCR4B = val; // set pwm duty
235
+				break;
236
+			#endif
237
+
238
+			#if defined(TCCR4A) && defined(COM4C1)
239
+			case TIMER4C:
240
+				// connect pwm to pin on timer 4, channel C
241
+				sbi(TCCR4A, COM4C1);
242
+				OCR4C = val; // set pwm duty
243
+				break;
244
+			#endif
245
+				
246
+			#if defined(TCCR4C) && defined(COM4D1)
247
+			case TIMER4D:				
248
+				// connect pwm to pin on timer 4, channel D
249
+				sbi(TCCR4C, COM4D1);
250
+				#if defined(COM4D0)		// only used on 32U4
251
+				cbi(TCCR4C, COM4D0);
252
+				#endif
253
+				OCR4D = val;	// set pwm duty
254
+				break;
255
+			#endif
256
+
257
+							
258
+			#if defined(TCCR5A) && defined(COM5A1)
259
+			case TIMER5A:
260
+				// connect pwm to pin on timer 5, channel A
261
+				sbi(TCCR5A, COM5A1);
262
+				OCR5A = val; // set pwm duty
263
+				break;
264
+			#endif
265
+
266
+			#if defined(TCCR5A) && defined(COM5B1)
267
+			case TIMER5B:
268
+				// connect pwm to pin on timer 5, channel B
269
+				sbi(TCCR5A, COM5B1);
270
+				OCR5B = val; // set pwm duty
271
+				break;
272
+			#endif
273
+
274
+			#if defined(TCCR5A) && defined(COM5C1)
275
+			case TIMER5C:
276
+				// connect pwm to pin on timer 5, channel C
277
+				sbi(TCCR5A, COM5C1);
278
+				OCR5C = val; // set pwm duty
279
+				break;
280
+			#endif
281
+
282
+			case NOT_ON_TIMER:
283
+			default:
284
+				if (val < 128) {
285
+					digitalWrite(pin, LOW);
286
+				} else {
287
+					digitalWrite(pin, HIGH);
288
+				}
289
+		}
290
+	}
291
+}
292
+

+ 181
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_digital.c View File

@@ -0,0 +1,181 @@
1
+/*
2
+  wiring_digital.c - digital input and output functions
3
+  Part of Arduino - http://www.arduino.cc/
4
+
5
+  Copyright (c) 2005-2006 David A. Mellis
6
+
7
+  This library is free software; you can redistribute it and/or
8
+  modify it under the terms of the GNU Lesser General Public
9
+  License as published by the Free Software Foundation; either
10
+  version 2.1 of the License, or (at your option) any later version.
11
+
12
+  This library is distributed in the hope that it will be useful,
13
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+  Lesser General Public License for more details.
16
+
17
+  You should have received a copy of the GNU Lesser General
18
+  Public License along with this library; if not, write to the
19
+  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
+  Boston, MA  02111-1307  USA
21
+
22
+  Modified 28 September 2010 by Mark Sproul
23
+
24
+  $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
25
+*/
26
+
27
+#define ARDUINO_MAIN
28
+#include "wiring_private.h"
29
+#include "pins_arduino.h"
30
+
31
+void pinMode(uint8_t pin, uint8_t mode)
32
+{
33
+	uint8_t bit = digitalPinToBitMask(pin);
34
+	uint8_t port = digitalPinToPort(pin);
35
+	volatile uint8_t *reg, *out;
36
+
37
+	if (port == NOT_A_PIN) return;
38
+
39
+	// JWS: can I let the optimizer do this?
40
+	reg = portModeRegister(port);
41
+	out = portOutputRegister(port);
42
+
43
+	if (mode == INPUT) { 
44
+		uint8_t oldSREG = SREG;
45
+                cli();
46
+		*reg &= ~bit;
47
+		*out &= ~bit;
48
+		SREG = oldSREG;
49
+	} else if (mode == INPUT_PULLUP) {
50
+		uint8_t oldSREG = SREG;
51
+                cli();
52
+		*reg &= ~bit;
53
+		*out |= bit;
54
+		SREG = oldSREG;
55
+	} else {
56
+		uint8_t oldSREG = SREG;
57
+                cli();
58
+		*reg |= bit;
59
+		SREG = oldSREG;
60
+	}
61
+}
62
+
63
+// Forcing this inline keeps the callers from having to push their own stuff
64
+// on the stack. It is a good performance win and only takes 1 more byte per
65
+// user than calling. (It will take more bytes on the 168.)
66
+//
67
+// But shouldn't this be moved into pinMode? Seems silly to check and do on
68
+// each digitalread or write.
69
+//
70
+// Mark Sproul:
71
+// - Removed inline. Save 170 bytes on atmega1280
72
+// - changed to a switch statment; added 32 bytes but much easier to read and maintain.
73
+// - Added more #ifdefs, now compiles for atmega645
74
+//
75
+//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
76
+//static inline void turnOffPWM(uint8_t timer)
77
+static void turnOffPWM(uint8_t timer)
78
+{
79
+	switch (timer)
80
+	{
81
+		#if defined(TCCR1A) && defined(COM1A1)
82
+		case TIMER1A:   cbi(TCCR1A, COM1A1);    break;
83
+		#endif
84
+		#if defined(TCCR1A) && defined(COM1B1)
85
+		case TIMER1B:   cbi(TCCR1A, COM1B1);    break;
86
+		#endif
87
+		#if defined(TCCR1A) && defined(COM1C1)
88
+		case TIMER1C:   cbi(TCCR1A, COM1C1);    break;
89
+		#endif
90
+		
91
+		#if defined(TCCR2) && defined(COM21)
92
+		case  TIMER2:   cbi(TCCR2, COM21);      break;
93
+		#endif
94
+		
95
+		#if defined(TCCR0A) && defined(COM0A1)
96
+		case  TIMER0A:  cbi(TCCR0A, COM0A1);    break;
97
+		#endif
98
+		
99
+		#if defined(TIMER0B) && defined(COM0B1)
100
+		case  TIMER0B:  cbi(TCCR0A, COM0B1);    break;
101
+		#endif
102
+		#if defined(TCCR2A) && defined(COM2A1)
103
+		case  TIMER2A:  cbi(TCCR2A, COM2A1);    break;
104
+		#endif
105
+		#if defined(TCCR2A) && defined(COM2B1)
106
+		case  TIMER2B:  cbi(TCCR2A, COM2B1);    break;
107
+		#endif
108
+		
109
+		#if defined(TCCR3A) && defined(COM3A1)
110
+		case  TIMER3A:  cbi(TCCR3A, COM3A1);    break;
111
+		#endif
112
+		#if defined(TCCR3A) && defined(COM3B1)
113
+		case  TIMER3B:  cbi(TCCR3A, COM3B1);    break;
114
+		#endif
115
+		#if defined(TCCR3A) && defined(COM3C1)
116
+		case  TIMER3C:  cbi(TCCR3A, COM3C1);    break;
117
+		#endif
118
+
119
+		#if defined(TCCR4A) && defined(COM4A1)
120
+		case  TIMER4A:  cbi(TCCR4A, COM4A1);    break;
121
+		#endif					
122
+		#if defined(TCCR4A) && defined(COM4B1)
123
+		case  TIMER4B:  cbi(TCCR4A, COM4B1);    break;
124
+		#endif
125
+		#if defined(TCCR4A) && defined(COM4C1)
126
+		case  TIMER4C:  cbi(TCCR4A, COM4C1);    break;
127
+		#endif			
128
+		#if defined(TCCR4C) && defined(COM4D1)
129
+		case TIMER4D:	cbi(TCCR4C, COM4D1);	break;
130
+		#endif			
131
+			
132
+		#if defined(TCCR5A)
133
+		case  TIMER5A:  cbi(TCCR5A, COM5A1);    break;
134
+		case  TIMER5B:  cbi(TCCR5A, COM5B1);    break;
135
+		case  TIMER5C:  cbi(TCCR5A, COM5C1);    break;
136
+		#endif
137
+	}
138
+}
139
+
140
+void digitalWrite(uint8_t pin, uint8_t val)
141
+{
142
+	uint8_t timer = digitalPinToTimer(pin);
143
+	uint8_t bit = digitalPinToBitMask(pin);
144
+	uint8_t port = digitalPinToPort(pin);
145
+	volatile uint8_t *out;
146
+
147
+	if (port == NOT_A_PIN) return;
148
+
149
+	// If the pin that support PWM output, we need to turn it off
150
+	// before doing a digital write.
151
+	if (timer != NOT_ON_TIMER) turnOffPWM(timer);
152
+
153
+	out = portOutputRegister(port);
154
+
155
+	uint8_t oldSREG = SREG;
156
+	cli();
157
+
158
+	if (val == LOW) {
159
+		*out &= ~bit;
160
+	} else {
161
+		*out |= bit;
162
+	}
163
+
164
+	SREG = oldSREG;
165
+}
166
+
167
+int digitalRead(uint8_t pin)
168
+{
169
+	uint8_t timer = digitalPinToTimer(pin);
170
+	uint8_t bit = digitalPinToBitMask(pin);
171
+	uint8_t port = digitalPinToPort(pin);
172
+
173
+	if (port == NOT_A_PIN) return LOW;
174
+
175
+	// If the pin that support PWM output, we need to turn it off
176
+	// before getting a digital reading.
177
+	if (timer != NOT_ON_TIMER) turnOffPWM(timer);
178
+
179
+	if (*portInputRegister(port) & bit) return HIGH;
180
+	return LOW;
181
+}

+ 71
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_private.h View File

@@ -0,0 +1,71 @@
1
+/*
2
+  wiring_private.h - Internal header file.
3
+  Part of Arduino - http://www.arduino.cc/
4
+
5
+  Copyright (c) 2005-2006 David A. Mellis
6
+
7
+  This library is free software; you can redistribute it and/or
8
+  modify it under the terms of the GNU Lesser General Public
9
+  License as published by the Free Software Foundation; either
10
+  version 2.1 of the License, or (at your option) any later version.
11
+
12
+  This library is distributed in the hope that it will be useful,
13
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+  Lesser General Public License for more details.
16
+
17
+  You should have received a copy of the GNU Lesser General
18
+  Public License along with this library; if not, write to the
19
+  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
+  Boston, MA  02111-1307  USA
21
+
22
+  $Id: wiring.h 239 2007-01-12 17:58:39Z mellis $
23
+*/
24
+
25
+#ifndef WiringPrivate_h
26
+#define WiringPrivate_h
27
+
28
+#include <avr/io.h>
29
+#include <avr/interrupt.h>
30
+#include <stdio.h>
31
+#include <stdarg.h>
32
+
33
+#include "Arduino.h"
34
+
35
+#ifdef __cplusplus
36
+extern "C"{
37
+#endif
38
+
39
+#ifndef cbi
40
+#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
41
+#endif
42
+#ifndef sbi
43
+#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
44
+#endif
45
+
46
+#define EXTERNAL_INT_0 0
47
+#define EXTERNAL_INT_1 1
48
+#define EXTERNAL_INT_2 2
49
+#define EXTERNAL_INT_3 3
50
+#define EXTERNAL_INT_4 4
51
+#define EXTERNAL_INT_5 5
52
+#define EXTERNAL_INT_6 6
53
+#define EXTERNAL_INT_7 7
54
+
55
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega128RFA1__) || defined(__AVR_ATmega256RFR2__)
56
+#define EXTERNAL_NUM_INTERRUPTS 8
57
+#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
58
+#define EXTERNAL_NUM_INTERRUPTS 3
59
+#elif defined(__AVR_ATmega32U4__)
60
+#define EXTERNAL_NUM_INTERRUPTS 5
61
+#else
62
+#define EXTERNAL_NUM_INTERRUPTS 2
63
+#endif
64
+
65
+typedef void (*voidFuncPtr)(void);
66
+
67
+#ifdef __cplusplus
68
+} // extern "C"
69
+#endif
70
+
71
+#endif

+ 85
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_pulse.c View File

@@ -0,0 +1,85 @@
1
+/*
2
+  wiring_pulse.c - pulseIn() function
3
+  Part of Arduino - http://www.arduino.cc/
4
+
5
+  Copyright (c) 2005-2006 David A. Mellis
6
+
7
+  This library is free software; you can redistribute it and/or
8
+  modify it under the terms of the GNU Lesser General Public
9
+  License as published by the Free Software Foundation; either
10
+  version 2.1 of the License, or (at your option) any later version.
11
+
12
+  This library is distributed in the hope that it will be useful,
13
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+  Lesser General Public License for more details.
16
+
17
+  You should have received a copy of the GNU Lesser General
18
+  Public License along with this library; if not, write to the
19
+  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
+  Boston, MA  02111-1307  USA
21
+
22
+  $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
23
+*/
24
+
25
+#include "wiring_private.h"
26
+#include "pins_arduino.h"
27
+
28
+/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
29
+ * or LOW, the type of pulse to measure.  Works on pulses from 2-3 microseconds
30
+ * to 3 minutes in length, but must be called at least a few dozen microseconds
31
+ * before the start of the pulse. */
32
+unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
33
+{
34
+	// cache the port and bit of the pin in order to speed up the
35
+	// pulse width measuring loop and achieve finer resolution.  calling
36
+	// digitalRead() instead yields much coarser resolution.
37
+	uint8_t bit = digitalPinToBitMask(pin);
38
+	uint8_t port = digitalPinToPort(pin);
39
+	uint8_t stateMask = (state ? bit : 0);
40
+	unsigned long width = 0; // keep initialization out of time critical area
41
+	
42
+	// convert the timeout from microseconds to a number of times through
43
+	// the initial loop; it takes 16 clock cycles per iteration.
44
+	unsigned long numloops = 0;
45
+	unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
46
+	
47
+	// wait for any previous pulse to end
48
+	while ((*portInputRegister(port) & bit) == stateMask)
49
+		if (numloops++ == maxloops)
50
+			return 0;
51
+	
52
+	// wait for the pulse to start
53
+	while ((*portInputRegister(port) & bit) != stateMask)
54
+		if (numloops++ == maxloops)
55
+			return 0;
56
+	
57
+	// wait for the pulse to stop
58
+	while ((*portInputRegister(port) & bit) == stateMask) {
59
+		if (numloops++ == maxloops)
60
+			return 0;
61
+		width++;
62
+	}
63
+
64
+	// convert the reading to microseconds. There will be some error introduced by
65
+	// the interrupt handlers.
66
+
67
+	// Conversion constants are compiler-dependent, different compiler versions
68
+	// have different levels of optimization.
69
+#if __GNUC__==4 && __GNUC_MINOR__==3 && __GNUC_PATCHLEVEL__==2
70
+	// avr-gcc 4.3.2
71
+	return clockCyclesToMicroseconds(width * 21 + 16);
72
+#elif __GNUC__==4 && __GNUC_MINOR__==8 && __GNUC_PATCHLEVEL__==1
73
+	// avr-gcc 4.8.1
74
+	return clockCyclesToMicroseconds(width * 24 + 16);
75
+#elif __GNUC__<=4 && __GNUC_MINOR__<=3
76
+	// avr-gcc <=4.3.x
77
+	#warning "pulseIn() results may not be accurate"
78
+	return clockCyclesToMicroseconds(width * 21 + 16);
79
+#else
80
+	// avr-gcc >4.3.x
81
+	#warning "pulseIn() results may not be accurate"
82
+	return clockCyclesToMicroseconds(width * 24 + 16);
83
+#endif
84
+
85
+}

+ 55
- 0
ArduinoAddons/Arduino_1.6.x/hardware/marlin/avr/cores/arduino/wiring_shift.c View File

@@ -0,0 +1,55 @@
1
+/*
2
+  wiring_shift.c - shiftOut() function
3
+  Part of Arduino - http://www.arduino.cc/
4
+
5
+  Copyright (c) 2005-2006 David A. Mellis
6
+
7
+  This library is free software; you can redistribute it and/or
8
+  modify it under the terms of the GNU Lesser General Public
9
+  License as published by the Free Software Foundation; either
10
+  version 2.1 of the License, or (at your option) any later version.
11
+
12
+  This library is distributed in the hope that it will be useful,
13
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+  Lesser General Public License for more details.
16
+
17
+  You should have received a copy of the GNU Lesser General
18
+  Public License along with this library; if not, write to the
19
+  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
+  Boston, MA  02111-1307  USA
21
+
22
+  $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
23
+*/
24
+
25
+#include "wiring_private.h"
26
+
27
+uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
28
+	uint8_t value = 0;
29
+	uint8_t i;
30
+
31
+	for (i = 0; i < 8; ++i) {
32
+		digitalWrite(clockPin, HIGH);
33
+		if (bitOrder == LSBFIRST)
34
+			value |= digitalRead(dataPin) << i;
35
+		else
36
+			value |= digitalRead(dataPin) << (7 - i);
37
+		digitalWrite(clockPin, LOW);
38
+	}
39
+	return value;
40
+}
41
+
42
+void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
43
+{
44
+	uint8_t i;
45
+
46
+	for (i = 0; i < 8; i++)  {
47
+		if (bitOrder == LSBFIRST)
48
+			digitalWrite(dataPin, !!(val & (1 << i)));
49
+		else	
50
+			digitalWrite(dataPin, !!(val & (1 << (7 - i))));
51
+			
52
+		digitalWrite(clockPin, HIGH);
53
+		digitalWrite(clockPin, LOW);		
54
+	}
55
+}

Loading…
Cancel
Save