commit
c7273d521f
@ -0,0 +1,22 @@
|
|||||||
|
# See: http://code.google.com/p/arduino/wiki/Platforms
|
||||||
|
|
||||||
|
##############################################################
|
||||||
|
|
||||||
|
mega2560.name=RAMBo
|
||||||
|
|
||||||
|
mega2560.upload.protocol=wiring
|
||||||
|
mega2560.upload.maximum_size=258048
|
||||||
|
mega2560.upload.speed=115200
|
||||||
|
|
||||||
|
mega2560.bootloader.low_fuses=0xFF
|
||||||
|
mega2560.bootloader.high_fuses=0xD8
|
||||||
|
mega2560.bootloader.extended_fuses=0xFD
|
||||||
|
mega2560.bootloader.path=stk500v2
|
||||||
|
mega2560.bootloader.file=stk500boot_v2_mega2560.hex
|
||||||
|
mega2560.bootloader.unlock_bits=0x3F
|
||||||
|
mega2560.bootloader.lock_bits=0x0F
|
||||||
|
|
||||||
|
mega2560.build.mcu=atmega2560
|
||||||
|
mega2560.build.f_cpu=16000000L
|
||||||
|
mega2560.build.core=arduino
|
||||||
|
mega2560.build.variant=standard
|
@ -0,0 +1,215 @@
|
|||||||
|
#ifndef Arduino_h
|
||||||
|
#define Arduino_h
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
#include "binary.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define HIGH 0x1
|
||||||
|
#define LOW 0x0
|
||||||
|
|
||||||
|
#define INPUT 0x0
|
||||||
|
#define OUTPUT 0x1
|
||||||
|
#define INPUT_PULLUP 0x2
|
||||||
|
|
||||||
|
#define true 0x1
|
||||||
|
#define false 0x0
|
||||||
|
|
||||||
|
#define PI 3.1415926535897932384626433832795
|
||||||
|
#define HALF_PI 1.5707963267948966192313216916398
|
||||||
|
#define TWO_PI 6.283185307179586476925286766559
|
||||||
|
#define DEG_TO_RAD 0.017453292519943295769236907684886
|
||||||
|
#define RAD_TO_DEG 57.295779513082320876798154814105
|
||||||
|
|
||||||
|
#define SERIAL 0x0
|
||||||
|
#define DISPLAY 0x1
|
||||||
|
|
||||||
|
#define LSBFIRST 0
|
||||||
|
#define MSBFIRST 1
|
||||||
|
|
||||||
|
#define CHANGE 1
|
||||||
|
#define FALLING 2
|
||||||
|
#define RISING 3
|
||||||
|
|
||||||
|
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
||||||
|
#define DEFAULT 0
|
||||||
|
#define EXTERNAL 1
|
||||||
|
#define INTERNAL 2
|
||||||
|
#else
|
||||||
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__)
|
||||||
|
#define INTERNAL1V1 2
|
||||||
|
#define INTERNAL2V56 3
|
||||||
|
#else
|
||||||
|
#define INTERNAL 3
|
||||||
|
#endif
|
||||||
|
#define DEFAULT 1
|
||||||
|
#define EXTERNAL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// undefine stdlib's abs if encountered
|
||||||
|
#ifdef abs
|
||||||
|
#undef abs
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define min(a,b) ((a)<(b)?(a):(b))
|
||||||
|
#define max(a,b) ((a)>(b)?(a):(b))
|
||||||
|
#define abs(x) ((x)>0?(x):-(x))
|
||||||
|
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
|
||||||
|
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
|
||||||
|
#define radians(deg) ((deg)*DEG_TO_RAD)
|
||||||
|
#define degrees(rad) ((rad)*RAD_TO_DEG)
|
||||||
|
#define sq(x) ((x)*(x))
|
||||||
|
|
||||||
|
#define interrupts() sei()
|
||||||
|
#define noInterrupts() cli()
|
||||||
|
|
||||||
|
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
|
||||||
|
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
|
||||||
|
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
|
||||||
|
|
||||||
|
#define lowByte(w) ((uint8_t) ((w) & 0xff))
|
||||||
|
#define highByte(w) ((uint8_t) ((w) >> 8))
|
||||||
|
|
||||||
|
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
|
||||||
|
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
|
||||||
|
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
|
||||||
|
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
|
||||||
|
|
||||||
|
|
||||||
|
typedef unsigned int word;
|
||||||
|
|
||||||
|
#define bit(b) (1UL << (b))
|
||||||
|
|
||||||
|
typedef uint8_t boolean;
|
||||||
|
typedef uint8_t byte;
|
||||||
|
|
||||||
|
void init(void);
|
||||||
|
|
||||||
|
void pinMode(uint8_t, uint8_t);
|
||||||
|
void digitalWrite(uint8_t, uint8_t);
|
||||||
|
int digitalRead(uint8_t);
|
||||||
|
int analogRead(uint8_t);
|
||||||
|
void analogReference(uint8_t mode);
|
||||||
|
void analogWrite(uint8_t, int);
|
||||||
|
|
||||||
|
unsigned long millis(void);
|
||||||
|
unsigned long micros(void);
|
||||||
|
void delay(unsigned long);
|
||||||
|
void delayMicroseconds(unsigned int us);
|
||||||
|
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
|
||||||
|
|
||||||
|
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
|
||||||
|
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
|
||||||
|
|
||||||
|
void attachInterrupt(uint8_t, void (*)(void), int mode);
|
||||||
|
void detachInterrupt(uint8_t);
|
||||||
|
|
||||||
|
void setup(void);
|
||||||
|
void loop(void);
|
||||||
|
|
||||||
|
// Get the bit location within the hardware port of the given virtual pin.
|
||||||
|
// This comes from the pins_*.c file for the active board configuration.
|
||||||
|
|
||||||
|
#define analogInPinToBit(P) (P)
|
||||||
|
|
||||||
|
// On the ATmega1280, the addresses of some of the port registers are
|
||||||
|
// greater than 255, so we can't store them in uint8_t's.
|
||||||
|
extern const uint16_t PROGMEM port_to_mode_PGM[];
|
||||||
|
extern const uint16_t PROGMEM port_to_input_PGM[];
|
||||||
|
extern const uint16_t PROGMEM port_to_output_PGM[];
|
||||||
|
|
||||||
|
extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
|
||||||
|
// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
|
||||||
|
extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
|
||||||
|
extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
|
||||||
|
|
||||||
|
// Get the bit location within the hardware port of the given virtual pin.
|
||||||
|
// This comes from the pins_*.c file for the active board configuration.
|
||||||
|
//
|
||||||
|
// These perform slightly better as macros compared to inline functions
|
||||||
|
//
|
||||||
|
#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
|
||||||
|
#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
|
||||||
|
#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
|
||||||
|
#define analogInPinToBit(P) (P)
|
||||||
|
#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
|
||||||
|
#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
|
||||||
|
#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
|
||||||
|
|
||||||
|
#define NOT_A_PIN 0
|
||||||
|
#define NOT_A_PORT 0
|
||||||
|
|
||||||
|
#ifdef ARDUINO_MAIN
|
||||||
|
#define PA 1
|
||||||
|
#define PB 2
|
||||||
|
#define PC 3
|
||||||
|
#define PD 4
|
||||||
|
#define PE 5
|
||||||
|
#define PF 6
|
||||||
|
#define PG 7
|
||||||
|
#define PH 8
|
||||||
|
#define PJ 10
|
||||||
|
#define PK 11
|
||||||
|
#define PL 12
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define NOT_ON_TIMER 0
|
||||||
|
#define TIMER0A 1
|
||||||
|
#define TIMER0B 2
|
||||||
|
#define TIMER1A 3
|
||||||
|
#define TIMER1B 4
|
||||||
|
#define TIMER2 5
|
||||||
|
#define TIMER2A 6
|
||||||
|
#define TIMER2B 7
|
||||||
|
|
||||||
|
#define TIMER3A 8
|
||||||
|
#define TIMER3B 9
|
||||||
|
#define TIMER3C 10
|
||||||
|
#define TIMER4A 11
|
||||||
|
#define TIMER4B 12
|
||||||
|
#define TIMER4C 13
|
||||||
|
#define TIMER4D 14
|
||||||
|
#define TIMER5A 15
|
||||||
|
#define TIMER5B 16
|
||||||
|
#define TIMER5C 17
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#include "WCharacter.h"
|
||||||
|
#include "WString.h"
|
||||||
|
#include "HardwareSerial.h"
|
||||||
|
|
||||||
|
uint16_t makeWord(uint16_t w);
|
||||||
|
uint16_t makeWord(byte h, byte l);
|
||||||
|
|
||||||
|
#define word(...) makeWord(__VA_ARGS__)
|
||||||
|
|
||||||
|
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
|
||||||
|
|
||||||
|
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
|
||||||
|
void noTone(uint8_t _pin);
|
||||||
|
|
||||||
|
// WMath prototypes
|
||||||
|
long random(long);
|
||||||
|
long random(long, long);
|
||||||
|
void randomSeed(unsigned int);
|
||||||
|
long map(long, long, long, long, long);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "pins_arduino.h"
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,239 @@
|
|||||||
|
|
||||||
|
|
||||||
|
/* Copyright (c) 2011, Peter Barrett
|
||||||
|
**
|
||||||
|
** Permission to use, copy, modify, and/or distribute this software for
|
||||||
|
** any purpose with or without fee is hereby granted, provided that the
|
||||||
|
** above copyright notice and this permission notice appear in all copies.
|
||||||
|
**
|
||||||
|
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||||
|
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||||
|
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
||||||
|
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
||||||
|
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
||||||
|
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||||
|
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||||
|
** SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Platform.h"
|
||||||
|
#include "USBAPI.h"
|
||||||
|
#include <avr/wdt.h>
|
||||||
|
|
||||||
|
#if defined(USBCON)
|
||||||
|
#ifdef CDC_ENABLED
|
||||||
|
|
||||||
|
#if (RAMEND < 1000)
|
||||||
|
#define SERIAL_BUFFER_SIZE 16
|
||||||
|
#else
|
||||||
|
#define SERIAL_BUFFER_SIZE 64
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct ring_buffer
|
||||||
|
{
|
||||||
|
unsigned char buffer[SERIAL_BUFFER_SIZE];
|
||||||
|
volatile int head;
|
||||||
|
volatile int tail;
|
||||||
|
};
|
||||||
|
|
||||||
|
ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u32 dwDTERate;
|
||||||
|
u8 bCharFormat;
|
||||||
|
u8 bParityType;
|
||||||
|
u8 bDataBits;
|
||||||
|
u8 lineState;
|
||||||
|
} LineInfo;
|
||||||
|
|
||||||
|
static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
#define WEAK __attribute__ ((weak))
|
||||||
|
|
||||||
|
extern const CDCDescriptor _cdcInterface PROGMEM;
|
||||||
|
const CDCDescriptor _cdcInterface =
|
||||||
|
{
|
||||||
|
D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
|
||||||
|
|
||||||
|
// CDC communication interface
|
||||||
|
D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
|
||||||
|
D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd)
|
||||||
|
D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management (not)
|
||||||
|
D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
|
||||||
|
D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
|
||||||
|
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
|
||||||
|
|
||||||
|
// CDC data interface
|
||||||
|
D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
|
||||||
|
D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
|
||||||
|
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
|
||||||
|
};
|
||||||
|
|
||||||
|
int WEAK CDC_GetInterface(u8* interfaceNum)
|
||||||
|
{
|
||||||
|
interfaceNum[0] += 2; // uses 2
|
||||||
|
return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WEAK CDC_Setup(Setup& setup)
|
||||||
|
{
|
||||||
|
u8 r = setup.bRequest;
|
||||||
|
u8 requestType = setup.bmRequestType;
|
||||||
|
|
||||||
|
if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
|
||||||
|
{
|
||||||
|
if (CDC_GET_LINE_CODING == r)
|
||||||
|
{
|
||||||
|
USB_SendControl(0,(void*)&_usbLineInfo,7);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
|
||||||
|
{
|
||||||
|
if (CDC_SET_LINE_CODING == r)
|
||||||
|
{
|
||||||
|
USB_RecvControl((void*)&_usbLineInfo,7);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (CDC_SET_CONTROL_LINE_STATE == r)
|
||||||
|
{
|
||||||
|
_usbLineInfo.lineState = setup.wValueL;
|
||||||
|
|
||||||
|
// auto-reset into the bootloader is triggered when the port, already
|
||||||
|
// open at 1200 bps, is closed. this is the signal to start the watchdog
|
||||||
|
// with a relatively long period so it can finish housekeeping tasks
|
||||||
|
// like servicing endpoints before the sketch ends
|
||||||
|
if (1200 == _usbLineInfo.dwDTERate) {
|
||||||
|
// We check DTR state to determine if host port is open (bit 0 of lineState).
|
||||||
|
if ((_usbLineInfo.lineState & 0x01) == 0) {
|
||||||
|
*(uint16_t *)0x0800 = 0x7777;
|
||||||
|
wdt_enable(WDTO_120MS);
|
||||||
|
} else {
|
||||||
|
// Most OSs do some intermediate steps when configuring ports and DTR can
|
||||||
|
// twiggle more than once before stabilizing.
|
||||||
|
// To avoid spurious resets we set the watchdog to 250ms and eventually
|
||||||
|
// cancel if DTR goes back high.
|
||||||
|
|
||||||
|
wdt_disable();
|
||||||
|
wdt_reset();
|
||||||
|
*(uint16_t *)0x0800 = 0x0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int _serialPeek = -1;
|
||||||
|
void Serial_::begin(uint16_t baud_count)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Serial_::end(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Serial_::accept(void)
|
||||||
|
{
|
||||||
|
ring_buffer *buffer = &cdc_rx_buffer;
|
||||||
|
int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
|
||||||
|
|
||||||
|
// if we should be storing the received character into the location
|
||||||
|
// just before the tail (meaning that the head would advance to the
|
||||||
|
// current location of the tail), we're about to overflow the buffer
|
||||||
|
// and so we don't write the character or advance the head.
|
||||||
|
|
||||||
|
// while we have room to store a byte
|
||||||
|
while (i != buffer->tail) {
|
||||||
|
int c = USB_Recv(CDC_RX);
|
||||||
|
if (c == -1)
|
||||||
|
break; // no more data
|
||||||
|
buffer->buffer[buffer->head] = c;
|
||||||
|
buffer->head = i;
|
||||||
|
|
||||||
|
i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Serial_::available(void)
|
||||||
|
{
|
||||||
|
ring_buffer *buffer = &cdc_rx_buffer;
|
||||||
|
return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Serial_::peek(void)
|
||||||
|
{
|
||||||
|
ring_buffer *buffer = &cdc_rx_buffer;
|
||||||
|
if (buffer->head == buffer->tail) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
return buffer->buffer[buffer->tail];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Serial_::read(void)
|
||||||
|
{
|
||||||
|
ring_buffer *buffer = &cdc_rx_buffer;
|
||||||
|
// if the head isn't ahead of the tail, we don't have any characters
|
||||||
|
if (buffer->head == buffer->tail) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
unsigned char c = buffer->buffer[buffer->tail];
|
||||||
|
buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Serial_::flush(void)
|
||||||
|
{
|
||||||
|
USB_Flush(CDC_TX);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Serial_::write(uint8_t c)
|
||||||
|
{
|
||||||
|
/* only try to send bytes if the high-level CDC connection itself
|
||||||
|
is open (not just the pipe) - the OS should set lineState when the port
|
||||||
|
is opened and clear lineState when the port is closed.
|
||||||
|
bytes sent before the user opens the connection or after
|
||||||
|
the connection is closed are lost - just like with a UART. */
|
||||||
|
|
||||||
|
// TODO - ZE - check behavior on different OSes and test what happens if an
|
||||||
|
// open connection isn't broken cleanly (cable is yanked out, host dies
|
||||||
|
// or locks up, or host virtual serial port hangs)
|
||||||
|
if (_usbLineInfo.lineState > 0) {
|
||||||
|
int r = USB_Send(CDC_TX,&c,1);
|
||||||
|
if (r > 0) {
|
||||||
|
return r;
|
||||||
|
} else {
|
||||||
|
setWriteError();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setWriteError();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This operator is a convenient way for a sketch to check whether the
|
||||||
|
// port has actually been configured and opened by the host (as opposed
|
||||||
|
// to just being connected to the host). It can be used, for example, in
|
||||||
|
// setup() before printing to ensure that an application on the host is
|
||||||
|
// actually ready to receive and display the data.
|
||||||
|
// We add a short delay before returning to fix a bug observed by Federico
|
||||||
|
// where the port is configured (lineState != 0) but not quite opened.
|
||||||
|
Serial_::operator bool() {
|
||||||
|
bool result = false;
|
||||||
|
if (_usbLineInfo.lineState > 0)
|
||||||
|
result = true;
|
||||||
|
delay(10);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial_ Serial;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#endif /* if defined(USBCON) */
|
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef client_h
|
||||||
|
#define client_h
|
||||||
|
#include "Print.h"
|
||||||
|
#include "Stream.h"
|
||||||
|
#include "IPAddress.h"
|
||||||
|
|
||||||
|
class Client : public Stream {
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual int connect(IPAddress ip, uint16_t port) =0;
|
||||||
|
virtual int connect(const char *host, uint16_t port) =0;
|
||||||
|
virtual size_t write(uint8_t) =0;
|
||||||
|
virtual size_t write(const uint8_t *buf, size_t size) =0;
|
||||||
|
virtual int available() = 0;
|
||||||
|
virtual int read() = 0;
|
||||||
|
virtual int read(uint8_t *buf, size_t size) = 0;
|
||||||
|
virtual int peek() = 0;
|
||||||
|
virtual void flush() = 0;
|
||||||
|
virtual void stop() = 0;
|
||||||
|
virtual uint8_t connected() = 0;
|
||||||
|
virtual operator bool() = 0;
|
||||||
|
protected:
|
||||||
|
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,520 @@
|
|||||||
|
|
||||||
|
|
||||||
|
/* Copyright (c) 2011, Peter Barrett
|
||||||
|
**
|
||||||
|
** Permission to use, copy, modify, and/or distribute this software for
|
||||||
|
** any purpose with or without fee is hereby granted, provided that the
|
||||||
|
** above copyright notice and this permission notice appear in all copies.
|
||||||
|
**
|
||||||
|
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||||
|
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||||
|
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
||||||
|
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
||||||
|
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
||||||
|
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||||
|
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||||
|
** SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Platform.h"
|
||||||
|
#include "USBAPI.h"
|
||||||
|
#include "USBDesc.h"
|
||||||
|
|
||||||
|
#if defined(USBCON)
|
||||||
|
#ifdef HID_ENABLED
|
||||||
|
|
||||||
|
//#define RAWHID_ENABLED
|
||||||
|
|
||||||
|
// Singletons for mouse and keyboard
|
||||||
|
|
||||||
|
Mouse_ Mouse;
|
||||||
|
Keyboard_ Keyboard;
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
|
||||||
|
// HID report descriptor
|
||||||
|
|
||||||
|
#define LSB(_x) ((_x) & 0xFF)
|
||||||
|
#define MSB(_x) ((_x) >> 8)
|
||||||
|
|
||||||
|
#define RAWHID_USAGE_PAGE 0xFFC0
|
||||||
|
#define RAWHID_USAGE 0x0C00
|
||||||
|
#define RAWHID_TX_SIZE 64
|
||||||
|
#define RAWHID_RX_SIZE 64
|
||||||
|
|
||||||
|
extern const u8 _hidReportDescriptor[] PROGMEM;
|
||||||
|
const u8 _hidReportDescriptor[] = {
|
||||||
|
|
||||||
|
// Mouse
|
||||||
|
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54
|
||||||
|
0x09, 0x02, // USAGE (Mouse)
|
||||||
|
0xa1, 0x01, // COLLECTION (Application)
|
||||||
|
0x09, 0x01, // USAGE (Pointer)
|
||||||
|
0xa1, 0x00, // COLLECTION (Physical)
|
||||||
|
0x85, 0x01, // REPORT_ID (1)
|
||||||
|
0x05, 0x09, // USAGE_PAGE (Button)
|
||||||
|
0x19, 0x01, // USAGE_MINIMUM (Button 1)
|
||||||
|
0x29, 0x03, // USAGE_MAXIMUM (Button 3)
|
||||||
|
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
||||||
|
0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
||||||
|
0x95, 0x03, // REPORT_COUNT (3)
|
||||||
|
0x75, 0x01, // REPORT_SIZE (1)
|
||||||
|
0x81, 0x02, // INPUT (Data,Var,Abs)
|
||||||
|
0x95, 0x01, // REPORT_COUNT (1)
|
||||||
|
0x75, 0x05, // REPORT_SIZE (5)
|
||||||
|
0x81, 0x03, // INPUT (Cnst,Var,Abs)
|
||||||
|
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
|
||||||
|
0x09, 0x30, // USAGE (X)
|
||||||
|
0x09, 0x31, // USAGE (Y)
|
||||||
|
0x09, 0x38, // USAGE (Wheel)
|
||||||
|
0x15, 0x81, // LOGICAL_MINIMUM (-127)
|
||||||
|
0x25, 0x7f, // LOGICAL_MAXIMUM (127)
|
||||||
|
0x75, 0x08, // REPORT_SIZE (8)
|
||||||
|
0x95, 0x03, // REPORT_COUNT (3)
|
||||||
|
0x81, 0x06, // INPUT (Data,Var,Rel)
|
||||||
|
0xc0, // END_COLLECTION
|
||||||
|
0xc0, // END_COLLECTION
|
||||||
|
|
||||||
|
// Keyboard
|
||||||
|
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
|
||||||
|
0x09, 0x06, // USAGE (Keyboard)
|
||||||
|
0xa1, 0x01, // COLLECTION (Application)
|
||||||
|
0x85, 0x02, // REPORT_ID (2)
|
||||||
|
0x05, 0x07, // USAGE_PAGE (Keyboard)
|
||||||
|
|
||||||
|
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
|
||||||
|
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
|
||||||
|
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
||||||
|
0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
||||||
|
0x75, 0x01, // REPORT_SIZE (1)
|
||||||
|
|
||||||
|
0x95, 0x08, // REPORT_COUNT (8)
|
||||||
|
0x81, 0x02, // INPUT (Data,Var,Abs)
|
||||||
|
0x95, 0x01, // REPORT_COUNT (1)
|
||||||
|
0x75, 0x08, // REPORT_SIZE (8)
|
||||||
|
0x81, 0x03, // INPUT (Cnst,Var,Abs)
|
||||||
|
|
||||||
|
0x95, 0x06, // REPORT_COUNT (6)
|
||||||
|
0x75, 0x08, // REPORT_SIZE (8)
|
||||||
|
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
||||||
|
0x25, 0x65, // LOGICAL_MAXIMUM (101)
|
||||||
|
0x05, 0x07, // USAGE_PAGE (Keyboard)
|
||||||
|
|
||||||
|
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
|
||||||
|
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
|
||||||
|
0x81, 0x00, // INPUT (Data,Ary,Abs)
|
||||||
|
0xc0, // END_COLLECTION
|
||||||
|
|
||||||
|
#if RAWHID_ENABLED
|
||||||
|
// RAW HID
|
||||||
|
0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30
|
||||||
|
0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
|
||||||
|
|
||||||
|
0xA1, 0x01, // Collection 0x01
|
||||||
|
0x85, 0x03, // REPORT_ID (3)
|
||||||
|
0x75, 0x08, // report size = 8 bits
|
||||||
|
0x15, 0x00, // logical minimum = 0
|
||||||
|
0x26, 0xFF, 0x00, // logical maximum = 255
|
||||||
|
|
||||||
|
0x95, 64, // report count TX
|
||||||
|
0x09, 0x01, // usage
|
||||||
|
0x81, 0x02, // Input (array)
|
||||||
|
|
||||||
|
0x95, 64, // report count RX
|
||||||
|
0x09, 0x02, // usage
|
||||||
|
0x91, 0x02, // Output (array)
|
||||||
|
0xC0 // end collection
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
extern const HIDDescriptor _hidInterface PROGMEM;
|
||||||
|
const HIDDescriptor _hidInterface =
|
||||||
|
{
|
||||||
|
D_INTERFACE(HID_INTERFACE,1,3,0,0),
|
||||||
|
D_HIDREPORT(sizeof(_hidReportDescriptor)),
|
||||||
|
D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
|
||||||
|
};
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// Driver
|
||||||
|
|
||||||
|
u8 _hid_protocol = 1;
|
||||||
|
u8 _hid_idle = 1;
|
||||||
|
|
||||||
|
#define WEAK __attribute__ ((weak))
|
||||||
|
|
||||||
|
int WEAK HID_GetInterface(u8* interfaceNum)
|
||||||
|
{
|
||||||
|
interfaceNum[0] += 1; // uses 1
|
||||||
|
return USB_SendControl(TRANSFER_PGM,&_hidInterface,sizeof(_hidInterface));
|
||||||
|
}
|
||||||
|
|
||||||
|
int WEAK HID_GetDescriptor(int i)
|
||||||
|
{
|
||||||
|
return USB_SendControl(TRANSFER_PGM,_hidReportDescriptor,sizeof(_hidReportDescriptor));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WEAK HID_SendReport(u8 id, const void* data, int len)
|
||||||
|
{
|
||||||
|
USB_Send(HID_TX, &id, 1);
|
||||||
|
USB_Send(HID_TX | TRANSFER_RELEASE,data,len);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WEAK HID_Setup(Setup& setup)
|
||||||
|
{
|
||||||
|
u8 r = setup.bRequest;
|
||||||
|
u8 requestType = setup.bmRequestType;
|
||||||
|
if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
|
||||||
|
{
|
||||||
|
if (HID_GET_REPORT == r)
|
||||||
|
{
|
||||||
|
//HID_GetReport();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (HID_GET_PROTOCOL == r)
|
||||||
|
{
|
||||||
|
//Send8(_hid_protocol); // TODO
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
|
||||||
|
{
|
||||||
|
if (HID_SET_PROTOCOL == r)
|
||||||
|
{
|
||||||
|
_hid_protocol = setup.wValueL;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (HID_SET_IDLE == r)
|
||||||
|
{
|
||||||
|
_hid_idle = setup.wValueL;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// Mouse
|
||||||
|
|
||||||
|
Mouse_::Mouse_(void) : _buttons(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mouse_::begin(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mouse_::end(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mouse_::click(uint8_t b)
|
||||||
|
{
|
||||||
|
_buttons = b;
|
||||||
|
move(0,0,0);
|
||||||
|
_buttons = 0;
|
||||||
|
move(0,0,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mouse_::move(signed char x, signed char y, signed char wheel)
|
||||||
|
{
|
||||||
|
u8 m[4];
|
||||||
|
m[0] = _buttons;
|
||||||
|
m[1] = x;
|
||||||
|
m[2] = y;
|
||||||
|
m[3] = wheel;
|
||||||
|
HID_SendReport(1,m,4);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mouse_::buttons(uint8_t b)
|
||||||
|
{
|
||||||
|
if (b != _buttons)
|
||||||
|
{
|
||||||
|
_buttons = b;
|
||||||
|
move(0,0,0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mouse_::press(uint8_t b)
|
||||||
|
{
|
||||||
|
buttons(_buttons | b);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mouse_::release(uint8_t b)
|
||||||
|
{
|
||||||
|
buttons(_buttons & ~b);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Mouse_::isPressed(uint8_t b)
|
||||||
|
{
|
||||||
|
if ((b & _buttons) > 0)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// Keyboard
|
||||||
|
|
||||||
|
Keyboard_::Keyboard_(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Keyboard_::begin(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Keyboard_::end(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Keyboard_::sendReport(KeyReport* keys)
|
||||||
|
{
|
||||||
|
HID_SendReport(2,keys,sizeof(KeyReport));
|
||||||
|
}
|
||||||
|
|
||||||
|
extern
|
||||||
|
const uint8_t _asciimap[128] PROGMEM;
|
||||||
|
|
||||||
|
#define SHIFT 0x80
|
||||||
|
const uint8_t _asciimap[128] =
|
||||||
|
{
|
||||||
|
0x00, // NUL
|
||||||
|
0x00, // SOH
|
||||||
|
0x00, // STX
|
||||||
|
0x00, // ETX
|
||||||
|
0x00, // EOT
|
||||||
|
0x00, // ENQ
|
||||||
|
0x00, // ACK
|
||||||
|
0x00, // BEL
|
||||||
|
0x2a, // BS Backspace
|
||||||
|
0x2b, // TAB Tab
|
||||||
|
0x28, // LF Enter
|
||||||
|
0x00, // VT
|
||||||
|
0x00, // FF
|
||||||
|
0x00, // CR
|
||||||
|
0x00, // SO
|
||||||
|
0x00, // SI
|
||||||
|
0x00, // DEL
|
||||||
|
0x00, // DC1
|
||||||
|
0x00, // DC2
|
||||||
|
0x00, // DC3
|
||||||
|
0x00, // DC4
|
||||||
|
0x00, // NAK
|
||||||
|
0x00, // SYN
|
||||||
|
0x00, // ETB
|
||||||
|
0x00, // CAN
|
||||||
|
0x00, // EM
|
||||||
|
0x00, // SUB
|
||||||
|
0x00, // ESC
|
||||||
|
0x00, // FS
|
||||||
|
0x00, // GS
|
||||||
|
0x00, // RS
|
||||||
|
0x00, // US
|
||||||
|
|
||||||
|
0x2c, // ' '
|
||||||
|
0x1e|SHIFT, // !
|
||||||
|
0x34|SHIFT, // "
|
||||||
|
0x20|SHIFT, // #
|
||||||
|
0x21|SHIFT, // $
|
||||||
|
0x22|SHIFT, // %
|
||||||
|
0x24|SHIFT, // &
|
||||||
|
0x34, // '
|
||||||
|
0x26|SHIFT, // (
|
||||||
|
0x27|SHIFT, // )
|
||||||
|
0x25|SHIFT, // *
|
||||||
|
0x2e|SHIFT, // +
|
||||||
|
0x36, // ,
|
||||||
|
0x2d, // -
|
||||||
|
0x37, // .
|
||||||
|
0x38, // /
|
||||||
|
0x27, // 0
|
||||||
|
0x1e, // 1
|
||||||
|
0x1f, // 2
|
||||||
|
0x20, // 3
|
||||||
|
0x21, // 4
|
||||||
|
0x22, // 5
|
||||||
|
0x23, // 6
|
||||||
|
0x24, // 7
|
||||||
|
0x25, // 8
|
||||||
|
0x26, // 9
|
||||||
|
0x33|SHIFT, // :
|
||||||
|
0x33, // ;
|
||||||
|
0x36|SHIFT, // <
|
||||||
|
0x2e, // =
|
||||||
|
0x37|SHIFT, // >
|
||||||
|
0x38|SHIFT, // ?
|
||||||
|
0x1f|SHIFT, // @
|
||||||
|
0x04|SHIFT, // A
|
||||||
|
0x05|SHIFT, // B
|
||||||
|
0x06|SHIFT, // C
|
||||||
|
0x07|SHIFT, // D
|
||||||
|
0x08|SHIFT, // E
|
||||||
|
0x09|SHIFT, // F
|
||||||
|
0x0a|SHIFT, // G
|
||||||
|
0x0b|SHIFT, // H
|
||||||
|
0x0c|SHIFT, // I
|
||||||
|
0x0d|SHIFT, // J
|
||||||
|
0x0e|SHIFT, // K
|
||||||
|
0x0f|SHIFT, // L
|
||||||
|
0x10|SHIFT, // M
|
||||||
|
0x11|SHIFT, // N
|
||||||
|
0x12|SHIFT, // O
|
||||||
|
0x13|SHIFT, // P
|
||||||
|
0x14|SHIFT, // Q
|
||||||
|
0x15|SHIFT, // R
|
||||||
|
0x16|SHIFT, // S
|
||||||
|
0x17|SHIFT, // T
|
||||||
|
0x18|SHIFT, // U
|
||||||
|
0x19|SHIFT, // V
|
||||||
|
0x1a|SHIFT, // W
|
||||||
|
0x1b|SHIFT, // X
|
||||||
|
0x1c|SHIFT, // Y
|
||||||
|
0x1d|SHIFT, // Z
|
||||||
|
0x2f, // [
|
||||||
|
0x31, // bslash
|
||||||
|
0x30, // ]
|
||||||
|
0x23|SHIFT, // ^
|
||||||
|
0x2d|SHIFT, // _
|
||||||
|
0x35, // `
|
||||||
|
0x04, // a
|
||||||
|
0x05, // b
|
||||||
|
0x06, // c
|
||||||
|
0x07, // d
|
||||||
|
0x08, // e
|
||||||
|
0x09, // f
|
||||||
|
0x0a, // g
|
||||||
|
0x0b, // h
|
||||||
|
0x0c, // i
|
||||||
|
0x0d, // j
|
||||||
|
0x0e, // k
|
||||||
|
0x0f, // l
|
||||||
|
0x10, // m
|
||||||
|
0x11, // n
|
||||||
|
0x12, // o
|
||||||
|
0x13, // p
|
||||||
|
0x14, // q
|
||||||
|
0x15, // r
|
||||||
|
0x16, // s
|
||||||
|
0x17, // t
|
||||||
|
0x18, // u
|
||||||
|
0x19, // v
|
||||||
|
0x1a, // w
|
||||||
|
0x1b, // x
|
||||||
|
0x1c, // y
|
||||||
|
0x1d, // z
|
||||||
|
0x2f|SHIFT, //
|
||||||
|
0x31|SHIFT, // |
|
||||||
|
0x30|SHIFT, // }
|
||||||
|
0x35|SHIFT, // ~
|
||||||
|
0 // DEL
|
||||||
|
};
|
||||||
|
|
||||||
|
uint8_t USBPutChar(uint8_t c);
|
||||||
|
|
||||||
|
// press() adds the specified key (printing, non-printing, or modifier)
|
||||||
|
// to the persistent key report and sends the report. Because of the way
|
||||||
|
// USB HID works, the host acts like the key remains pressed until we
|
||||||
|
// call release(), releaseAll(), or otherwise clear the report and resend.
|
||||||
|
size_t Keyboard_::press(uint8_t k)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
if (k >= 136) { // it's a non-printing key (not a modifier)
|
||||||
|
k = k - 136;
|
||||||
|
} else if (k >= 128) { // it's a modifier key
|
||||||
|
_keyReport.modifiers |= (1<<(k-128));
|
||||||
|
k = 0;
|
||||||
|
} else { // it's a printing key
|
||||||
|
k = pgm_read_byte(_asciimap + k);
|
||||||
|
if (!k) {
|
||||||
|
setWriteError();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (k & 0x80) { // it's a capital letter or other character reached with shift
|
||||||
|
_keyReport.modifiers |= 0x02; // the left shift modifier
|
||||||
|
k &= 0x7F;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add k to the key report only if it's not already present
|
||||||
|
// and if there is an empty slot.
|
||||||
|
if (_keyReport.keys[0] != k && _keyReport.keys[1] != k &&
|
||||||
|
_keyReport.keys[2] != k && _keyReport.keys[3] != k &&
|
||||||
|
_keyReport.keys[4] != k && _keyReport.keys[5] != k) {
|
||||||
|
|
||||||
|
for (i=0; i<6; i++) {
|
||||||
|
if (_keyReport.keys[i] == 0x00) {
|
||||||
|
_keyReport.keys[i] = k;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i == 6) {
|
||||||
|
setWriteError();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sendReport(&_keyReport);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// release() takes the specified key out of the persistent key report and
|
||||||
|
// sends the report. This tells the OS the key is no longer pressed and that
|
||||||
|
// it shouldn't be repeated any more.
|
||||||
|
size_t Keyboard_::release(uint8_t k)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
if (k >= 136) { // it's a non-printing key (not a modifier)
|
||||||
|
k = k - 136;
|
||||||
|
} else if (k >= 128) { // it's a modifier key
|
||||||
|
_keyReport.modifiers &= ~(1<<(k-128));
|
||||||
|
k = 0;
|
||||||
|
} else { // it's a printing key
|
||||||
|
k = pgm_read_byte(_asciimap + k);
|
||||||
|
if (!k) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (k & 0x80) { // it's a capital letter or other character reached with shift
|
||||||
|
_keyReport.modifiers &= ~(0x02); // the left shift modifier
|
||||||
|
k &= 0x7F;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test the key report to see if k is present. Clear it if it exists.
|
||||||
|
// Check all positions in case the key is present more than once (which it shouldn't be)
|
||||||
|
for (i=0; i<6; i++) {
|
||||||
|
if (0 != k && _keyReport.keys[i] == k) {
|
||||||
|
_keyReport.keys[i] = 0x00;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sendReport(&_keyReport);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Keyboard_::releaseAll(void)
|
||||||
|
{
|
||||||
|
_keyReport.keys[0] = 0;
|
||||||
|
_keyReport.keys[1] = 0;
|
||||||
|
_keyReport.keys[2] = 0;
|
||||||
|
_keyReport.keys[3] = 0;
|
||||||
|
_keyReport.keys[4] = 0;
|
||||||
|
_keyReport.keys[5] = 0;
|
||||||
|
_keyReport.modifiers = 0;
|
||||||
|
sendReport(&_keyReport);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Keyboard_::write(uint8_t c)
|
||||||
|
{
|
||||||
|
uint8_t p = press(c); // Keydown
|
||||||
|
uint8_t r = release(c); // Keyup
|
||||||
|
return (p); // just return the result of press() since release() almost always returns 1
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* if defined(USBCON) */
|
@ -0,0 +1,519 @@
|
|||||||
|
/*
|
||||||
|
HardwareSerial.cpp - Hardware serial library for Wiring
|
||||||
|
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
|
Modified 23 November 2006 by David A. Mellis
|
||||||
|
Modified 28 September 2010 by Mark Sproul
|
||||||
|
Modified 14 August 2012 by Alarus
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include "Arduino.h"
|
||||||
|
#include "wiring_private.h"
|
||||||
|
|
||||||
|
// this next line disables the entire HardwareSerial.cpp,
|
||||||
|
// this is so I can support Attiny series and any other chip without a uart
|
||||||
|
#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
|
||||||
|
|
||||||
|
#include "HardwareSerial.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* on ATmega8, the uart and its bits are not numbered, so there is no "TXC0"
|
||||||
|
* definition.
|
||||||
|
*/
|
||||||
|
#if !defined(TXC0)
|
||||||
|
#if defined(TXC)
|
||||||
|
#define TXC0 TXC
|
||||||
|
#elif defined(TXC1)
|
||||||
|
// Some devices have uart1 but no uart0
|
||||||
|
#define TXC0 TXC1
|
||||||
|
#else
|
||||||
|
#error TXC0 not definable in HardwareSerial.h
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Define constants and variables for buffering incoming serial data. We're
|
||||||
|
// using a ring buffer (I think), in which head is the index of the location
|
||||||
|
// to which to write the next incoming character and tail is the index of the
|
||||||
|
// location from which to read.
|
||||||
|
#if (RAMEND < 1000)
|
||||||
|
#define SERIAL_BUFFER_SIZE 16
|
||||||
|
#else
|
||||||
|
#define SERIAL_BUFFER_SIZE 64
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct ring_buffer
|
||||||
|
{
|
||||||
|
unsigned char buffer[SERIAL_BUFFER_SIZE];
|
||||||
|
volatile unsigned int head;
|
||||||
|
volatile unsigned int tail;
|
||||||
|
};
|
||||||
|
|
||||||
|
#if defined(USBCON)
|
||||||
|
ring_buffer rx_buffer = { { 0 }, 0, 0};
|
||||||
|
ring_buffer tx_buffer = { { 0 }, 0, 0};
|
||||||
|
#endif
|
||||||
|
#if defined(UBRRH) || defined(UBRR0H)
|
||||||
|
ring_buffer rx_buffer = { { 0 }, 0, 0 };
|
||||||
|
ring_buffer tx_buffer = { { 0 }, 0, 0 };
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR1H)
|
||||||
|
ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
|
||||||
|
ring_buffer tx_buffer1 = { { 0 }, 0, 0 };
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR2H)
|
||||||
|
ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
|
||||||
|
ring_buffer tx_buffer2 = { { 0 }, 0, 0 };
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR3H)
|
||||||
|
ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
|
||||||
|
ring_buffer tx_buffer3 = { { 0 }, 0, 0 };
|
||||||
|
#endif
|
||||||
|
|
||||||
|
inline void store_char(unsigned char c, ring_buffer *buffer)
|
||||||
|
{
|
||||||
|
int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
|
||||||
|
|
||||||
|
// if we should be storing the received character into the location
|
||||||
|
// just before the tail (meaning that the head would advance to the
|
||||||
|
// current location of the tail), we're about to overflow the buffer
|
||||||
|
// and so we don't write the character or advance the head.
|
||||||
|
if (i != buffer->tail) {
|
||||||
|
buffer->buffer[buffer->head] = c;
|
||||||
|
buffer->head = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined(USART0_RX_vect) && defined(USART1_RX_vect)
|
||||||
|
// do nothing - on the 32u4 the first USART is USART1
|
||||||
|
#else
|
||||||
|
#if !defined(USART_RX_vect) && !defined(SIG_USART0_RECV) && \
|
||||||
|
!defined(SIG_UART0_RECV) && !defined(USART0_RX_vect) && \
|
||||||
|
!defined(SIG_UART_RECV)
|
||||||
|
#error "Don't know what the Data Received vector is called for the first UART"
|
||||||
|
#else
|
||||||
|
void serialEvent() __attribute__((weak));
|
||||||
|
void serialEvent() {}
|
||||||
|
#define serialEvent_implemented
|
||||||
|
#if defined(USART_RX_vect)
|
||||||
|
SIGNAL(USART_RX_vect)
|
||||||
|
#elif defined(SIG_USART0_RECV)
|
||||||
|
SIGNAL(SIG_USART0_RECV)
|
||||||
|
#elif defined(SIG_UART0_RECV)
|
||||||
|
SIGNAL(SIG_UART0_RECV)
|
||||||
|
#elif defined(USART0_RX_vect)
|
||||||
|
SIGNAL(USART0_RX_vect)
|
||||||
|
#elif defined(SIG_UART_RECV)
|
||||||
|
SIGNAL(SIG_UART_RECV)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
#if defined(UDR0)
|
||||||
|
if (bit_is_clear(UCSR0A, UPE0)) {
|
||||||
|
unsigned char c = UDR0;
|
||||||
|
store_char(c, &rx_buffer);
|
||||||
|
} else {
|
||||||
|
unsigned char c = UDR0;
|
||||||
|
};
|
||||||
|
#elif defined(UDR)
|
||||||
|
if (bit_is_clear(UCSRA, PE)) {
|
||||||
|
unsigned char c = UDR;
|
||||||
|
store_char(c, &rx_buffer);
|
||||||
|
} else {
|
||||||
|
unsigned char c = UDR;
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
#error UDR not defined
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(USART1_RX_vect)
|
||||||
|
void serialEvent1() __attribute__((weak));
|
||||||
|
void serialEvent1() {}
|
||||||
|
#define serialEvent1_implemented
|
||||||
|
SIGNAL(USART1_RX_vect)
|
||||||
|
{
|
||||||
|
if (bit_is_clear(UCSR1A, UPE1)) {
|
||||||
|
unsigned char c = UDR1;
|
||||||
|
store_char(c, &rx_buffer1);
|
||||||
|
} else {
|
||||||
|
unsigned char c = UDR1;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#elif defined(SIG_USART1_RECV)
|
||||||
|
#error SIG_USART1_RECV
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(USART2_RX_vect) && defined(UDR2)
|
||||||
|
void serialEvent2() __attribute__((weak));
|
||||||
|
void serialEvent2() {}
|
||||||
|
#define serialEvent2_implemented
|
||||||
|
SIGNAL(USART2_RX_vect)
|
||||||
|
{
|
||||||
|
if (bit_is_clear(UCSR2A, UPE2)) {
|
||||||
|
unsigned char c = UDR2;
|
||||||
|
store_char(c, &rx_buffer2);
|
||||||
|
} else {
|
||||||
|
unsigned char c = UDR2;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#elif defined(SIG_USART2_RECV)
|
||||||
|
#error SIG_USART2_RECV
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(USART3_RX_vect) && defined(UDR3)
|
||||||
|
void serialEvent3() __attribute__((weak));
|
||||||
|
void serialEvent3() {}
|
||||||
|
#define serialEvent3_implemented
|
||||||
|
SIGNAL(USART3_RX_vect)
|
||||||
|
{
|
||||||
|
if (bit_is_clear(UCSR3A, UPE3)) {
|
||||||
|
unsigned char c = UDR3;
|
||||||
|
store_char(c, &rx_buffer3);
|
||||||
|
} else {
|
||||||
|
unsigned char c = UDR3;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#elif defined(SIG_USART3_RECV)
|
||||||
|
#error SIG_USART3_RECV
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void serialEventRun(void)
|
||||||
|
{
|
||||||
|
#ifdef serialEvent_implemented
|
||||||
|
if (Serial.available()) serialEvent();
|
||||||
|
#endif
|
||||||
|
#ifdef serialEvent1_implemented
|
||||||
|
if (Serial1.available()) serialEvent1();
|
||||||
|
#endif
|
||||||
|
#ifdef serialEvent2_implemented
|
||||||
|
if (Serial2.available()) serialEvent2();
|
||||||
|
#endif
|
||||||
|
#ifdef serialEvent3_implemented
|
||||||
|
if (Serial3.available()) serialEvent3();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(USART0_UDRE_vect) && defined(USART1_UDRE_vect)
|
||||||
|
// do nothing - on the 32u4 the first USART is USART1
|
||||||
|
#else
|
||||||
|
#if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
|
||||||
|
#error "Don't know what the Data Register Empty vector is called for the first UART"
|
||||||
|
#else
|
||||||
|
#if defined(UART0_UDRE_vect)
|
||||||
|
ISR(UART0_UDRE_vect)
|
||||||
|
#elif defined(UART_UDRE_vect)
|
||||||
|
ISR(UART_UDRE_vect)
|
||||||
|
#elif defined(USART0_UDRE_vect)
|
||||||
|
ISR(USART0_UDRE_vect)
|
||||||
|
#elif defined(USART_UDRE_vect)
|
||||||
|
ISR(USART_UDRE_vect)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
if (tx_buffer.head == tx_buffer.tail) {
|
||||||
|
// Buffer empty, so disable interrupts
|
||||||
|
#if defined(UCSR0B)
|
||||||
|
cbi(UCSR0B, UDRIE0);
|
||||||
|
#else
|
||||||
|
cbi(UCSRB, UDRIE);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// There is more data in the output buffer. Send the next byte
|
||||||
|
unsigned char c = tx_buffer.buffer[tx_buffer.tail];
|
||||||
|
tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
|
||||||
|
|
||||||
|
#if defined(UDR0)
|
||||||
|
UDR0 = c;
|
||||||
|
#elif defined(UDR)
|
||||||
|
UDR = c;
|
||||||
|
#else
|
||||||
|
#error UDR not defined
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef USART1_UDRE_vect
|
||||||
|
ISR(USART1_UDRE_vect)
|
||||||
|
{
|
||||||
|
if (tx_buffer1.head == tx_buffer1.tail) {
|
||||||
|
// Buffer empty, so disable interrupts
|
||||||
|
cbi(UCSR1B, UDRIE1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// There is more data in the output buffer. Send the next byte
|
||||||
|
unsigned char c = tx_buffer1.buffer[tx_buffer1.tail];
|
||||||
|
tx_buffer1.tail = (tx_buffer1.tail + 1) % SERIAL_BUFFER_SIZE;
|
||||||
|
|
||||||
|
UDR1 = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef USART2_UDRE_vect
|
||||||
|
ISR(USART2_UDRE_vect)
|
||||||
|
{
|
||||||
|
if (tx_buffer2.head == tx_buffer2.tail) {
|
||||||
|
// Buffer empty, so disable interrupts
|
||||||
|
cbi(UCSR2B, UDRIE2);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// There is more data in the output buffer. Send the next byte
|
||||||
|
unsigned char c = tx_buffer2.buffer[tx_buffer2.tail];
|
||||||
|
tx_buffer2.tail = (tx_buffer2.tail + 1) % SERIAL_BUFFER_SIZE;
|
||||||
|
|
||||||
|
UDR2 = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef USART3_UDRE_vect
|
||||||
|
ISR(USART3_UDRE_vect)
|
||||||
|
{
|
||||||
|
if (tx_buffer3.head == tx_buffer3.tail) {
|
||||||
|
// Buffer empty, so disable interrupts
|
||||||
|
cbi(UCSR3B, UDRIE3);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// There is more data in the output buffer. Send the next byte
|
||||||
|
unsigned char c = tx_buffer3.buffer[tx_buffer3.tail];
|
||||||
|
tx_buffer3.tail = (tx_buffer3.tail + 1) % SERIAL_BUFFER_SIZE;
|
||||||
|
|
||||||
|
UDR3 = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// Constructors ////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
|
||||||
|
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
|
||||||
|
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
|
||||||
|
volatile uint8_t *ucsrc, volatile uint8_t *udr,
|
||||||
|
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x)
|
||||||
|
{
|
||||||
|
_rx_buffer = rx_buffer;
|
||||||
|
_tx_buffer = tx_buffer;
|
||||||
|
_ubrrh = ubrrh;
|
||||||
|
_ubrrl = ubrrl;
|
||||||
|
_ucsra = ucsra;
|
||||||
|
_ucsrb = ucsrb;
|
||||||
|
_ucsrc = ucsrc;
|
||||||
|
_udr = udr;
|
||||||
|
_rxen = rxen;
|
||||||
|
_txen = txen;
|
||||||
|
_rxcie = rxcie;
|
||||||
|
_udrie = udrie;
|
||||||
|
_u2x = u2x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Public Methods //////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void HardwareSerial::begin(unsigned long baud)
|
||||||
|
{
|
||||||
|
uint16_t baud_setting;
|
||||||
|
bool use_u2x = true;
|
||||||
|
|
||||||
|
#if F_CPU == 16000000UL
|
||||||
|
// hardcoded exception for compatibility with the bootloader shipped
|
||||||
|
// with the Duemilanove and previous boards and the firmware on the 8U2
|
||||||
|
// on the Uno and Mega 2560.
|
||||||
|
if (baud == 57600) {
|
||||||
|
use_u2x = false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
try_again:
|
||||||
|
|
||||||
|
if (use_u2x) {
|
||||||
|
*_ucsra = 1 << _u2x;
|
||||||
|
baud_setting = (F_CPU / 4 / baud - 1) / 2;
|
||||||
|
} else {
|
||||||
|
*_ucsra = 0;
|
||||||
|
baud_setting = (F_CPU / 8 / baud - 1) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((baud_setting > 4095) && use_u2x)
|
||||||
|
{
|
||||||
|
use_u2x = false;
|
||||||
|
goto try_again;
|
||||||
|
}
|
||||||
|
|
||||||
|
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
|
||||||
|
*_ubrrh = baud_setting >> 8;
|
||||||
|
*_ubrrl = baud_setting;
|
||||||
|
|
||||||
|
transmitting = false;
|
||||||
|
|
||||||
|
sbi(*_ucsrb, _rxen);
|
||||||
|
sbi(*_ucsrb, _txen);
|
||||||
|
sbi(*_ucsrb, _rxcie);
|
||||||
|
cbi(*_ucsrb, _udrie);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HardwareSerial::begin(unsigned long baud, byte config)
|
||||||
|
{
|
||||||
|
uint16_t baud_setting;
|
||||||
|
uint8_t current_config;
|
||||||
|
bool use_u2x = true;
|
||||||
|
|
||||||
|
#if F_CPU == 16000000UL
|
||||||
|
// hardcoded exception for compatibility with the bootloader shipped
|
||||||
|
// with the Duemilanove and previous boards and the firmware on the 8U2
|
||||||
|
// on the Uno and Mega 2560.
|
||||||
|
if (baud == 57600) {
|
||||||
|
use_u2x = false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
try_again:
|
||||||
|
|
||||||
|
if (use_u2x) {
|
||||||
|
*_ucsra = 1 << _u2x;
|
||||||
|
baud_setting = (F_CPU / 4 / baud - 1) / 2;
|
||||||
|
} else {
|
||||||
|
*_ucsra = 0;
|
||||||
|
baud_setting = (F_CPU / 8 / baud - 1) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((baud_setting > 4095) && use_u2x)
|
||||||
|
{
|
||||||
|
use_u2x = false;
|
||||||
|
goto try_again;
|
||||||
|
}
|
||||||
|
|
||||||
|
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
|
||||||
|
*_ubrrh = baud_setting >> 8;
|
||||||
|
*_ubrrl = baud_setting;
|
||||||
|
|
||||||
|
//set the data bits, parity, and stop bits
|
||||||
|
#if defined(__AVR_ATmega8__)
|
||||||
|
config |= 0x80; // select UCSRC register (shared with UBRRH)
|
||||||
|
#endif
|
||||||
|
*_ucsrc = config;
|
||||||
|
|
||||||
|
sbi(*_ucsrb, _rxen);
|
||||||
|
sbi(*_ucsrb, _txen);
|
||||||
|
sbi(*_ucsrb, _rxcie);
|
||||||
|
cbi(*_ucsrb, _udrie);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HardwareSerial::end()
|
||||||
|
{
|
||||||
|
// wait for transmission of outgoing data
|
||||||
|
while (_tx_buffer->head != _tx_buffer->tail)
|
||||||
|
;
|
||||||
|
|
||||||
|
cbi(*_ucsrb, _rxen);
|
||||||
|
cbi(*_ucsrb, _txen);
|
||||||
|
cbi(*_ucsrb, _rxcie);
|
||||||
|
cbi(*_ucsrb, _udrie);
|
||||||
|
|
||||||
|
// clear any received data
|
||||||
|
_rx_buffer->head = _rx_buffer->tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
int HardwareSerial::available(void)
|
||||||
|
{
|
||||||
|
return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int HardwareSerial::peek(void)
|
||||||
|
{
|
||||||
|
if (_rx_buffer->head == _rx_buffer->tail) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
return _rx_buffer->buffer[_rx_buffer->tail];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int HardwareSerial::read(void)
|
||||||
|
{
|
||||||
|
// if the head isn't ahead of the tail, we don't have any characters
|
||||||
|
if (_rx_buffer->head == _rx_buffer->tail) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
|
||||||
|
_rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void HardwareSerial::flush()
|
||||||
|
{
|
||||||
|
// UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
|
||||||
|
while (transmitting && ! (*_ucsra & _BV(TXC0)));
|
||||||
|
transmitting = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t HardwareSerial::write(uint8_t c)
|
||||||
|
{
|
||||||
|
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
|
||||||
|
|
||||||
|
// If the output buffer is full, there's nothing for it other than to
|
||||||
|
// wait for the interrupt handler to empty it a bit
|
||||||
|
// ???: return 0 here instead?
|
||||||
|
while (i == _tx_buffer->tail)
|
||||||
|
;
|
||||||
|
|
||||||
|
_tx_buffer->buffer[_tx_buffer->head] = c;
|
||||||
|
_tx_buffer->head = i;
|
||||||
|
|
||||||
|
sbi(*_ucsrb, _udrie);
|
||||||
|
// clear the TXC bit -- "can be cleared by writing a one to its bit location"
|
||||||
|
transmitting = true;
|
||||||
|
sbi(*_ucsra, TXC0);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
HardwareSerial::operator bool() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preinstantiate Objects //////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#if defined(UBRRH) && defined(UBRRL)
|
||||||
|
HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
|
||||||
|
#elif defined(UBRR0H) && defined(UBRR0L)
|
||||||
|
HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
|
||||||
|
#elif defined(USBCON)
|
||||||
|
// do nothing - Serial object and buffers are initialized in CDC code
|
||||||
|
#else
|
||||||
|
#error no serial port defined (port 0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(UBRR1H)
|
||||||
|
HardwareSerial Serial1(&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR2H)
|
||||||
|
HardwareSerial Serial2(&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR3H)
|
||||||
|
HardwareSerial Serial3(&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // whole file
|
||||||
|
|
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
HardwareSerial.h - Hardware serial library for Wiring
|
||||||
|
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
|
Modified 28 September 2010 by Mark Sproul
|
||||||
|
Modified 14 August 2012 by Alarus
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef HardwareSerial_h
|
||||||
|
#define HardwareSerial_h
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
#include "Stream.h"
|
||||||
|
|
||||||
|
struct ring_buffer;
|
||||||
|
|
||||||
|
class HardwareSerial : public Stream
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
ring_buffer *_rx_buffer;
|
||||||
|
ring_buffer *_tx_buffer;
|
||||||
|
volatile uint8_t *_ubrrh;
|
||||||
|
volatile uint8_t *_ubrrl;
|
||||||
|
volatile uint8_t *_ucsra;
|
||||||
|
volatile uint8_t *_ucsrb;
|
||||||
|
volatile uint8_t *_ucsrc;
|
||||||
|
volatile uint8_t *_udr;
|
||||||
|
uint8_t _rxen;
|
||||||
|
uint8_t _txen;
|
||||||
|
uint8_t _rxcie;
|
||||||
|
uint8_t _udrie;
|
||||||
|
uint8_t _u2x;
|
||||||
|
bool transmitting;
|
||||||
|
public:
|
||||||
|
HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
|
||||||
|
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
|
||||||
|
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
|
||||||
|
volatile uint8_t *ucsrc, volatile uint8_t *udr,
|
||||||
|
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x);
|
||||||
|
void begin(unsigned long);
|
||||||
|
void begin(unsigned long, uint8_t);
|
||||||
|
void end();
|
||||||
|
virtual int available(void);
|
||||||
|
virtual int peek(void);
|
||||||
|
virtual int read(void);
|
||||||
|
virtual void flush(void);
|
||||||
|
virtual size_t write(uint8_t);
|
||||||
|
inline size_t write(unsigned long n) { return write((uint8_t)n); }
|
||||||
|
inline size_t write(long n) { return write((uint8_t)n); }
|
||||||
|
inline size_t write(unsigned int n) { return write((uint8_t)n); }
|
||||||
|
inline size_t write(int n) { return write((uint8_t)n); }
|
||||||
|
using Print::write; // pull in write(str) and write(buf, size) from Print
|
||||||
|
operator bool();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Define config for Serial.begin(baud, config);
|
||||||
|
#define SERIAL_5N1 0x00
|
||||||
|
#define SERIAL_6N1 0x02
|
||||||
|
#define SERIAL_7N1 0x04
|
||||||
|
#define SERIAL_8N1 0x06
|
||||||
|
#define SERIAL_5N2 0x08
|
||||||
|
#define SERIAL_6N2 0x0A
|
||||||
|
#define SERIAL_7N2 0x0C
|
||||||
|
#define SERIAL_8N2 0x0E
|
||||||
|
#define SERIAL_5E1 0x20
|
||||||
|
#define SERIAL_6E1 0x22
|
||||||
|
#define SERIAL_7E1 0x24
|
||||||
|
#define SERIAL_8E1 0x26
|
||||||
|
#define SERIAL_5E2 0x28
|
||||||
|
#define SERIAL_6E2 0x2A
|
||||||
|
#define SERIAL_7E2 0x2C
|
||||||
|
#define SERIAL_8E2 0x2E
|
||||||
|
#define SERIAL_5O1 0x30
|
||||||
|
#define SERIAL_6O1 0x32
|
||||||
|
#define SERIAL_7O1 0x34
|
||||||
|
#define SERIAL_8O1 0x36
|
||||||
|
#define SERIAL_5O2 0x38
|
||||||
|
#define SERIAL_6O2 0x3A
|
||||||
|
#define SERIAL_7O2 0x3C
|
||||||
|
#define SERIAL_8O2 0x3E
|
||||||
|
|
||||||
|
#if defined(UBRRH) || defined(UBRR0H)
|
||||||
|
extern HardwareSerial Serial;
|
||||||
|
#elif defined(USBCON)
|
||||||
|
#include "USBAPI.h"
|
||||||
|
// extern HardwareSerial Serial_;
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR1H)
|
||||||
|
extern HardwareSerial Serial1;
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR2H)
|
||||||
|
extern HardwareSerial Serial2;
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR3H)
|
||||||
|
extern HardwareSerial Serial3;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern void serialEventRun(void) __attribute__((weak));
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,56 @@
|
|||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <IPAddress.h>
|
||||||
|
|
||||||
|
IPAddress::IPAddress()
|
||||||
|
{
|
||||||
|
memset(_address, 0, sizeof(_address));
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
|
||||||
|
{
|
||||||
|
_address[0] = first_octet;
|
||||||
|
_address[1] = second_octet;
|
||||||
|
_address[2] = third_octet;
|
||||||
|
_address[3] = fourth_octet;
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress::IPAddress(uint32_t address)
|
||||||
|
{
|
||||||
|
memcpy(_address, &address, sizeof(_address));
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress::IPAddress(const uint8_t *address)
|
||||||
|
{
|
||||||
|
memcpy(_address, address, sizeof(_address));
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress& IPAddress::operator=(const uint8_t *address)
|
||||||
|
{
|
||||||
|
memcpy(_address, address, sizeof(_address));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress& IPAddress::operator=(uint32_t address)
|
||||||
|
{
|
||||||
|
memcpy(_address, (const uint8_t *)&address, sizeof(_address));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IPAddress::operator==(const uint8_t* addr)
|
||||||
|
{
|
||||||
|
return memcmp(addr, _address, sizeof(_address)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t IPAddress::printTo(Print& p) const
|
||||||
|
{
|
||||||
|
size_t n = 0;
|
||||||
|
for (int i =0; i < 3; i++)
|
||||||
|
{
|
||||||
|
n += p.print(_address[i], DEC);
|
||||||
|
n += p.print('.');
|
||||||
|
}
|
||||||
|
n += p.print(_address[3], DEC);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* MIT License:
|
||||||
|
* Copyright (c) 2011 Adrian McEwen
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*
|
||||||
|
* adrianm@mcqn.com 1/1/2011
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef IPAddress_h
|
||||||
|
#define IPAddress_h
|
||||||
|
|
||||||
|
#include <Printable.h>
|
||||||
|
|
||||||
|
// A class to make it easier to handle and pass around IP addresses
|
||||||
|
|
||||||
|
class IPAddress : public Printable {
|
||||||
|
private:
|
||||||
|
uint8_t _address[4]; // IPv4 address
|
||||||
|
// Access the raw byte array containing the address. Because this returns a pointer
|
||||||
|
// to the internal structure rather than a copy of the address this function should only
|
||||||
|
// be used when you know that the usage of the returned uint8_t* will be transient and not
|
||||||
|
// stored.
|
||||||
|
uint8_t* raw_address() { return _address; };
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructors
|
||||||
|
IPAddress();
|
||||||
|
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
|
||||||
|
IPAddress(uint32_t address);
|
||||||
|
IPAddress(const uint8_t *address);
|
||||||
|
|
||||||
|
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
||||||
|
// to a four-byte uint8_t array is expected
|
||||||
|
operator uint32_t() { return *((uint32_t*)_address); };
|
||||||
|
bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
|
||||||
|
bool operator==(const uint8_t* addr);
|
||||||
|
|
||||||
|
// Overloaded index operator to allow getting and setting individual octets of the address
|
||||||
|
uint8_t operator[](int index) const { return _address[index]; };
|
||||||
|
uint8_t& operator[](int index) { return _address[index]; };
|
||||||
|
|
||||||
|
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
|
||||||
|
IPAddress& operator=(const uint8_t *address);
|
||||||
|
IPAddress& operator=(uint32_t address);
|
||||||
|
|
||||||
|
virtual size_t printTo(Print& p) const;
|
||||||
|
|
||||||
|
friend class EthernetClass;
|
||||||
|
friend class UDP;
|
||||||
|
friend class Client;
|
||||||
|
friend class Server;
|
||||||
|
friend class DhcpClass;
|
||||||
|
friend class DNSClient;
|
||||||
|
};
|
||||||
|
|
||||||
|
const IPAddress INADDR_NONE(0,0,0,0);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
#ifndef __PLATFORM_H__
|
||||||
|
#define __PLATFORM_H__
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#include <avr/eeprom.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <util/delay.h>
|
||||||
|
|
||||||
|
typedef unsigned char u8;
|
||||||
|
typedef unsigned short u16;
|
||||||
|
typedef unsigned long u32;
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
#if defined(USBCON)
|
||||||
|
#include "USBDesc.h"
|
||||||
|
#include "USBCore.h"
|
||||||
|
#include "USBAPI.h"
|
||||||
|
#endif /* if defined(USBCON) */
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,268 @@
|
|||||||
|
/*
|
||||||
|
Print.cpp - Base class that provides print() and println()
|
||||||
|
Copyright (c) 2008 David A. Mellis. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
|
Modified 23 November 2006 by David A. Mellis
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
#include "Print.h"
|
||||||
|
|
||||||
|
// Public Methods //////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/* default implementation: may be overridden */
|
||||||
|
size_t Print::write(const uint8_t *buffer, size_t size)
|
||||||
|
{
|
||||||
|
size_t n = 0;
|
||||||
|
while (size--) {
|
||||||
|
n += write(*buffer++);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(const __FlashStringHelper *ifsh)
|
||||||
|
{
|
||||||
|
const char PROGMEM *p = (const char PROGMEM *)ifsh;
|
||||||
|
size_t n = 0;
|
||||||
|
while (1) {
|
||||||
|
unsigned char c = pgm_read_byte(p++);
|
||||||
|
if (c == 0) break;
|
||||||
|
n += write(c);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(const String &s)
|
||||||
|
{
|
||||||
|
size_t n = 0;
|
||||||
|
for (uint16_t i = 0; i < s.length(); i++) {
|
||||||
|
n += write(s[i]);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(const char str[])
|
||||||
|
{
|
||||||
|
return write(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(char c)
|
||||||
|
{
|
||||||
|
return write(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(unsigned char b, int base)
|
||||||
|
{
|
||||||
|
return print((unsigned long) b, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(int n, int base)
|
||||||
|
{
|
||||||
|
return print((long) n, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(unsigned int n, int base)
|
||||||
|
{
|
||||||
|
return print((unsigned long) n, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(long n, int base)
|
||||||
|
{
|
||||||
|
if (base == 0) {
|
||||||
|
return write(n);
|
||||||
|
} else if (base == 10) {
|
||||||
|
if (n < 0) {
|
||||||
|
int t = print('-');
|
||||||
|
n = -n;
|
||||||
|
return printNumber(n, 10) + t;
|
||||||
|
}
|
||||||
|
return printNumber(n, 10);
|
||||||
|
} else {
|
||||||
|
return printNumber(n, base);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(unsigned long n, int base)
|
||||||
|
{
|
||||||
|
if (base == 0) return write(n);
|
||||||
|
else return printNumber(n, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(double n, int digits)
|
||||||
|
{
|
||||||
|
return printFloat(n, digits);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(const __FlashStringHelper *ifsh)
|
||||||
|
{
|
||||||
|
size_t n = print(ifsh);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::print(const Printable& x)
|
||||||
|
{
|
||||||
|
return x.printTo(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(void)
|
||||||
|
{
|
||||||
|
size_t n = print('\r');
|
||||||
|
n += print('\n');
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(const String &s)
|
||||||
|
{
|
||||||
|
size_t n = print(s);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(const char c[])
|
||||||
|
{
|
||||||
|
size_t n = print(c);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(char c)
|
||||||
|
{
|
||||||
|
size_t n = print(c);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(unsigned char b, int base)
|
||||||
|
{
|
||||||
|
size_t n = print(b, base);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(int num, int base)
|
||||||
|
{
|
||||||
|
size_t n = print(num, base);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(unsigned int num, int base)
|
||||||
|
{
|
||||||
|
size_t n = print(num, base);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(long num, int base)
|
||||||
|
{
|
||||||
|
size_t n = print(num, base);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(unsigned long num, int base)
|
||||||
|
{
|
||||||
|
size_t n = print(num, base);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(double num, int digits)
|
||||||
|
{
|
||||||
|
size_t n = print(num, digits);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::println(const Printable& x)
|
||||||
|
{
|
||||||
|
size_t n = print(x);
|
||||||
|
n += println();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Private Methods /////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
size_t Print::printNumber(unsigned long n, uint8_t base) {
|
||||||
|
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
|
||||||
|
char *str = &buf[sizeof(buf) - 1];
|
||||||
|
|
||||||
|
*str = '\0';
|
||||||
|
|
||||||
|
// prevent crash if called with base == 1
|
||||||
|
if (base < 2) base = 10;
|
||||||
|
|
||||||
|
do {
|
||||||
|
unsigned long m = n;
|
||||||
|
n /= base;
|
||||||
|
char c = m - base * n;
|
||||||
|
*--str = c < 10 ? c + '0' : c + 'A' - 10;
|
||||||
|
} while(n);
|
||||||
|
|
||||||
|
return write(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Print::printFloat(double number, uint8_t digits)
|
||||||
|
{
|
||||||
|
size_t n = 0;
|
||||||
|
|
||||||
|
if (isnan(number)) return print("nan");
|
||||||
|
if (isinf(number)) return print("inf");
|
||||||
|
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
|
||||||
|
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
|
||||||
|
|
||||||
|
// Handle negative numbers
|
||||||
|
if (number < 0.0)
|
||||||
|
{
|
||||||
|
n += print('-');
|
||||||
|
number = -number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||||
|
double rounding = 0.5;
|
||||||
|
for (uint8_t i=0; i<digits; ++i)
|
||||||
|
rounding /= 10.0;
|
||||||
|
|
||||||
|
number += rounding;
|
||||||
|
|
||||||
|
// Extract the integer part of the number and print it
|
||||||
|
unsigned long int_part = (unsigned long)number;
|
||||||
|
double remainder = number - (double)int_part;
|
||||||
|
n += print(int_part);
|
||||||
|
|
||||||
|
// Print the decimal point, but only if there are digits beyond
|
||||||
|
if (digits > 0) {
|
||||||
|
n += print(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract digits from the remainder one at a time
|
||||||
|
while (digits-- > 0)
|
||||||
|
{
|
||||||
|
remainder *= 10.0;
|
||||||
|
int toPrint = int(remainder);
|
||||||
|
n += print(toPrint);
|
||||||
|
remainder -= toPrint;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
Print.h - Base class that provides print() and println()
|
||||||
|
Copyright (c) 2008 David A. Mellis. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef Print_h
|
||||||
|
#define Print_h
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <stdio.h> // for size_t
|
||||||
|
|
||||||
|
#include "WString.h"
|
||||||
|
#include "Printable.h"
|
||||||
|
|
||||||
|
#define DEC 10
|
||||||
|
#define HEX 16
|
||||||
|
#define OCT 8
|
||||||
|
#define BIN 2
|
||||||
|
|
||||||
|
class Print
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int write_error;
|
||||||
|
size_t printNumber(unsigned long, uint8_t);
|
||||||
|
size_t printFloat(double, uint8_t);
|
||||||
|
protected:
|
||||||
|
void setWriteError(int err = 1) { write_error = err; }
|
||||||
|
public:
|
||||||
|
Print() : write_error(0) {}
|
||||||
|
|
||||||
|
int getWriteError() { return write_error; }
|
||||||
|
void clearWriteError() { setWriteError(0); }
|
||||||
|
|
||||||
|
virtual size_t write(uint8_t) = 0;
|
||||||
|
size_t write(const char *str) {
|
||||||
|
if (str == NULL) return 0;
|
||||||
|
return write((const uint8_t *)str, strlen(str));
|
||||||
|
}
|
||||||
|
virtual size_t write(const uint8_t *buffer, size_t size);
|
||||||
|
|
||||||
|
size_t print(const __FlashStringHelper *);
|
||||||
|
size_t print(const String &);
|
||||||
|
size_t print(const char[]);
|
||||||
|
size_t print(char);
|
||||||
|
size_t print(unsigned char, int = DEC);
|
||||||
|
size_t print(int, int = DEC);
|
||||||
|
size_t print(unsigned int, int = DEC);
|
||||||
|
size_t print(long, int = DEC);
|
||||||
|
size_t print(unsigned long, int = DEC);
|
||||||
|
size_t print(double, int = 2);
|
||||||
|
size_t print(const Printable&);
|
||||||
|
|
||||||
|
size_t println(const __FlashStringHelper *);
|
||||||
|
size_t println(const String &s);
|
||||||
|
size_t println(const char[]);
|
||||||
|
size_t println(char);
|
||||||
|
size_t println(unsigned char, int = DEC);
|
||||||
|
size_t println(int, int = DEC);
|
||||||
|
size_t println(unsigned int, int = DEC);
|
||||||
|
size_t println(long, int = DEC);
|
||||||
|
size_t println(unsigned long, int = DEC);
|
||||||
|
size_t println(double, int = 2);
|
||||||
|
size_t println(const Printable&);
|
||||||
|
size_t println(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
Printable.h - Interface class that allows printing of complex types
|
||||||
|
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef Printable_h
|
||||||
|
#define Printable_h
|
||||||
|
|
||||||
|
#include <new.h>
|
||||||
|
|
||||||
|
class Print;
|
||||||
|
|
||||||
|
/** The Printable class provides a way for new classes to allow themselves to be printed.
|
||||||
|
By deriving from Printable and implementing the printTo method, it will then be possible
|
||||||
|
for users to print out instances of this class by passing them into the usual
|
||||||
|
Print::print and Print::println methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Printable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual size_t printTo(Print& p) const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef server_h
|
||||||
|
#define server_h
|
||||||
|
|
||||||
|
class Server : public Print {
|
||||||
|
public:
|
||||||
|
virtual void begin() =0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,270 @@
|
|||||||
|
/*
|
||||||
|
Stream.cpp - adds parsing methods to Stream class
|
||||||
|
Copyright (c) 2008 David A. Mellis. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
|
Created July 2011
|
||||||
|
parsing functions based on TextFinder library by Michael Margolis
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
#include "Stream.h"
|
||||||
|
|
||||||
|
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
|
||||||
|
#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
|
||||||
|
|
||||||
|
// private method to read stream with timeout
|
||||||
|
int Stream::timedRead()
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
_startMillis = millis();
|
||||||
|
do {
|
||||||
|
c = read();
|
||||||
|
if (c >= 0) return c;
|
||||||
|
} while(millis() - _startMillis < _timeout);
|
||||||
|
return -1; // -1 indicates timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
// private method to peek stream with timeout
|
||||||
|
int Stream::timedPeek()
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
_startMillis = millis();
|
||||||
|
do {
|
||||||
|
c = peek();
|
||||||
|
if (c >= 0) return c;
|
||||||
|
} while(millis() - _startMillis < _timeout);
|
||||||
|
return -1; // -1 indicates timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns peek of the next digit in the stream or -1 if timeout
|
||||||
|
// discards non-numeric characters
|
||||||
|
int Stream::peekNextDigit()
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
while (1) {
|
||||||
|
c = timedPeek();
|
||||||
|
if (c < 0) return c; // timeout
|
||||||
|
if (c == '-') return c;
|
||||||
|
if (c >= '0' && c <= '9') return c;
|
||||||
|
read(); // discard non-numeric
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Public Methods
|
||||||
|
//////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
|
||||||
|
{
|
||||||
|
_timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find returns true if the target string is found
|
||||||
|
bool Stream::find(char *target)
|
||||||
|
{
|
||||||
|
return findUntil(target, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// reads data from the stream until the target string of given length is found
|
||||||
|
// returns true if target string is found, false if timed out
|
||||||
|
bool Stream::find(char *target, size_t length)
|
||||||
|
{
|
||||||
|
return findUntil(target, length, NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// as find but search ends if the terminator string is found
|
||||||
|
bool Stream::findUntil(char *target, char *terminator)
|
||||||
|
{
|
||||||
|
return findUntil(target, strlen(target), terminator, strlen(terminator));
|
||||||
|
}
|
||||||
|
|
||||||
|
// reads data from the stream until the target string of the given length is found
|
||||||
|
// search terminated if the terminator string is found
|
||||||
|
// returns true if target string is found, false if terminated or timed out
|
||||||
|
bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
|
||||||
|
{
|
||||||
|
size_t index = 0; // maximum target string length is 64k bytes!
|
||||||
|
size_t termIndex = 0;
|
||||||
|
int c;
|
||||||
|
|
||||||
|
if( *target == 0)
|
||||||
|
return true; // return true if target is a null string
|
||||||
|
while( (c = timedRead()) > 0){
|
||||||
|
|
||||||
|
if(c != target[index])
|
||||||
|
index = 0; // reset index if any char does not match
|
||||||
|
|
||||||
|
if( c == target[index]){
|
||||||
|
//////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
|
||||||
|
if(++index >= targetLen){ // return true if all chars in the target match
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(termLen > 0 && c == terminator[termIndex]){
|
||||||
|
if(++termIndex >= termLen)
|
||||||
|
return false; // return false if terminate string found before target string
|
||||||
|
}
|
||||||
|
else
|
||||||
|
termIndex = 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// returns the first valid (long) integer value from the current position.
|
||||||
|
// initial characters that are not digits (or the minus sign) are skipped
|
||||||
|
// function is terminated by the first character that is not a digit.
|
||||||
|
long Stream::parseInt()
|
||||||
|
{
|
||||||
|
return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// as above but a given skipChar is ignored
|
||||||
|
// this allows format characters (typically commas) in values to be ignored
|
||||||
|
long Stream::parseInt(char skipChar)
|
||||||
|
{
|
||||||
|
boolean isNegative = false;
|
||||||
|
long value = 0;
|
||||||
|
int c;
|
||||||
|
|
||||||
|
c = peekNextDigit();
|
||||||
|
// ignore non numeric leading characters
|
||||||
|
if(c < 0)
|
||||||
|
return 0; // zero returned if timeout
|
||||||
|
|
||||||
|
do{
|
||||||
|
if(c == skipChar)
|
||||||
|
; // ignore this charactor
|
||||||
|
else if(c == '-')
|
||||||
|
isNegative = true;
|
||||||
|
else if(c >= '0' && c <= '9') // is c a digit?
|
||||||
|
value = value * 10 + c - '0';
|
||||||
|
read(); // consume the character we got with peek
|
||||||
|
c = timedPeek();
|
||||||
|
}
|
||||||
|
while( (c >= '0' && c <= '9') || c == skipChar );
|
||||||
|
|
||||||
|
if(isNegative)
|
||||||
|
value = -value;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// as parseInt but returns a floating point value
|
||||||
|
float Stream::parseFloat()
|
||||||
|
{
|
||||||
|
return parseFloat(NO_SKIP_CHAR);
|
||||||
|
}
|
||||||
|
|
||||||
|
// as above but the given skipChar is ignored
|
||||||
|
// this allows format characters (typically commas) in values to be ignored
|
||||||
|
float Stream::parseFloat(char skipChar){
|
||||||
|
boolean isNegative = false;
|
||||||
|
boolean isFraction = false;
|
||||||
|
long value = 0;
|
||||||
|
char c;
|
||||||
|
float fraction = 1.0;
|
||||||
|
|
||||||
|
c = peekNextDigit();
|
||||||
|
// ignore non numeric leading characters
|
||||||
|
if(c < 0)
|
||||||
|
return 0; // zero returned if timeout
|
||||||
|
|
||||||
|
do{
|
||||||
|
if(c == skipChar)
|
||||||
|
; // ignore
|
||||||
|
else if(c == '-')
|
||||||
|
isNegative = true;
|
||||||
|
else if (c == '.')
|
||||||
|
isFraction = true;
|
||||||
|
else if(c >= '0' && c <= '9') { // is c a digit?
|
||||||
|
value = value * 10 + c - '0';
|
||||||
|
if(isFraction)
|
||||||
|
fraction *= 0.1;
|
||||||
|
}
|
||||||
|
read(); // consume the character we got with peek
|
||||||
|
c = timedPeek();
|
||||||
|
}
|
||||||
|
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
|
||||||
|
|
||||||
|
if(isNegative)
|
||||||
|
value = -value;
|
||||||
|
if(isFraction)
|
||||||
|
return value * fraction;
|
||||||
|
else
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// read characters from stream into buffer
|
||||||
|
// terminates if length characters have been read, or timeout (see setTimeout)
|
||||||
|
// returns the number of characters placed in the buffer
|
||||||
|
// the buffer is NOT null terminated.
|
||||||
|
//
|
||||||
|
size_t Stream::readBytes(char *buffer, size_t length)
|
||||||
|
{
|
||||||
|
size_t count = 0;
|
||||||
|
while (count < length) {
|
||||||
|
int c = timedRead();
|
||||||
|
if (c < 0) break;
|
||||||
|
*buffer++ = (char)c;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// as readBytes with terminator character
|
||||||
|
// terminates if length characters have been read, timeout, or if the terminator character detected
|
||||||
|
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||||
|
|
||||||
|
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
|
||||||
|
{
|
||||||
|
if (length < 1) return 0;
|
||||||
|
size_t index = 0;
|
||||||
|
while (index < length) {
|
||||||
|
int c = timedRead();
|
||||||
|
if (c < 0 || c == terminator) break;
|
||||||
|
*buffer++ = (char)c;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return index; // return number of characters, not including null terminator
|
||||||
|
}
|
||||||
|
|
||||||
|
String Stream::readString()
|
||||||
|
{
|
||||||
|
String ret;
|
||||||
|
int c = timedRead();
|
||||||
|
while (c >= 0)
|
||||||
|
{
|
||||||
|
ret += (char)c;
|
||||||
|
c = timedRead();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
String Stream::readStringUntil(char terminator)
|
||||||
|
{
|
||||||
|
String ret;
|
||||||
|
int c = timedRead();
|
||||||
|
while (c >= 0 && c != terminator)
|
||||||
|
{
|
||||||
|
ret += (char)c;
|
||||||
|
c = timedRead();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
Stream.h - base class for character-based streams.
|
||||||
|
Copyright (c) 2010 David A. Mellis. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
|
parsing functions based on TextFinder library by Michael Margolis
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef Stream_h
|
||||||
|
#define Stream_h
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include "Print.h"
|
||||||
|
|
||||||
|
// compatability macros for testing
|
||||||
|
/*
|
||||||
|
#define getInt() parseInt()
|
||||||
|
#define getInt(skipChar) parseInt(skipchar)
|
||||||
|
#define getFloat() parseFloat()
|
||||||
|
#define getFloat(skipChar) parseFloat(skipChar)
|
||||||
|
#define getString( pre_string, post_string, buffer, length)
|
||||||
|
readBytesBetween( pre_string, terminator, buffer, length)
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Stream : public Print
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
|
||||||
|
unsigned long _startMillis; // used for timeout measurement
|
||||||
|
int timedRead(); // private method to read stream with timeout
|
||||||
|
int timedPeek(); // private method to peek stream with timeout
|
||||||
|
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual int available() = 0;
|
||||||
|
virtual int read() = 0;
|
||||||
|
virtual int peek() = 0;
|
||||||
|
virtual void flush() = 0;
|
||||||
|
|
||||||
|
Stream() {_timeout=1000;}
|
||||||
|
|
||||||
|
// parsing methods
|
||||||
|
|
||||||
|
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
|
||||||
|
|
||||||
|
bool find(char *target); // reads data from the stream until the target string is found
|
||||||
|
// returns true if target string is found, false if timed out (see setTimeout)
|
||||||
|
|
||||||
|
bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
|
||||||
|
// returns true if target string is found, false if timed out
|
||||||
|
|
||||||
|
bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
|
||||||
|
|
||||||
|
bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
|
||||||
|
|
||||||
|
|
||||||
|
long parseInt(); // returns the first valid (long) integer value from the current position.
|
||||||
|
// initial characters that are not digits (or the minus sign) are skipped
|
||||||
|
// integer is terminated by the first character that is not a digit.
|
||||||
|
|
||||||
|
float parseFloat(); // float version of parseInt
|
||||||
|
|
||||||
|
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
|
||||||
|
// terminates if length characters have been read or timeout (see setTimeout)
|
||||||
|
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||||
|
|
||||||
|
size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
|
||||||
|
// terminates if length characters have been read, timeout, or if the terminator character detected
|
||||||
|
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||||
|
|
||||||
|
// Arduino String functions to be added here
|
||||||
|
String readString();
|
||||||
|
String readStringUntil(char terminator);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
long parseInt(char skipChar); // as above but the given skipChar is ignored
|
||||||
|
// as above but the given skipChar is ignored
|
||||||
|
// this allows format characters (typically commas) in values to be ignored
|
||||||
|
|
||||||
|
float parseFloat(char skipChar); // as above but the given skipChar is ignored
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,616 @@
|
|||||||
|
/* Tone.cpp
|
||||||
|
|
||||||
|
A Tone Generator Library
|
||||||
|
|
||||||
|
Written by Brett Hagman
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
|
Version Modified By Date Comments
|
||||||
|
------- ----------- -------- --------
|
||||||
|
0001 B Hagman 09/08/02 Initial coding
|
||||||
|
0002 B Hagman 09/08/18 Multiple pins
|
||||||
|
0003 B Hagman 09/08/18 Moved initialization from constructor to begin()
|
||||||
|
0004 B Hagman 09/09/26 Fixed problems with ATmega8
|
||||||
|
0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers
|
||||||
|
09/11/25 Changed pin toggle method to XOR
|
||||||
|
09/11/25 Fixed timer0 from being excluded
|
||||||
|
0006 D Mellis 09/12/29 Replaced objects with functions
|
||||||
|
0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
|
||||||
|
0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
|
||||||
|
*************************************************/
|
||||||
|
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#include "Arduino.h"
|
||||||
|
#include "pins_arduino.h"
|
||||||
|
|
||||||
|
#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
|
||||||
|
#define TCCR2A TCCR2
|
||||||
|
#define TCCR2B TCCR2
|
||||||
|
#define COM2A1 COM21
|
||||||
|
#define COM2A0 COM20
|
||||||
|
#define OCR2A OCR2
|
||||||
|
#define TIMSK2 TIMSK
|
||||||
|
#define OCIE2A OCIE2
|
||||||
|
#define TIMER2_COMPA_vect TIMER2_COMP_vect
|
||||||
|
#define TIMSK1 TIMSK
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// timerx_toggle_count:
|
||||||
|
// > 0 - duration specified
|
||||||
|
// = 0 - stopped
|
||||||
|
// < 0 - infinitely (until stop() method called, or new play() called)
|
||||||
|
|
||||||
|
#if !defined(__AVR_ATmega8__)
|
||||||
|
volatile long timer0_toggle_count;
|
||||||
|
volatile uint8_t *timer0_pin_port;
|
||||||
|
volatile uint8_t timer0_pin_mask;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
volatile long timer1_toggle_count;
|
||||||
|
volatile uint8_t *timer1_pin_port;
|
||||||
|
volatile uint8_t timer1_pin_mask;
|
||||||
|
volatile long timer2_toggle_count;
|
||||||
|
volatile uint8_t *timer2_pin_port;
|
||||||
|
volatile uint8_t timer2_pin_mask;
|
||||||
|
|
||||||
|
#if defined(TIMSK3)
|
||||||
|
volatile long timer3_toggle_count;
|
||||||
|
volatile uint8_t *timer3_pin_port;
|
||||||
|
volatile uint8_t timer3_pin_mask;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TIMSK4)
|
||||||
|
volatile long timer4_toggle_count;
|
||||||
|
volatile uint8_t *timer4_pin_port;
|
||||||
|
volatile uint8_t timer4_pin_mask;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TIMSK5)
|
||||||
|
volatile long timer5_toggle_count;
|
||||||
|
volatile uint8_t *timer5_pin_port;
|
||||||
|
volatile uint8_t timer5_pin_mask;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
||||||
|
|
||||||
|
#define AVAILABLE_TONE_PINS 1
|
||||||
|
#define USE_TIMER2
|
||||||
|
|
||||||
|
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
|
||||||
|
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
|
||||||
|
|
||||||
|
#elif defined(__AVR_ATmega8__)
|
||||||
|
|
||||||
|
#define AVAILABLE_TONE_PINS 1
|
||||||
|
#define USE_TIMER2
|
||||||
|
|
||||||
|
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
|
||||||
|
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
|
||||||
|
|
||||||
|
#elif defined(__AVR_ATmega32U4__)
|
||||||
|
|
||||||
|
#define AVAILABLE_TONE_PINS 1
|
||||||
|
#define USE_TIMER3
|
||||||
|
|
||||||
|
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
|
||||||
|
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define AVAILABLE_TONE_PINS 1
|
||||||
|
#define USE_TIMER2
|
||||||
|
|
||||||
|
// Leave timer 0 to last.
|
||||||
|
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
|
||||||
|
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int8_t toneBegin(uint8_t _pin)
|
||||||
|
{
|
||||||
|
int8_t _timer = -1;
|
||||||
|
|
||||||
|
// if we're already using the pin, the timer should be configured.
|
||||||
|
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
|
||||||
|
if (tone_pins[i] == _pin) {
|
||||||
|
return pgm_read_byte(tone_pin_to_timer_PGM + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// search for an unused timer.
|
||||||
|
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
|
||||||
|
if (tone_pins[i] == 255) {
|
||||||
|
tone_pins[i] = _pin;
|
||||||
|
_timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_timer != -1)
|
||||||
|
{
|
||||||
|
// Set timer specific stuff
|
||||||
|
// All timers in CTC mode
|
||||||
|
// 8 bit timers will require changing prescalar values,
|
||||||
|
// whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
|
||||||
|
switch (_timer)
|
||||||
|
{
|
||||||
|
#if defined(TCCR0A) && defined(TCCR0B)
|
||||||
|
case 0:
|
||||||
|
// 8 bit timer
|
||||||
|
TCCR0A = 0;
|
||||||
|
TCCR0B = 0;
|
||||||
|
bitWrite(TCCR0A, WGM01, 1);
|
||||||
|
bitWrite(TCCR0B, CS00, 1);
|
||||||
|
timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
||||||
|
timer0_pin_mask = digitalPinToBitMask(_pin);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
|
||||||
|
case 1:
|
||||||
|
// 16 bit timer
|
||||||
|
TCCR1A = 0;
|
||||||
|
TCCR1B = 0;
|
||||||
|
bitWrite(TCCR1B, WGM12, 1);
|
||||||
|
bitWrite(TCCR1B, CS10, 1);
|
||||||
|
timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
||||||
|
timer1_pin_mask = digitalPinToBitMask(_pin);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR2A) && defined(TCCR2B)
|
||||||
|
case 2:
|
||||||
|
// 8 bit timer
|
||||||
|
TCCR2A = 0;
|
||||||
|
TCCR2B = 0;
|
||||||
|
bitWrite(TCCR2A, WGM21, 1);
|
||||||
|
bitWrite(TCCR2B, CS20, 1);
|
||||||
|
timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
||||||
|
timer2_pin_mask = digitalPinToBitMask(_pin);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3)
|
||||||
|
case 3:
|
||||||
|
// 16 bit timer
|
||||||
|
TCCR3A = 0;
|
||||||
|
TCCR3B = 0;
|
||||||
|
bitWrite(TCCR3B, WGM32, 1);
|
||||||
|
bitWrite(TCCR3B, CS30, 1);
|
||||||
|
timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
||||||
|
timer3_pin_mask = digitalPinToBitMask(_pin);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4)
|
||||||
|
case 4:
|
||||||
|
// 16 bit timer
|
||||||
|
TCCR4A = 0;
|
||||||
|
TCCR4B = 0;
|
||||||
|
#if defined(WGM42)
|
||||||
|
bitWrite(TCCR4B, WGM42, 1);
|
||||||
|
#elif defined(CS43)
|
||||||
|
#warning this may not be correct
|
||||||
|
// atmega32u4
|
||||||
|
bitWrite(TCCR4B, CS43, 1);
|
||||||
|
#endif
|
||||||
|
bitWrite(TCCR4B, CS40, 1);
|
||||||
|
timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
||||||
|
timer4_pin_mask = digitalPinToBitMask(_pin);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5)
|
||||||
|
case 5:
|
||||||
|
// 16 bit timer
|
||||||
|
TCCR5A = 0;
|
||||||
|
TCCR5B = 0;
|
||||||
|
bitWrite(TCCR5B, WGM52, 1);
|
||||||
|
bitWrite(TCCR5B, CS50, 1);
|
||||||
|
timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
||||||
|
timer5_pin_mask = digitalPinToBitMask(_pin);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return _timer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// frequency (in hertz) and duration (in milliseconds).
|
||||||
|
|
||||||
|
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
|
||||||
|
{
|
||||||
|
uint8_t prescalarbits = 0b001;
|
||||||
|
long toggle_count = 0;
|
||||||
|
uint32_t ocr = 0;
|
||||||
|
int8_t _timer;
|
||||||
|
|
||||||
|
_timer = toneBegin(_pin);
|
||||||
|
|
||||||
|
if (_timer >= 0)
|
||||||
|
{
|
||||||
|
// Set the pinMode as OUTPUT
|
||||||
|
pinMode(_pin, OUTPUT);
|
||||||
|
|
||||||
|
// if we are using an 8 bit timer, scan through prescalars to find the best fit
|
||||||
|
if (_timer == 0 || _timer == 2)
|
||||||
|
{
|
||||||
|
ocr = F_CPU / frequency / 2 - 1;
|
||||||
|
prescalarbits = 0b001; // ck/1: same for both timers
|
||||||
|
if (ocr > 255)
|
||||||
|
{
|
||||||
|
ocr = F_CPU / frequency / 2 / 8 - 1;
|
||||||
|
prescalarbits = 0b010; // ck/8: same for both timers
|
||||||
|
|
||||||
|
if (_timer == 2 && ocr > 255)
|
||||||
|
{
|
||||||
|
ocr = F_CPU / frequency / 2 / 32 - 1;
|
||||||
|
prescalarbits = 0b011;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ocr > 255)
|
||||||
|
{
|
||||||
|
ocr = F_CPU / frequency / 2 / 64 - 1;
|
||||||
|
prescalarbits = _timer == 0 ? 0b011 : 0b100;
|
||||||
|
|
||||||
|
if (_timer == 2 && ocr > 255)
|
||||||
|
{
|
||||||
|
ocr = F_CPU / frequency / 2 / 128 - 1;
|
||||||
|
prescalarbits = 0b101;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ocr > 255)
|
||||||
|
{
|
||||||
|
ocr = F_CPU / frequency / 2 / 256 - 1;
|
||||||
|
prescalarbits = _timer == 0 ? 0b100 : 0b110;
|
||||||
|
if (ocr > 255)
|
||||||
|
{
|
||||||
|
// can't do any better than /1024
|
||||||
|
ocr = F_CPU / frequency / 2 / 1024 - 1;
|
||||||
|
prescalarbits = _timer == 0 ? 0b101 : 0b111;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(TCCR0B)
|
||||||
|
if (_timer == 0)
|
||||||
|
{
|
||||||
|
TCCR0B = prescalarbits;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR2B)
|
||||||
|
{
|
||||||
|
TCCR2B = prescalarbits;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
{
|
||||||
|
// dummy place holder to make the above ifdefs work
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// two choices for the 16 bit timers: ck/1 or ck/64
|
||||||
|
ocr = F_CPU / frequency / 2 - 1;
|
||||||
|
|
||||||
|
prescalarbits = 0b001;
|
||||||
|
if (ocr > 0xffff)
|
||||||
|
{
|
||||||
|
ocr = F_CPU / frequency / 2 / 64 - 1;
|
||||||
|
prescalarbits = 0b011;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_timer == 1)
|
||||||
|
{
|
||||||
|
#if defined(TCCR1B)
|
||||||
|
TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#if defined(TCCR3B)
|
||||||
|
else if (_timer == 3)
|
||||||
|
TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR4B)
|
||||||
|
else if (_timer == 4)
|
||||||
|
TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR5B)
|
||||||
|
else if (_timer == 5)
|
||||||
|
TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Calculate the toggle count
|
||||||
|
if (duration > 0)
|
||||||
|
{
|
||||||
|
toggle_count = 2 * frequency * duration / 1000;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
toggle_count = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the OCR for the given timer,
|
||||||
|
// set the toggle count,
|
||||||
|
// then turn on the interrupts
|
||||||
|
switch (_timer)
|
||||||
|
{
|
||||||
|
|
||||||
|
#if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
|
||||||
|
case 0:
|
||||||
|
OCR0A = ocr;
|
||||||
|
timer0_toggle_count = toggle_count;
|
||||||
|
bitWrite(TIMSK0, OCIE0A, 1);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
#if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
|
||||||
|
OCR1A = ocr;
|
||||||
|
timer1_toggle_count = toggle_count;
|
||||||
|
bitWrite(TIMSK1, OCIE1A, 1);
|
||||||
|
#elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
|
||||||
|
// this combination is for at least the ATmega32
|
||||||
|
OCR1A = ocr;
|
||||||
|
timer1_toggle_count = toggle_count;
|
||||||
|
bitWrite(TIMSK, OCIE1A, 1);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
#if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
|
||||||
|
case 2:
|
||||||
|
OCR2A = ocr;
|
||||||
|
timer2_toggle_count = toggle_count;
|
||||||
|
bitWrite(TIMSK2, OCIE2A, 1);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TIMSK3)
|
||||||
|
case 3:
|
||||||
|
OCR3A = ocr;
|
||||||
|
timer3_toggle_count = toggle_count;
|
||||||
|
bitWrite(TIMSK3, OCIE3A, 1);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TIMSK4)
|
||||||
|
case 4:
|
||||||
|
OCR4A = ocr;
|
||||||
|
timer4_toggle_count = toggle_count;
|
||||||
|
bitWrite(TIMSK4, OCIE4A, 1);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
|
||||||
|
case 5:
|
||||||
|
OCR5A = ocr;
|
||||||
|
timer5_toggle_count = toggle_count;
|
||||||
|
bitWrite(TIMSK5, OCIE5A, 1);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// XXX: this function only works properly for timer 2 (the only one we use
|
||||||
|
// currently). for the others, it should end the tone, but won't restore
|
||||||
|
// proper PWM functionality for the timer.
|
||||||
|
void disableTimer(uint8_t _timer)
|
||||||
|
{
|
||||||
|
switch (_timer)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
#if defined(TIMSK0)
|
||||||
|
TIMSK0 = 0;
|
||||||
|
#elif defined(TIMSK)
|
||||||
|
TIMSK = 0; // atmega32
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
#if defined(TIMSK1) && defined(OCIE1A)
|
||||||
|
case 1:
|
||||||
|
bitWrite(TIMSK1, OCIE1A, 0);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
#if defined(TIMSK2) && defined(OCIE2A)
|
||||||
|
bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR2A) && defined(WGM20)
|
||||||
|
TCCR2A = (1 << WGM20);
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR2B) && defined(CS22)
|
||||||
|
TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
|
||||||
|
#endif
|
||||||
|
#if defined(OCR2A)
|
||||||
|
OCR2A = 0;
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
#if defined(TIMSK3)
|
||||||
|
case 3:
|
||||||
|
TIMSK3 = 0;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TIMSK4)
|
||||||
|
case 4:
|
||||||
|
TIMSK4 = 0;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TIMSK5)
|
||||||
|
case 5:
|
||||||
|
TIMSK5 = 0;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void noTone(uint8_t _pin)
|
||||||
|
{
|
||||||
|
int8_t _timer = -1;
|
||||||
|
|
||||||
|
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
|
||||||
|
if (tone_pins[i] == _pin) {
|
||||||
|
_timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
|
||||||
|
tone_pins[i] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
disableTimer(_timer);
|
||||||
|
|
||||||
|
digitalWrite(_pin, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef USE_TIMER0
|
||||||
|
ISR(TIMER0_COMPA_vect)
|
||||||
|
{
|
||||||
|
if (timer0_toggle_count != 0)
|
||||||
|
{
|
||||||
|
// toggle the pin
|
||||||
|
*timer0_pin_port ^= timer0_pin_mask;
|
||||||
|
|
||||||
|
if (timer0_toggle_count > 0)
|
||||||
|
timer0_toggle_count--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
disableTimer(0);
|
||||||
|
*timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef USE_TIMER1
|
||||||
|
ISR(TIMER1_COMPA_vect)
|
||||||
|
{
|
||||||
|
if (timer1_toggle_count != 0)
|
||||||
|
{
|
||||||
|
// toggle the pin
|
||||||
|
*timer1_pin_port ^= timer1_pin_mask;
|
||||||
|
|
||||||
|
if (timer1_toggle_count > 0)
|
||||||
|
timer1_toggle_count--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
disableTimer(1);
|
||||||
|
*timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef USE_TIMER2
|
||||||
|
ISR(TIMER2_COMPA_vect)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (timer2_toggle_count != 0)
|
||||||
|
{
|
||||||
|
// toggle the pin
|
||||||
|
*timer2_pin_port ^= timer2_pin_mask;
|
||||||
|
|
||||||
|
if (timer2_toggle_count > 0)
|
||||||
|
timer2_toggle_count--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// need to call noTone() so that the tone_pins[] entry is reset, so the
|
||||||
|
// timer gets initialized next time we call tone().
|
||||||
|
// XXX: this assumes timer 2 is always the first one used.
|
||||||
|
noTone(tone_pins[0]);
|
||||||
|
// disableTimer(2);
|
||||||
|
// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef USE_TIMER3
|
||||||
|
ISR(TIMER3_COMPA_vect)
|
||||||
|
{
|
||||||
|
if (timer3_toggle_count != 0)
|
||||||
|
{
|
||||||
|
// toggle the pin
|
||||||
|
*timer3_pin_port ^= timer3_pin_mask;
|
||||||
|
|
||||||
|
if (timer3_toggle_count > 0)
|
||||||
|
timer3_toggle_count--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
disableTimer(3);
|
||||||
|
*timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef USE_TIMER4
|
||||||
|
ISR(TIMER4_COMPA_vect)
|
||||||
|
{
|
||||||
|
if (timer4_toggle_count != 0)
|
||||||
|
{
|
||||||
|
// toggle the pin
|
||||||
|
*timer4_pin_port ^= timer4_pin_mask;
|
||||||
|
|
||||||
|
if (timer4_toggle_count > 0)
|
||||||
|
timer4_toggle_count--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
disableTimer(4);
|
||||||
|
*timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef USE_TIMER5
|
||||||
|
ISR(TIMER5_COMPA_vect)
|
||||||
|
{
|
||||||
|
if (timer5_toggle_count != 0)
|
||||||
|
{
|
||||||
|
// toggle the pin
|
||||||
|
*timer5_pin_port ^= timer5_pin_mask;
|
||||||
|
|
||||||
|
if (timer5_toggle_count > 0)
|
||||||
|
timer5_toggle_count--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
disableTimer(5);
|
||||||
|
*timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
@ -0,0 +1,196 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#ifndef __USBAPI__
|
||||||
|
#define __USBAPI__
|
||||||
|
|
||||||
|
#if defined(USBCON)
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// USB
|
||||||
|
|
||||||
|
class USBDevice_
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
USBDevice_();
|
||||||
|
bool configured();
|
||||||
|
|
||||||
|
void attach();
|
||||||
|
void detach(); // Serial port goes down too...
|
||||||
|
void poll();
|
||||||
|
};
|
||||||
|
extern USBDevice_ USBDevice;
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// Serial over CDC (Serial1 is the physical port)
|
||||||
|
|
||||||
|
class Serial_ : public Stream
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
ring_buffer *_cdc_rx_buffer;
|
||||||
|
public:
|
||||||
|
void begin(uint16_t baud_count);
|
||||||
|
void end(void);
|
||||||
|
|
||||||
|
virtual int available(void);
|
||||||
|
virtual void accept(void);
|
||||||
|
virtual int peek(void);
|
||||||
|
virtual int read(void);
|
||||||
|
virtual void flush(void);
|
||||||
|
virtual size_t write(uint8_t);
|
||||||
|
using Print::write; // pull in write(str) and write(buf, size) from Print
|
||||||
|
operator bool();
|
||||||
|
};
|
||||||
|
extern Serial_ Serial;
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// Mouse
|
||||||
|
|
||||||
|
#define MOUSE_LEFT 1
|
||||||
|
#define MOUSE_RIGHT 2
|
||||||
|
#define MOUSE_MIDDLE 4
|
||||||
|
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
|
||||||
|
|
||||||
|
class Mouse_
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
uint8_t _buttons;
|
||||||
|
void buttons(uint8_t b);
|
||||||
|
public:
|
||||||
|
Mouse_(void);
|
||||||
|
void begin(void);
|
||||||
|
void end(void);
|
||||||
|
void click(uint8_t b = MOUSE_LEFT);
|
||||||
|
void move(signed char x, signed char y, signed char wheel = 0);
|
||||||
|
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
|
||||||
|
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
|
||||||
|
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
|
||||||
|
};
|
||||||
|
extern Mouse_ Mouse;
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// Keyboard
|
||||||
|
|
||||||
|
#define KEY_LEFT_CTRL 0x80
|
||||||
|
#define KEY_LEFT_SHIFT 0x81
|
||||||
|
#define KEY_LEFT_ALT 0x82
|
||||||
|
#define KEY_LEFT_GUI 0x83
|
||||||
|
#define KEY_RIGHT_CTRL 0x84
|
||||||
|
#define KEY_RIGHT_SHIFT 0x85
|
||||||
|
#define KEY_RIGHT_ALT 0x86
|
||||||
|
#define KEY_RIGHT_GUI 0x87
|
||||||
|
|
||||||
|
#define KEY_UP_ARROW 0xDA
|
||||||
|
#define KEY_DOWN_ARROW 0xD9
|
||||||
|
#define KEY_LEFT_ARROW 0xD8
|
||||||
|
#define KEY_RIGHT_ARROW 0xD7
|
||||||
|
#define KEY_BACKSPACE 0xB2
|
||||||
|
#define KEY_TAB 0xB3
|
||||||
|
#define KEY_RETURN 0xB0
|
||||||
|
#define KEY_ESC 0xB1
|
||||||
|
#define KEY_INSERT 0xD1
|
||||||
|
#define KEY_DELETE 0xD4
|
||||||
|
#define KEY_PAGE_UP 0xD3
|
||||||
|
#define KEY_PAGE_DOWN 0xD6
|
||||||
|
#define KEY_HOME 0xD2
|
||||||
|
#define KEY_END 0xD5
|
||||||
|
#define KEY_CAPS_LOCK 0xC1
|
||||||
|
#define KEY_F1 0xC2
|
||||||
|
#define KEY_F2 0xC3
|
||||||
|
#define KEY_F3 0xC4
|
||||||
|
#define KEY_F4 0xC5
|
||||||
|
#define KEY_F5 0xC6
|
||||||
|
#define KEY_F6 0xC7
|
||||||
|
#define KEY_F7 0xC8
|
||||||
|
#define KEY_F8 0xC9
|
||||||
|
#define KEY_F9 0xCA
|
||||||
|
#define KEY_F10 0xCB
|
||||||
|
#define KEY_F11 0xCC
|
||||||
|
#define KEY_F12 0xCD
|
||||||
|
|
||||||
|
// Low level key report: up to 6 keys and shift, ctrl etc at once
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
uint8_t modifiers;
|
||||||
|
uint8_t reserved;
|
||||||
|
uint8_t keys[6];
|
||||||
|
} KeyReport;
|
||||||
|
|
||||||
|
class Keyboard_ : public Print
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
KeyReport _keyReport;
|
||||||
|
void sendReport(KeyReport* keys);
|
||||||
|
public:
|
||||||
|
Keyboard_(void);
|
||||||
|
void begin(void);
|
||||||
|
void end(void);
|
||||||
|
virtual size_t write(uint8_t k);
|
||||||
|
virtual size_t press(uint8_t k);
|
||||||
|
virtual size_t release(uint8_t k);
|
||||||
|
virtual void releaseAll(void);
|
||||||
|
};
|
||||||
|
extern Keyboard_ Keyboard;
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// Low level API
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
uint8_t bmRequestType;
|
||||||
|
uint8_t bRequest;
|
||||||
|
uint8_t wValueL;
|
||||||
|
uint8_t wValueH;
|
||||||
|
uint16_t wIndex;
|
||||||
|
uint16_t wLength;
|
||||||
|
} Setup;
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// HID 'Driver'
|
||||||
|
|
||||||
|
int HID_GetInterface(uint8_t* interfaceNum);
|
||||||
|
int HID_GetDescriptor(int i);
|
||||||
|
bool HID_Setup(Setup& setup);
|
||||||
|
void HID_SendReport(uint8_t id, const void* data, int len);
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// MSC 'Driver'
|
||||||
|
|
||||||
|
int MSC_GetInterface(uint8_t* interfaceNum);
|
||||||
|
int MSC_GetDescriptor(int i);
|
||||||
|
bool MSC_Setup(Setup& setup);
|
||||||
|
bool MSC_Data(uint8_t rx,uint8_t tx);
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
// CSC 'Driver'
|
||||||
|
|
||||||
|
int CDC_GetInterface(uint8_t* interfaceNum);
|
||||||
|
int CDC_GetDescriptor(int i);
|
||||||
|
bool CDC_Setup(Setup& setup);
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
//================================================================================
|
||||||
|
|
||||||
|
#define TRANSFER_PGM 0x80
|
||||||
|
#define TRANSFER_RELEASE 0x40
|
||||||
|
#define TRANSFER_ZERO 0x20
|
||||||
|
|
||||||
|
int USB_SendControl(uint8_t flags, const void* d, int len);
|
||||||
|
int USB_RecvControl(void* d, int len);
|
||||||
|
|
||||||
|
uint8_t USB_Available(uint8_t ep);
|
||||||
|
int USB_Send(uint8_t ep, const void* data, int len); // blocking
|
||||||
|
int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
|
||||||
|
int USB_Recv(uint8_t ep); // non-blocking
|
||||||
|
void USB_Flush(uint8_t ep);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* if defined(USBCON) */
|
@ -0,0 +1,684 @@
|
|||||||
|
|
||||||
|
|
||||||
|
/* Copyright (c) 2010, Peter Barrett
|
||||||
|
**
|
||||||
|
** Permission to use, copy, modify, and/or distribute this software for
|
||||||
|
** any purpose with or without fee is hereby granted, provided that the
|
||||||
|
** above copyright notice and this permission notice appear in all copies.
|
||||||
|
**
|
||||||
|
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||||
|
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||||
|
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
||||||
|
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
||||||
|
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
||||||
|
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||||
|
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||||
|
** SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Platform.h"
|
||||||
|
#include "USBAPI.h"
|
||||||
|
#include "USBDesc.h"
|
||||||
|
|
||||||
|
#if defined(USBCON)
|
||||||
|
|
||||||
|
#define EP_TYPE_CONTROL 0x00
|
||||||
|
#define EP_TYPE_BULK_IN 0x81
|
||||||
|
#define EP_TYPE_BULK_OUT 0x80
|
||||||
|
#define EP_TYPE_INTERRUPT_IN 0xC1
|
||||||
|
#define EP_TYPE_INTERRUPT_OUT 0xC0
|
||||||
|
#define EP_TYPE_ISOCHRONOUS_IN 0x41
|
||||||
|
#define EP_TYPE_ISOCHRONOUS_OUT 0x40
|
||||||
|
|
||||||
|
/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
|
||||||
|
#define TX_RX_LED_PULSE_MS 100
|
||||||
|
volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
|
||||||
|
volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
|
||||||
|
|
||||||
|
//==================================================================
|
||||||
|
//==================================================================
|
||||||
|
|
||||||
|
extern const u16 STRING_LANGUAGE[] PROGMEM;
|
||||||
|
extern const u16 STRING_IPRODUCT[] PROGMEM;
|
||||||
|
extern const u16 STRING_IMANUFACTURER[] PROGMEM;
|
||||||
|
extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM;
|
||||||
|
extern const DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
|
||||||
|
|
||||||
|
const u16 STRING_LANGUAGE[2] = {
|
||||||
|
(3<<8) | (2+2),
|
||||||
|
0x0409 // English
|
||||||
|
};
|
||||||
|
|
||||||
|
const u16 STRING_IPRODUCT[17] = {
|
||||||
|
(3<<8) | (2+2*16),
|
||||||
|
#if USB_PID == 0x8036
|
||||||
|
'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o'
|
||||||
|
#elif USB_PID == 0x8037
|
||||||
|
'A','r','d','u','i','n','o',' ','M','i','c','r','o',' ',' ',' '
|
||||||
|
#elif USB_PID == 0x803C
|
||||||
|
'A','r','d','u','i','n','o',' ','E','s','p','l','o','r','a',' '
|
||||||
|
#elif USB_PID == 0x9208
|
||||||
|
'L','i','l','y','P','a','d','U','S','B',' ',' ',' ',' ',' ',' '
|
||||||
|
#else
|
||||||
|
'U','S','B',' ','I','O',' ','B','o','a','r','d',' ',' ',' ',' '
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
const u16 STRING_IMANUFACTURER[12] = {
|
||||||
|
(3<<8) | (2+2*11),
|
||||||
|
#if USB_VID == 0x2341
|
||||||
|
'A','r','d','u','i','n','o',' ','L','L','C'
|
||||||
|
#elif USB_VID == 0x1b4f
|
||||||
|
'S','p','a','r','k','F','u','n',' ',' ',' '
|
||||||
|
#else
|
||||||
|
'U','n','k','n','o','w','n',' ',' ',' ',' '
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef CDC_ENABLED
|
||||||
|
#define DEVICE_CLASS 0x02
|
||||||
|
#else
|
||||||
|
#define DEVICE_CLASS 0x00
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// DEVICE DESCRIPTOR
|
||||||
|
const DeviceDescriptor USB_DeviceDescriptor =
|
||||||
|
D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
|
||||||
|
|
||||||
|
const DeviceDescriptor USB_DeviceDescriptorA =
|
||||||
|
D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
|
||||||
|
|
||||||
|
//==================================================================
|
||||||
|
//==================================================================
|
||||||
|
|
||||||
|
volatile u8 _usbConfiguration = 0;
|
||||||
|
|
||||||
|
static inline void WaitIN(void)
|
||||||
|
{
|
||||||
|
while (!(UEINTX & (1<<TXINI)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ClearIN(void)
|
||||||
|
{
|
||||||
|
UEINTX = ~(1<<TXINI);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void WaitOUT(void)
|
||||||
|
{
|
||||||
|
while (!(UEINTX & (1<<RXOUTI)))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline u8 WaitForINOrOUT()
|
||||||
|
{
|
||||||
|
while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
|
||||||
|
;
|
||||||
|
return (UEINTX & (1<<RXOUTI)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ClearOUT(void)
|
||||||
|
{
|
||||||
|
UEINTX = ~(1<<RXOUTI);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Recv(volatile u8* data, u8 count)
|
||||||
|
{
|
||||||
|
while (count--)
|
||||||
|
*data++ = UEDATX;
|
||||||
|
|
||||||
|
RXLED1; // light the RX LED
|
||||||
|
RxLEDPulse = TX_RX_LED_PULSE_MS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline u8 Recv8()
|
||||||
|
{
|
||||||
|
RXLED1; // light the RX LED
|
||||||
|
RxLEDPulse = TX_RX_LED_PULSE_MS;
|
||||||
|
|
||||||
|
return UEDATX;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void Send8(u8 d)
|
||||||
|
{
|
||||||
|
UEDATX = d;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void SetEP(u8 ep)
|
||||||
|
{
|
||||||
|
UENUM = ep;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline u8 FifoByteCount()
|
||||||
|
{
|
||||||
|
return UEBCLX;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline u8 ReceivedSetupInt()
|
||||||
|
{
|
||||||
|
return UEINTX & (1<<RXSTPI);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ClearSetupInt()
|
||||||
|
{
|
||||||
|
UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void Stall()
|
||||||
|
{
|
||||||
|
UECONX = (1<<STALLRQ) | (1<<EPEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline u8 ReadWriteAllowed()
|
||||||
|
{
|
||||||
|
return UEINTX & (1<<RWAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline u8 Stalled()
|
||||||
|
{
|
||||||
|
return UEINTX & (1<<STALLEDI);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline u8 FifoFree()
|
||||||
|
{
|
||||||
|
return UEINTX & (1<<FIFOCON);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ReleaseRX()
|
||||||
|
{
|
||||||
|
UEINTX = 0x6B; // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ReleaseTX()
|
||||||
|
{
|
||||||
|
UEINTX = 0x3A; // FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline u8 FrameNumber()
|
||||||
|
{
|
||||||
|
return UDFNUML;
|
||||||
|
}
|
||||||
|
|
||||||
|
//==================================================================
|
||||||
|
//==================================================================
|
||||||
|
|
||||||
|
u8 USBGetConfiguration(void)
|
||||||
|
{
|
||||||
|
return _usbConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define USB_RECV_TIMEOUT
|
||||||
|
class LockEP
|
||||||
|
{
|
||||||
|
u8 _sreg;
|
||||||
|
public:
|
||||||
|
LockEP(u8 ep) : _sreg(SREG)
|
||||||
|
{
|
||||||
|
cli();
|
||||||
|
SetEP(ep & 7);
|
||||||
|
}
|
||||||
|
~LockEP()
|
||||||
|
{
|
||||||
|
SREG = _sreg;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Number of bytes, assumes a rx endpoint
|
||||||
|
u8 USB_Available(u8 ep)
|
||||||
|
{
|
||||||
|
LockEP lock(ep);
|
||||||
|
return FifoByteCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non Blocking receive
|
||||||
|
// Return number of bytes read
|
||||||
|
int USB_Recv(u8 ep, void* d, int len)
|
||||||
|
{
|
||||||
|
if (!_usbConfiguration || len < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
LockEP lock(ep);
|
||||||
|
u8 n = FifoByteCount();
|
||||||
|
len = min(n,len);
|
||||||
|
n = len;
|
||||||
|
u8* dst = (u8*)d;
|
||||||
|
while (n--)
|
||||||
|
*dst++ = Recv8();
|
||||||
|
if (len && !FifoByteCount()) // release empty buffer
|
||||||
|
ReleaseRX();
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recv 1 byte if ready
|
||||||
|
int USB_Recv(u8 ep)
|
||||||
|
{
|
||||||
|
u8 c;
|
||||||
|
if (USB_Recv(ep,&c,1) != 1)
|
||||||
|
return -1;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Space in send EP
|
||||||
|
u8 USB_SendSpace(u8 ep)
|
||||||
|
{
|
||||||
|
LockEP lock(ep);
|
||||||
|
if (!ReadWriteAllowed())
|
||||||
|
return 0;
|
||||||
|
return 64 - FifoByteCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blocking Send of data to an endpoint
|
||||||
|
int USB_Send(u8 ep, const void* d, int len)
|
||||||
|
{
|
||||||
|
if (!_usbConfiguration)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int r = len;
|
||||||
|
const u8* data = (const u8*)d;
|
||||||
|
u8 zero = ep & TRANSFER_ZERO;
|
||||||
|
u8 timeout = 250; // 250ms timeout on send? TODO
|
||||||
|
while (len)
|
||||||
|
{
|
||||||
|
u8 n = USB_SendSpace(ep);
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
if (!(--timeout))
|
||||||
|
return -1;
|
||||||
|
delay(1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n > len)
|
||||||
|
n = len;
|
||||||
|
len -= n;
|
||||||
|
{
|
||||||
|
LockEP lock(ep);
|
||||||
|
if (ep & TRANSFER_ZERO)
|
||||||
|
{
|
||||||
|
while (n--)
|
||||||
|
Send8(0);
|
||||||
|
}
|
||||||
|
else if (ep & TRANSFER_PGM)
|
||||||
|
{
|
||||||
|
while (n--)
|
||||||
|
Send8(pgm_read_byte(data++));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (n--)
|
||||||
|
Send8(*data++);
|
||||||
|
}
|
||||||
|
if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer
|
||||||
|
ReleaseTX();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TXLED1; // light the TX LED
|
||||||
|
TxLEDPulse = TX_RX_LED_PULSE_MS;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern const u8 _initEndpoints[] PROGMEM;
|
||||||
|
const u8 _initEndpoints[] =
|
||||||
|
{
|
||||||
|
0,
|
||||||
|
|
||||||
|
#ifdef CDC_ENABLED
|
||||||
|
EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
|
||||||
|
EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
|
||||||
|
EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HID_ENABLED
|
||||||
|
EP_TYPE_INTERRUPT_IN // HID_ENDPOINT_INT
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#define EP_SINGLE_64 0x32 // EP0
|
||||||
|
#define EP_DOUBLE_64 0x36 // Other endpoints
|
||||||
|
|
||||||
|
static
|
||||||
|
void InitEP(u8 index, u8 type, u8 size)
|
||||||
|
{
|
||||||
|
UENUM = index;
|
||||||
|
UECONX = 1;
|
||||||
|
UECFG0X = type;
|
||||||
|
UECFG1X = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void InitEndpoints()
|
||||||
|
{
|
||||||
|
for (u8 i = 1; i < sizeof(_initEndpoints); i++)
|
||||||
|
{
|
||||||
|
UENUM = i;
|
||||||
|
UECONX = 1;
|
||||||
|
UECFG0X = pgm_read_byte(_initEndpoints+i);
|
||||||
|
UECFG1X = EP_DOUBLE_64;
|
||||||
|
}
|
||||||
|
UERST = 0x7E; // And reset them
|
||||||
|
UERST = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle CLASS_INTERFACE requests
|
||||||
|
static
|
||||||
|
bool ClassInterfaceRequest(Setup& setup)
|
||||||
|
{
|
||||||
|
u8 i = setup.wIndex;
|
||||||
|
|
||||||
|
#ifdef CDC_ENABLED
|
||||||
|
if (CDC_ACM_INTERFACE == i)
|
||||||
|
return CDC_Setup(setup);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HID_ENABLED
|
||||||
|
if (HID_INTERFACE == i)
|
||||||
|
return HID_Setup(setup);
|
||||||
|
#endif
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _cmark;
|
||||||
|
int _cend;
|
||||||
|
void InitControl(int end)
|
||||||
|
{
|
||||||
|
SetEP(0);
|
||||||
|
_cmark = 0;
|
||||||
|
_cend = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
bool SendControl(u8 d)
|
||||||
|
{
|
||||||
|
if (_cmark < _cend)
|
||||||
|
{
|
||||||
|
if (!WaitForINOrOUT())
|
||||||
|
return false;
|
||||||
|
Send8(d);
|
||||||
|
if (!((_cmark + 1) & 0x3F))
|
||||||
|
ClearIN(); // Fifo is full, release this packet
|
||||||
|
}
|
||||||
|
_cmark++;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Clipped by _cmark/_cend
|
||||||
|
int USB_SendControl(u8 flags, const void* d, int len)
|
||||||
|
{
|
||||||
|
int sent = len;
|
||||||
|
const u8* data = (const u8*)d;
|
||||||
|
bool pgm = flags & TRANSFER_PGM;
|
||||||
|
while (len--)
|
||||||
|
{
|
||||||
|
u8 c = pgm ? pgm_read_byte(data++) : *data++;
|
||||||
|
if (!SendControl(c))
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return sent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Does not timeout or cross fifo boundaries
|
||||||
|
// Will only work for transfers <= 64 bytes
|
||||||
|
// TODO
|
||||||
|
int USB_RecvControl(void* d, int len)
|
||||||
|
{
|
||||||
|
WaitOUT();
|
||||||
|
Recv((u8*)d,len);
|
||||||
|
ClearOUT();
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SendInterfaces()
|
||||||
|
{
|
||||||
|
int total = 0;
|
||||||
|
u8 interfaces = 0;
|
||||||
|
|
||||||
|
#ifdef CDC_ENABLED
|
||||||
|
total = CDC_GetInterface(&interfaces);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HID_ENABLED
|
||||||
|
total += HID_GetInterface(&interfaces);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return interfaces;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construct a dynamic configuration descriptor
|
||||||
|
// This really needs dynamic endpoint allocation etc
|
||||||
|
// TODO
|
||||||
|
static
|
||||||
|
bool SendConfiguration(int maxlen)
|
||||||
|
{
|
||||||
|
// Count and measure interfaces
|
||||||
|
InitControl(0);
|
||||||
|
int interfaces = SendInterfaces();
|
||||||
|
ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
|
||||||
|
|
||||||
|
// Now send them
|
||||||
|
InitControl(maxlen);
|
||||||
|
USB_SendControl(0,&config,sizeof(ConfigDescriptor));
|
||||||
|
SendInterfaces();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 _cdcComposite = 0;
|
||||||
|
|
||||||
|
static
|
||||||
|
bool SendDescriptor(Setup& setup)
|
||||||
|
{
|
||||||
|
u8 t = setup.wValueH;
|
||||||
|
if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
|
||||||
|
return SendConfiguration(setup.wLength);
|
||||||
|
|
||||||
|
InitControl(setup.wLength);
|
||||||
|
#ifdef HID_ENABLED
|
||||||
|
if (HID_REPORT_DESCRIPTOR_TYPE == t)
|
||||||
|
return HID_GetDescriptor(t);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
u8 desc_length = 0;
|
||||||
|
const u8* desc_addr = 0;
|
||||||
|
if (USB_DEVICE_DESCRIPTOR_TYPE == t)
|
||||||
|
{
|
||||||
|
if (setup.wLength == 8)
|
||||||
|
_cdcComposite = 1;
|
||||||
|
desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor;
|
||||||
|
}
|
||||||
|
else if (USB_STRING_DESCRIPTOR_TYPE == t)
|
||||||
|
{
|
||||||
|
if (setup.wValueL == 0)
|
||||||
|
desc_addr = (const u8*)&STRING_LANGUAGE;
|
||||||
|
else if (setup.wValueL == IPRODUCT)
|
||||||
|
desc_addr = (const u8*)&STRING_IPRODUCT;
|
||||||
|
else if (setup.wValueL == IMANUFACTURER)
|
||||||
|
desc_addr = (const u8*)&STRING_IMANUFACTURER;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (desc_addr == 0)
|
||||||
|
return false;
|
||||||
|
if (desc_length == 0)
|
||||||
|
desc_length = pgm_read_byte(desc_addr);
|
||||||
|
|
||||||
|
USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoint 0 interrupt
|
||||||
|
ISR(USB_COM_vect)
|
||||||
|
{
|
||||||
|
SetEP(0);
|
||||||
|
if (!ReceivedSetupInt())
|
||||||
|
return;
|
||||||
|
|
||||||
|
Setup setup;
|
||||||
|
Recv((u8*)&setup,8);
|
||||||
|
ClearSetupInt();
|
||||||
|
|
||||||
|
u8 requestType = setup.bmRequestType;
|
||||||
|
if (requestType & REQUEST_DEVICETOHOST)
|
||||||
|
WaitIN();
|
||||||
|
else
|
||||||
|
ClearIN();
|
||||||
|
|
||||||
|
bool ok = true;
|
||||||
|
if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
|
||||||
|
{
|
||||||
|
// Standard Requests
|
||||||
|
u8 r = setup.bRequest;
|
||||||
|
if (GET_STATUS == r)
|
||||||
|
{
|
||||||
|
Send8(0); // TODO
|
||||||
|
Send8(0);
|
||||||
|
}
|
||||||
|
else if (CLEAR_FEATURE == r)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else if (SET_FEATURE == r)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else if (SET_ADDRESS == r)
|
||||||
|
{
|
||||||
|
WaitIN();
|
||||||
|
UDADDR = setup.wValueL | (1<<ADDEN);
|
||||||
|
}
|
||||||
|
else if (GET_DESCRIPTOR == r)
|
||||||
|
{
|
||||||
|
ok = SendDescriptor(setup);
|
||||||
|
}
|
||||||
|
else if (SET_DESCRIPTOR == r)
|
||||||
|
{
|
||||||
|
ok = false;
|
||||||
|
}
|
||||||
|
else if (GET_CONFIGURATION == r)
|
||||||
|
{
|
||||||
|
Send8(1);
|
||||||
|
}
|
||||||
|
else if (SET_CONFIGURATION == r)
|
||||||
|
{
|
||||||
|
if (REQUEST_DEVICE == (requestType & REQUEST_RECIPIENT))
|
||||||
|
{
|
||||||
|
InitEndpoints();
|
||||||
|
_usbConfiguration = setup.wValueL;
|
||||||
|
} else
|
||||||
|
ok = false;
|
||||||
|
}
|
||||||
|
else if (GET_INTERFACE == r)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else if (SET_INTERFACE == r)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
InitControl(setup.wLength); // Max length of transfer
|
||||||
|
ok = ClassInterfaceRequest(setup);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ok)
|
||||||
|
ClearIN();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Stall();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void USB_Flush(u8 ep)
|
||||||
|
{
|
||||||
|
SetEP(ep);
|
||||||
|
if (FifoByteCount())
|
||||||
|
ReleaseTX();
|
||||||
|
}
|
||||||
|
|
||||||
|
// General interrupt
|
||||||
|
ISR(USB_GEN_vect)
|
||||||
|
{
|
||||||
|
u8 udint = UDINT;
|
||||||
|
UDINT = 0;
|
||||||
|
|
||||||
|
// End of Reset
|
||||||
|
if (udint & (1<<EORSTI))
|
||||||
|
{
|
||||||
|
InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64); // init ep0
|
||||||
|
_usbConfiguration = 0; // not configured yet
|
||||||
|
UEIENX = 1 << RXSTPE; // Enable interrupts for ep0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
|
||||||
|
if (udint & (1<<SOFI))
|
||||||
|
{
|
||||||
|
#ifdef CDC_ENABLED
|
||||||
|
USB_Flush(CDC_TX); // Send a tx frame if found
|
||||||
|
if (USB_Available(CDC_RX)) // Handle received bytes (if any)
|
||||||
|
Serial.accept();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// check whether the one-shot period has elapsed. if so, turn off the LED
|
||||||
|
if (TxLEDPulse && !(--TxLEDPulse))
|
||||||
|
TXLED0;
|
||||||
|
if (RxLEDPulse && !(--RxLEDPulse))
|
||||||
|
RXLED0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// VBUS or counting frames
|
||||||
|
// Any frame counting?
|
||||||
|
u8 USBConnected()
|
||||||
|
{
|
||||||
|
u8 f = UDFNUML;
|
||||||
|
delay(3);
|
||||||
|
return f != UDFNUML;
|
||||||
|
}
|
||||||
|
|
||||||
|
//=======================================================================
|
||||||
|
//=======================================================================
|
||||||
|
|
||||||
|
USBDevice_ USBDevice;
|
||||||
|
|
||||||
|
USBDevice_::USBDevice_()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void USBDevice_::attach()
|
||||||
|
{
|
||||||
|
_usbConfiguration = 0;
|
||||||
|
UHWCON = 0x01; // power internal reg
|
||||||
|
USBCON = (1<<USBE)|(1<<FRZCLK); // clock frozen, usb enabled
|
||||||
|
#if F_CPU == 16000000UL
|
||||||
|
PLLCSR = 0x12; // Need 16 MHz xtal
|
||||||
|
#elif F_CPU == 8000000UL
|
||||||
|
PLLCSR = 0x02; // Need 8 MHz xtal
|
||||||
|
#endif
|
||||||
|
while (!(PLLCSR & (1<<PLOCK))) // wait for lock pll
|
||||||
|
;
|
||||||
|
|
||||||
|
// Some tests on specific versions of macosx (10.7.3), reported some
|
||||||
|
// strange behaviuors when the board is reset using the serial
|
||||||
|
// port touch at 1200 bps. This delay fixes this behaviour.
|
||||||
|
delay(1);
|
||||||
|
|
||||||
|
USBCON = ((1<<USBE)|(1<<OTGPADE)); // start USB clock
|
||||||
|
UDIEN = (1<<EORSTE)|(1<<SOFE); // Enable interrupts for EOR (End of Reset) and SOF (start of frame)
|
||||||
|
UDCON = 0; // enable attach resistor
|
||||||
|
|
||||||
|
TX_RX_LED_INIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
void USBDevice_::detach()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for interrupts
|
||||||
|
// TODO: VBUS detection
|
||||||
|
bool USBDevice_::configured()
|
||||||
|
{
|
||||||
|
return _usbConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
void USBDevice_::poll()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* if defined(USBCON) */
|
@ -0,0 +1,303 @@
|
|||||||
|
|
||||||
|
// Copyright (c) 2010, Peter Barrett
|
||||||
|
/*
|
||||||
|
** Permission to use, copy, modify, and/or distribute this software for
|
||||||
|
** any purpose with or without fee is hereby granted, provided that the
|
||||||
|
** above copyright notice and this permission notice appear in all copies.
|
||||||
|
**
|
||||||
|
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||||
|
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||||
|
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
||||||
|
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
||||||
|
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
||||||
|
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||||
|
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||||
|
** SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __USBCORE_H__
|
||||||
|
#define __USBCORE_H__
|
||||||
|
|
||||||
|
// Standard requests
|
||||||
|
#define GET_STATUS 0
|
||||||
|
#define CLEAR_FEATURE 1
|
||||||
|
#define SET_FEATURE 3
|
||||||
|
#define SET_ADDRESS 5
|
||||||
|
#define GET_DESCRIPTOR 6
|
||||||
|
#define SET_DESCRIPTOR 7
|
||||||
|
#define GET_CONFIGURATION 8
|
||||||
|
#define SET_CONFIGURATION 9
|
||||||
|
#define GET_INTERFACE 10
|
||||||
|
#define SET_INTERFACE 11
|
||||||
|
|
||||||
|
|
||||||
|
// bmRequestType
|
||||||
|
#define REQUEST_HOSTTODEVICE 0x00
|
||||||
|
#define REQUEST_DEVICETOHOST 0x80
|
||||||
|
#define REQUEST_DIRECTION 0x80
|
||||||
|
|
||||||
|
#define REQUEST_STANDARD 0x00
|
||||||
|
#define REQUEST_CLASS 0x20
|
||||||
|
#define REQUEST_VENDOR 0x40
|
||||||
|
#define REQUEST_TYPE 0x60
|
||||||
|
|
||||||
|
#define REQUEST_DEVICE 0x00
|
||||||
|
#define REQUEST_INTERFACE 0x01
|
||||||
|
#define REQUEST_ENDPOINT 0x02
|
||||||
|
#define REQUEST_OTHER 0x03
|
||||||
|
#define REQUEST_RECIPIENT 0x03
|
||||||
|
|
||||||
|
#define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE)
|
||||||
|
#define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE)
|
||||||
|
|
||||||
|
// Class requests
|
||||||
|
|
||||||
|
#define CDC_SET_LINE_CODING 0x20
|
||||||
|
#define CDC_GET_LINE_CODING 0x21
|
||||||
|
#define CDC_SET_CONTROL_LINE_STATE 0x22
|
||||||
|
|
||||||
|
#define MSC_RESET 0xFF
|
||||||
|
#define MSC_GET_MAX_LUN 0xFE
|
||||||
|
|
||||||
|
#define HID_GET_REPORT 0x01
|
||||||
|
#define HID_GET_IDLE 0x02
|
||||||
|
#define HID_GET_PROTOCOL 0x03
|
||||||
|
#define HID_SET_REPORT 0x09
|
||||||
|
#define HID_SET_IDLE 0x0A
|
||||||
|
#define HID_SET_PROTOCOL 0x0B
|
||||||
|
|
||||||
|
// Descriptors
|
||||||
|
|
||||||
|
#define USB_DEVICE_DESC_SIZE 18
|
||||||
|
#define USB_CONFIGUARTION_DESC_SIZE 9
|
||||||
|
#define USB_INTERFACE_DESC_SIZE 9
|
||||||
|
#define USB_ENDPOINT_DESC_SIZE 7
|
||||||
|
|
||||||
|
#define USB_DEVICE_DESCRIPTOR_TYPE 1
|
||||||
|
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
|
||||||
|
#define USB_STRING_DESCRIPTOR_TYPE 3
|
||||||
|
#define USB_INTERFACE_DESCRIPTOR_TYPE 4
|
||||||
|
#define USB_ENDPOINT_DESCRIPTOR_TYPE 5
|
||||||
|
|
||||||
|
#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
|
||||||
|
#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
|
||||||
|
#define USB_DEVICE_CLASS_STORAGE 0x08
|
||||||
|
#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
|
||||||
|
|
||||||
|
#define USB_CONFIG_POWERED_MASK 0x40
|
||||||
|
#define USB_CONFIG_BUS_POWERED 0x80
|
||||||
|
#define USB_CONFIG_SELF_POWERED 0xC0
|
||||||
|
#define USB_CONFIG_REMOTE_WAKEUP 0x20
|
||||||
|
|
||||||
|
// bMaxPower in Configuration Descriptor
|
||||||
|
#define USB_CONFIG_POWER_MA(mA) ((mA)/2)
|
||||||
|
|
||||||
|
// bEndpointAddress in Endpoint Descriptor
|
||||||
|
#define USB_ENDPOINT_DIRECTION_MASK 0x80
|
||||||
|
#define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
|
||||||
|
#define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
|
||||||
|
|
||||||
|
#define USB_ENDPOINT_TYPE_MASK 0x03
|
||||||
|
#define USB_ENDPOINT_TYPE_CONTROL 0x00
|
||||||
|
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
|
||||||
|
#define USB_ENDPOINT_TYPE_BULK 0x02
|
||||||
|
#define USB_ENDPOINT_TYPE_INTERRUPT 0x03
|
||||||
|
|
||||||
|
#define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
|
||||||
|
|
||||||
|
#define CDC_V1_10 0x0110
|
||||||
|
#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
|
||||||
|
|
||||||
|
#define CDC_CALL_MANAGEMENT 0x01
|
||||||
|
#define CDC_ABSTRACT_CONTROL_MODEL 0x02
|
||||||
|
#define CDC_HEADER 0x00
|
||||||
|
#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
|
||||||
|
#define CDC_UNION 0x06
|
||||||
|
#define CDC_CS_INTERFACE 0x24
|
||||||
|
#define CDC_CS_ENDPOINT 0x25
|
||||||
|
#define CDC_DATA_INTERFACE_CLASS 0x0A
|
||||||
|
|
||||||
|
#define MSC_SUBCLASS_SCSI 0x06
|
||||||
|
#define MSC_PROTOCOL_BULK_ONLY 0x50
|
||||||
|
|
||||||
|
#define HID_HID_DESCRIPTOR_TYPE 0x21
|
||||||
|
#define HID_REPORT_DESCRIPTOR_TYPE 0x22
|
||||||
|
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
|
||||||
|
|
||||||
|
|
||||||
|
// Device
|
||||||
|
typedef struct {
|
||||||
|
u8 len; // 18
|
||||||
|
u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
|
||||||
|
u16 usbVersion; // 0x200
|
||||||
|
u8 deviceClass;
|
||||||
|
u8 deviceSubClass;
|
||||||
|
u8 deviceProtocol;
|
||||||
|
u8 packetSize0; // Packet 0
|
||||||
|
u16 idVendor;
|
||||||
|
u16 idProduct;
|
||||||
|
u16 deviceVersion; // 0x100
|
||||||
|
u8 iManufacturer;
|
||||||
|
u8 iProduct;
|
||||||
|
u8 iSerialNumber;
|
||||||
|
u8 bNumConfigurations;
|
||||||
|
} DeviceDescriptor;
|
||||||
|
|
||||||
|
// Config
|
||||||
|
typedef struct {
|
||||||
|
u8 len; // 9
|
||||||
|
u8 dtype; // 2
|
||||||
|
u16 clen; // total length
|
||||||
|
u8 numInterfaces;
|
||||||
|
u8 config;
|
||||||
|
u8 iconfig;
|
||||||
|
u8 attributes;
|
||||||
|
u8 maxPower;
|
||||||
|
} ConfigDescriptor;
|
||||||
|
|
||||||
|
// String
|
||||||
|
|
||||||
|
// Interface
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 len; // 9
|
||||||
|
u8 dtype; // 4
|
||||||
|
u8 number;
|
||||||
|
u8 alternate;
|
||||||
|
u8 numEndpoints;
|
||||||
|
u8 interfaceClass;
|
||||||
|
u8 interfaceSubClass;
|
||||||
|
u8 protocol;
|
||||||
|
u8 iInterface;
|
||||||
|
} InterfaceDescriptor;
|
||||||
|
|
||||||
|
// Endpoint
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 len; // 7
|
||||||
|
u8 dtype; // 5
|
||||||
|
u8 addr;
|
||||||
|
u8 attr;
|
||||||
|
u16 packetSize;
|
||||||
|
u8 interval;
|
||||||
|
} EndpointDescriptor;
|
||||||
|
|
||||||
|
// Interface Association Descriptor
|
||||||
|
// Used to bind 2 interfaces together in CDC compostite device
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 len; // 8
|
||||||
|
u8 dtype; // 11
|
||||||
|
u8 firstInterface;
|
||||||
|
u8 interfaceCount;
|
||||||
|
u8 functionClass;
|
||||||
|
u8 funtionSubClass;
|
||||||
|
u8 functionProtocol;
|
||||||
|
u8 iInterface;
|
||||||
|
} IADDescriptor;
|
||||||
|
|
||||||
|
// CDC CS interface descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 len; // 5
|
||||||
|
u8 dtype; // 0x24
|
||||||
|
u8 subtype;
|
||||||
|
u8 d0;
|
||||||
|
u8 d1;
|
||||||
|
} CDCCSInterfaceDescriptor;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 len; // 4
|
||||||
|
u8 dtype; // 0x24
|
||||||
|
u8 subtype;
|
||||||
|
u8 d0;
|
||||||
|
} CDCCSInterfaceDescriptor4;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 len;
|
||||||
|
u8 dtype; // 0x24
|
||||||
|
u8 subtype; // 1
|
||||||
|
u8 bmCapabilities;
|
||||||
|
u8 bDataInterface;
|
||||||
|
} CMFunctionalDescriptor;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 len;
|
||||||
|
u8 dtype; // 0x24
|
||||||
|
u8 subtype; // 1
|
||||||
|
u8 bmCapabilities;
|
||||||
|
} ACMFunctionalDescriptor;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
// IAD
|
||||||
|
IADDescriptor iad; // Only needed on compound device
|
||||||
|
|
||||||
|
// Control
|
||||||
|
InterfaceDescriptor cif; //
|
||||||
|
CDCCSInterfaceDescriptor header;
|
||||||
|
CMFunctionalDescriptor callManagement; // Call Management
|
||||||
|
ACMFunctionalDescriptor controlManagement; // ACM
|
||||||
|
CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION
|
||||||
|
EndpointDescriptor cifin;
|
||||||
|
|
||||||
|
// Data
|
||||||
|
InterfaceDescriptor dif;
|
||||||
|
EndpointDescriptor in;
|
||||||
|
EndpointDescriptor out;
|
||||||
|
} CDCDescriptor;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
InterfaceDescriptor msc;
|
||||||
|
EndpointDescriptor in;
|
||||||
|
EndpointDescriptor out;
|
||||||
|
} MSCDescriptor;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 len; // 9
|
||||||
|
u8 dtype; // 0x21
|
||||||
|
u8 addr;
|
||||||
|
u8 versionL; // 0x101
|
||||||
|
u8 versionH; // 0x101
|
||||||
|
u8 country;
|
||||||
|
u8 desctype; // 0x22 report
|
||||||
|
u8 descLenL;
|
||||||
|
u8 descLenH;
|
||||||
|
} HIDDescDescriptor;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
InterfaceDescriptor hid;
|
||||||
|
HIDDescDescriptor desc;
|
||||||
|
EndpointDescriptor in;
|
||||||
|
} HIDDescriptor;
|
||||||
|
|
||||||
|
|
||||||
|
#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
|
||||||
|
{ 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
|
||||||
|
|
||||||
|
#define D_CONFIG(_totalLength,_interfaces) \
|
||||||
|
{ 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) }
|
||||||
|
|
||||||
|
#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
|
||||||
|
{ 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
|
||||||
|
|
||||||
|
#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
|
||||||
|
{ 7, 5, _addr,_attr,_packetSize, _interval }
|
||||||
|
|
||||||
|
#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
|
||||||
|
{ 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
|
||||||
|
|
||||||
|
#define D_HIDREPORT(_descriptorLength) \
|
||||||
|
{ 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
|
||||||
|
|
||||||
|
#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
|
||||||
|
#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,63 @@
|
|||||||
|
|
||||||
|
|
||||||
|
/* Copyright (c) 2011, Peter Barrett
|
||||||
|
**
|
||||||
|
** Permission to use, copy, modify, and/or distribute this software for
|
||||||
|
** any purpose with or without fee is hereby granted, provided that the
|
||||||
|
** above copyright notice and this permission notice appear in all copies.
|
||||||
|
**
|
||||||
|
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||||
|
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||||
|
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
||||||
|
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
||||||
|
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
||||||
|
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||||
|
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||||
|
** SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define CDC_ENABLED
|
||||||
|
#define HID_ENABLED
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef CDC_ENABLED
|
||||||
|
#define CDC_INTERFACE_COUNT 2
|
||||||
|
#define CDC_ENPOINT_COUNT 3
|
||||||
|
#else
|
||||||
|
#define CDC_INTERFACE_COUNT 0
|
||||||
|
#define CDC_ENPOINT_COUNT 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HID_ENABLED
|
||||||
|
#define HID_INTERFACE_COUNT 1
|
||||||
|
#define HID_ENPOINT_COUNT 1
|
||||||
|
#else
|
||||||
|
#define HID_INTERFACE_COUNT 0
|
||||||
|
#define HID_ENPOINT_COUNT 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define CDC_ACM_INTERFACE 0 // CDC ACM
|
||||||
|
#define CDC_DATA_INTERFACE 1 // CDC Data
|
||||||
|
#define CDC_FIRST_ENDPOINT 1
|
||||||
|
#define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First
|
||||||
|
#define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1)
|
||||||
|
#define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2)
|
||||||
|
|
||||||
|
#define HID_INTERFACE (CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT) // HID Interface
|
||||||
|
#define HID_FIRST_ENDPOINT (CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT)
|
||||||
|
#define HID_ENDPOINT_INT (HID_FIRST_ENDPOINT)
|
||||||
|
|
||||||
|
#define INTERFACE_COUNT (MSC_INTERFACE + MSC_INTERFACE_COUNT)
|
||||||
|
|
||||||
|
#ifdef CDC_ENABLED
|
||||||
|
#define CDC_RX CDC_ENDPOINT_OUT
|
||||||
|
#define CDC_TX CDC_ENDPOINT_IN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HID_ENABLED
|
||||||
|
#define HID_TX HID_ENDPOINT_INT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define IMANUFACTURER 1
|
||||||
|
#define IPRODUCT 2
|
||||||
|
|
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* Udp.cpp: Library to send/receive UDP packets.
|
||||||
|
*
|
||||||
|
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
|
||||||
|
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
|
||||||
|
* might not happen often in practice, but in larger network topologies, a UDP
|
||||||
|
* packet can be received out of sequence.
|
||||||
|
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
|
||||||
|
* aware of it. Again, this may not be a concern in practice on small local networks.
|
||||||
|
* For more information, see http://www.cafeaulait.org/course/week12/35.html
|
||||||
|
*
|
||||||
|
* MIT License:
|
||||||
|
* Copyright (c) 2008 Bjoern Hartmann
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*
|
||||||
|
* bjoern@cs.stanford.edu 12/30/2008
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef udp_h
|
||||||
|
#define udp_h
|
||||||
|
|
||||||
|
#include <Stream.h>
|
||||||
|
#include <IPAddress.h>
|
||||||
|
|
||||||
|
class UDP : public Stream {
|
||||||
|
|
||||||
|
public:
|
||||||
|
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
|
||||||
|
virtual void stop() =0; // Finish with the UDP socket
|
||||||
|
|
||||||
|
// Sending UDP packets
|
||||||
|
|
||||||
|
// Start building up a packet to send to the remote host specific in ip and port
|
||||||
|
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
|
||||||
|
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
|
||||||
|
// Start building up a packet to send to the remote host specific in host and port
|
||||||
|
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
|
||||||
|
virtual int beginPacket(const char *host, uint16_t port) =0;
|
||||||
|
// Finish off this packet and send it
|
||||||
|
// Returns 1 if the packet was sent successfully, 0 if there was an error
|
||||||
|
virtual int endPacket() =0;
|
||||||
|
// Write a single byte into the packet
|
||||||
|
virtual size_t write(uint8_t) =0;
|
||||||
|
// Write size bytes from buffer into the packet
|
||||||
|
virtual size_t write(const uint8_t *buffer, size_t size) =0;
|
||||||
|
|
||||||
|
// Start processing the next available incoming packet
|
||||||
|
// Returns the size of the packet in bytes, or 0 if no packets are available
|
||||||
|
virtual int parsePacket() =0;
|
||||||
|
// Number of bytes remaining in the current packet
|
||||||
|
virtual int available() =0;
|
||||||
|
// Read a single byte from the current packet
|
||||||
|
virtual int read() =0;
|
||||||
|
// Read up to len bytes from the current packet and place them into buffer
|
||||||
|
// Returns the number of bytes read, or 0 if none are available
|
||||||
|
virtual int read(unsigned char* buffer, size_t len) =0;
|
||||||
|
// Read up to len characters from the current packet and place them into buffer
|
||||||
|
// Returns the number of characters read, or 0 if none are available
|
||||||
|
virtual int read(char* buffer, size_t len) =0;
|
||||||
|
// Return the next byte from the current packet without moving on to the next byte
|
||||||
|
virtual int peek() =0;
|
||||||
|
virtual void flush() =0; // Finish reading the current packet
|
||||||
|
|
||||||
|
// Return the IP address of the host who sent the current incoming packet
|
||||||
|
virtual IPAddress remoteIP() =0;
|
||||||
|
// Return the port of the host who sent the current incoming packet
|
||||||
|
virtual uint16_t remotePort() =0;
|
||||||
|
protected:
|
||||||
|
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,168 @@
|
|||||||
|
/*
|
||||||
|
WCharacter.h - Character utility functions for Wiring & Arduino
|
||||||
|
Copyright (c) 2010 Hernando Barragan. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef Character_h
|
||||||
|
#define Character_h
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
// WCharacter.h prototypes
|
||||||
|
inline boolean isAlphaNumeric(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isAlpha(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isAscii(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isWhitespace(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isControl(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isDigit(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isGraph(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isLowerCase(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isPrintable(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isPunct(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isSpace(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isUpperCase(int c) __attribute__((always_inline));
|
||||||
|
inline boolean isHexadecimalDigit(int c) __attribute__((always_inline));
|
||||||
|
inline int toAscii(int c) __attribute__((always_inline));
|
||||||
|
inline int toLowerCase(int c) __attribute__((always_inline));
|
||||||
|
inline int toUpperCase(int c)__attribute__((always_inline));
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for an alphanumeric character.
|
||||||
|
// It is equivalent to (isalpha(c) || isdigit(c)).
|
||||||
|
inline boolean isAlphaNumeric(int c)
|
||||||
|
{
|
||||||
|
return ( isalnum(c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for an alphabetic character.
|
||||||
|
// It is equivalent to (isupper(c) || islower(c)).
|
||||||
|
inline boolean isAlpha(int c)
|
||||||
|
{
|
||||||
|
return ( isalpha(c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks whether c is a 7-bit unsigned char value
|
||||||
|
// that fits into the ASCII character set.
|
||||||
|
inline boolean isAscii(int c)
|
||||||
|
{
|
||||||
|
return ( isascii (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for a blank character, that is, a space or a tab.
|
||||||
|
inline boolean isWhitespace(int c)
|
||||||
|
{
|
||||||
|
return ( isblank (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for a control character.
|
||||||
|
inline boolean isControl(int c)
|
||||||
|
{
|
||||||
|
return ( iscntrl (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for a digit (0 through 9).
|
||||||
|
inline boolean isDigit(int c)
|
||||||
|
{
|
||||||
|
return ( isdigit (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for any printable character except space.
|
||||||
|
inline boolean isGraph(int c)
|
||||||
|
{
|
||||||
|
return ( isgraph (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for a lower-case character.
|
||||||
|
inline boolean isLowerCase(int c)
|
||||||
|
{
|
||||||
|
return (islower (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for any printable character including space.
|
||||||
|
inline boolean isPrintable(int c)
|
||||||
|
{
|
||||||
|
return ( isprint (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for any printable character which is not a space
|
||||||
|
// or an alphanumeric character.
|
||||||
|
inline boolean isPunct(int c)
|
||||||
|
{
|
||||||
|
return ( ispunct (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for white-space characters. For the avr-libc library,
|
||||||
|
// these are: space, formfeed ('\f'), newline ('\n'), carriage
|
||||||
|
// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
|
||||||
|
inline boolean isSpace(int c)
|
||||||
|
{
|
||||||
|
return ( isspace (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for an uppercase letter.
|
||||||
|
inline boolean isUpperCase(int c)
|
||||||
|
{
|
||||||
|
return ( isupper (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7
|
||||||
|
// 8 9 a b c d e f A B C D E F.
|
||||||
|
inline boolean isHexadecimalDigit(int c)
|
||||||
|
{
|
||||||
|
return ( isxdigit (c) == 0 ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Converts c to a 7-bit unsigned char value that fits into the
|
||||||
|
// ASCII character set, by clearing the high-order bits.
|
||||||
|
inline int toAscii(int c)
|
||||||
|
{
|
||||||
|
return toascii (c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Warning:
|
||||||
|
// Many people will be unhappy if you use this function.
|
||||||
|
// This function will convert accented letters into random
|
||||||
|
// characters.
|
||||||
|
|
||||||
|
// Converts the letter c to lower case, if possible.
|
||||||
|
inline int toLowerCase(int c)
|
||||||
|
{
|
||||||
|
return tolower (c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Converts the letter c to upper case, if possible.
|
||||||
|
inline int toUpperCase(int c)
|
||||||
|
{
|
||||||
|
return toupper (c);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,322 @@
|
|||||||
|
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Part of the Wiring project - http://wiring.uniandes.edu.co
|
||||||
|
|
||||||
|
Copyright (c) 2004-05 Hernando Barragan
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General
|
||||||
|
Public License along with this library; if not, write to the
|
||||||
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Modified 24 November 2006 by David A. Mellis
|
||||||
|
Modified 1 August 2010 by Mark Sproul
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "wiring_private.h"
|
||||||
|
|
||||||
|
static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
|
||||||
|
// volatile static voidFuncPtr twiIntFunc;
|
||||||
|
|
||||||
|
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
|
||||||
|
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
|
||||||
|
intFunc[interruptNum] = userFunc;
|
||||||
|
|
||||||
|
// Configure the interrupt mode (trigger on low input, any change, rising
|
||||||
|
// edge, or falling edge). The mode constants were chosen to correspond
|
||||||
|
// to the configuration bits in the hardware register, so we simply shift
|
||||||
|
// the mode into place.
|
||||||
|
|
||||||
|
// Enable the interrupt.
|
||||||
|
|
||||||
|
switch (interruptNum) {
|
||||||
|
#if defined(__AVR_ATmega32U4__)
|
||||||
|
// I hate doing this, but the register assignment differs between the 1280/2560
|
||||||
|
// and the 32U4. Since avrlib defines registers PCMSK1 and PCMSK2 that aren't
|
||||||
|
// even present on the 32U4 this is the only way to distinguish between them.
|
||||||
|
case 0:
|
||||||
|
EICRA = (EICRA & ~((1<<ISC00) | (1<<ISC01))) | (mode << ISC00);
|
||||||
|
EIMSK |= (1<<INT0);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
EICRA = (EICRA & ~((1<<ISC10) | (1<<ISC11))) | (mode << ISC10);
|
||||||
|
EIMSK |= (1<<INT1);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
EICRA = (EICRA & ~((1<<ISC20) | (1<<ISC21))) | (mode << ISC20);
|
||||||
|
EIMSK |= (1<<INT2);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
EICRA = (EICRA & ~((1<<ISC30) | (1<<ISC31))) | (mode << ISC30);
|
||||||
|
EIMSK |= (1<<INT3);
|
||||||
|
break;
|
||||||
|
#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
|
||||||
|
case 2:
|
||||||
|
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
||||||
|
EIMSK |= (1 << INT0);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
||||||
|
EIMSK |= (1 << INT1);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
|
||||||
|
EIMSK |= (1 << INT2);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
|
||||||
|
EIMSK |= (1 << INT3);
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
|
||||||
|
EIMSK |= (1 << INT4);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
|
||||||
|
EIMSK |= (1 << INT5);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
|
||||||
|
EIMSK |= (1 << INT6);
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
|
||||||
|
EIMSK |= (1 << INT7);
|
||||||
|
break;
|
||||||
|
#else
|
||||||
|
case 0:
|
||||||
|
#if defined(EICRA) && defined(ISC00) && defined(EIMSK)
|
||||||
|
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
||||||
|
EIMSK |= (1 << INT0);
|
||||||
|
#elif defined(MCUCR) && defined(ISC00) && defined(GICR)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
||||||
|
GICR |= (1 << INT0);
|
||||||
|
#elif defined(MCUCR) && defined(ISC00) && defined(GIMSK)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
||||||
|
GIMSK |= (1 << INT0);
|
||||||
|
#else
|
||||||
|
#error attachInterrupt not finished for this CPU (case 0)
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
#if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK)
|
||||||
|
EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
||||||
|
EIMSK |= (1 << INT1);
|
||||||
|
#elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
||||||
|
GICR |= (1 << INT1);
|
||||||
|
#elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
||||||
|
GIMSK |= (1 << INT1);
|
||||||
|
#else
|
||||||
|
#warning attachInterrupt may need some more work for this cpu (case 1)
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
#if defined(EICRA) && defined(ISC20) && defined(ISC21) && defined(EIMSK)
|
||||||
|
EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
|
||||||
|
EIMSK |= (1 << INT2);
|
||||||
|
#elif defined(MCUCR) && defined(ISC20) && defined(ISC21) && defined(GICR)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
|
||||||
|
GICR |= (1 << INT2);
|
||||||
|
#elif defined(MCUCR) && defined(ISC20) && defined(GIMSK) && defined(GIMSK)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
|
||||||
|
GIMSK |= (1 << INT2);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void detachInterrupt(uint8_t interruptNum) {
|
||||||
|
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
|
||||||
|
// Disable the interrupt. (We can't assume that interruptNum is equal
|
||||||
|
// to the number of the EIMSK bit to clear, as this isn't true on the
|
||||||
|
// ATmega8. There, INT0 is 6 and INT1 is 7.)
|
||||||
|
switch (interruptNum) {
|
||||||
|
#if defined(__AVR_ATmega32U4__)
|
||||||
|
case 0:
|
||||||
|
EIMSK &= ~(1<<INT0);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
EIMSK &= ~(1<<INT1);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
EIMSK &= ~(1<<INT2);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
EIMSK &= ~(1<<INT3);
|
||||||
|
break;
|
||||||
|
#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
|
||||||
|
case 2:
|
||||||
|
EIMSK &= ~(1 << INT0);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
EIMSK &= ~(1 << INT1);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
EIMSK &= ~(1 << INT2);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
EIMSK &= ~(1 << INT3);
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
EIMSK &= ~(1 << INT4);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
EIMSK &= ~(1 << INT5);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
EIMSK &= ~(1 << INT6);
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
EIMSK &= ~(1 << INT7);
|
||||||
|
break;
|
||||||
|
#else
|
||||||
|
case 0:
|
||||||
|
#if defined(EIMSK) && defined(INT0)
|
||||||
|
EIMSK &= ~(1 << INT0);
|
||||||
|
#elif defined(GICR) && defined(ISC00)
|
||||||
|
GICR &= ~(1 << INT0); // atmega32
|
||||||
|
#elif defined(GIMSK) && defined(INT0)
|
||||||
|
GIMSK &= ~(1 << INT0);
|
||||||
|
#else
|
||||||
|
#error detachInterrupt not finished for this cpu
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
#if defined(EIMSK) && defined(INT1)
|
||||||
|
EIMSK &= ~(1 << INT1);
|
||||||
|
#elif defined(GICR) && defined(INT1)
|
||||||
|
GICR &= ~(1 << INT1); // atmega32
|
||||||
|
#elif defined(GIMSK) && defined(INT1)
|
||||||
|
GIMSK &= ~(1 << INT1);
|
||||||
|
#else
|
||||||
|
#warning detachInterrupt may need some more work for this cpu (case 1)
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
intFunc[interruptNum] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
void attachInterruptTwi(void (*userFunc)(void) ) {
|
||||||
|
twiIntFunc = userFunc;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined(__AVR_ATmega32U4__)
|
||||||
|
SIGNAL(INT0_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_0])
|
||||||
|
intFunc[EXTERNAL_INT_0]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT1_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_1])
|
||||||
|
intFunc[EXTERNAL_INT_1]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT2_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_2])
|
||||||
|
intFunc[EXTERNAL_INT_2]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT3_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_3])
|
||||||
|
intFunc[EXTERNAL_INT_3]();
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(EICRA) && defined(EICRB)
|
||||||
|
|
||||||
|
SIGNAL(INT0_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_2])
|
||||||
|
intFunc[EXTERNAL_INT_2]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT1_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_3])
|
||||||
|
intFunc[EXTERNAL_INT_3]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT2_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_4])
|
||||||
|
intFunc[EXTERNAL_INT_4]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT3_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_5])
|
||||||
|
intFunc[EXTERNAL_INT_5]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT4_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_0])
|
||||||
|
intFunc[EXTERNAL_INT_0]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT5_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_1])
|
||||||
|
intFunc[EXTERNAL_INT_1]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT6_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_6])
|
||||||
|
intFunc[EXTERNAL_INT_6]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT7_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_7])
|
||||||
|
intFunc[EXTERNAL_INT_7]();
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
SIGNAL(INT0_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_0])
|
||||||
|
intFunc[EXTERNAL_INT_0]();
|
||||||
|
}
|
||||||
|
|
||||||
|
SIGNAL(INT1_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_1])
|
||||||
|
intFunc[EXTERNAL_INT_1]();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(EICRA) && defined(ISC20)
|
||||||
|
SIGNAL(INT2_vect) {
|
||||||
|
if(intFunc[EXTERNAL_INT_2])
|
||||||
|
intFunc[EXTERNAL_INT_2]();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
SIGNAL(SIG_2WIRE_SERIAL) {
|
||||||
|
if(twiIntFunc)
|
||||||
|
twiIntFunc();
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
@ -0,0 +1,60 @@
|
|||||||
|
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Part of the Wiring project - http://wiring.org.co
|
||||||
|
Copyright (c) 2004-06 Hernando Barragan
|
||||||
|
Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General
|
||||||
|
Public License along with this library; if not, write to the
|
||||||
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "stdlib.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
void randomSeed(unsigned int seed)
|
||||||
|
{
|
||||||
|
if (seed != 0) {
|
||||||
|
srandom(seed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long random(long howbig)
|
||||||
|
{
|
||||||
|
if (howbig == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return random() % howbig;
|
||||||
|
}
|
||||||
|
|
||||||
|
long random(long howsmall, long howbig)
|
||||||
|
{
|
||||||
|
if (howsmall >= howbig) {
|
||||||
|
return howsmall;
|
||||||
|
}
|
||||||
|
long diff = howbig - howsmall;
|
||||||
|
return random(diff) + howsmall;
|
||||||
|
}
|
||||||
|
|
||||||
|
long map(long x, long in_min, long in_max, long out_min, long out_max)
|
||||||
|
{
|
||||||
|
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int makeWord(unsigned int w) { return w; }
|
||||||
|
unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }
|
@ -0,0 +1,645 @@
|
|||||||
|
/*
|
||||||
|
WString.cpp - String library for Wiring & Arduino
|
||||||
|
...mostly rewritten by Paul Stoffregen...
|
||||||
|
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
|
||||||
|
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "WString.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Constructors */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
String::String(const char *cstr)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
if (cstr) copy(cstr, strlen(cstr));
|
||||||
|
}
|
||||||
|
|
||||||
|
String::String(const String &value)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
*this = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
|
String::String(String &&rval)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
move(rval);
|
||||||
|
}
|
||||||
|
String::String(StringSumHelper &&rval)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
move(rval);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
String::String(char c)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
char buf[2];
|
||||||
|
buf[0] = c;
|
||||||
|
buf[1] = 0;
|
||||||
|
*this = buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
String::String(unsigned char value, unsigned char base)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
char buf[9];
|
||||||
|
utoa(value, buf, base);
|
||||||
|
*this = buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
String::String(int value, unsigned char base)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
char buf[18];
|
||||||
|
itoa(value, buf, base);
|
||||||
|
*this = buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
String::String(unsigned int value, unsigned char base)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
char buf[17];
|
||||||
|
utoa(value, buf, base);
|
||||||
|
*this = buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
String::String(long value, unsigned char base)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
char buf[34];
|
||||||
|
ltoa(value, buf, base);
|
||||||
|
*this = buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
String::String(unsigned long value, unsigned char base)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
char buf[33];
|
||||||
|
ultoa(value, buf, base);
|
||||||
|
*this = buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
String::~String()
|
||||||
|
{
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Memory Management */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
inline void String::init(void)
|
||||||
|
{
|
||||||
|
buffer = NULL;
|
||||||
|
capacity = 0;
|
||||||
|
len = 0;
|
||||||
|
flags = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void String::invalidate(void)
|
||||||
|
{
|
||||||
|
if (buffer) free(buffer);
|
||||||
|
buffer = NULL;
|
||||||
|
capacity = len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::reserve(unsigned int size)
|
||||||
|
{
|
||||||
|
if (buffer && capacity >= size) return 1;
|
||||||
|
if (changeBuffer(size)) {
|
||||||
|
if (len == 0) buffer[0] = 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::changeBuffer(unsigned int maxStrLen)
|
||||||
|
{
|
||||||
|
char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
|
||||||
|
if (newbuffer) {
|
||||||
|
buffer = newbuffer;
|
||||||
|
capacity = maxStrLen;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Copy and Move */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
String & String::copy(const char *cstr, unsigned int length)
|
||||||
|
{
|
||||||
|
if (!reserve(length)) {
|
||||||
|
invalidate();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
len = length;
|
||||||
|
strcpy(buffer, cstr);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
|
void String::move(String &rhs)
|
||||||
|
{
|
||||||
|
if (buffer) {
|
||||||
|
if (capacity >= rhs.len) {
|
||||||
|
strcpy(buffer, rhs.buffer);
|
||||||
|
len = rhs.len;
|
||||||
|
rhs.len = 0;
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buffer = rhs.buffer;
|
||||||
|
capacity = rhs.capacity;
|
||||||
|
len = rhs.len;
|
||||||
|
rhs.buffer = NULL;
|
||||||
|
rhs.capacity = 0;
|
||||||
|
rhs.len = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
String & String::operator = (const String &rhs)
|
||||||
|
{
|
||||||
|
if (this == &rhs) return *this;
|
||||||
|
|
||||||
|
if (rhs.buffer) copy(rhs.buffer, rhs.len);
|
||||||
|
else invalidate();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
|
String & String::operator = (String &&rval)
|
||||||
|
{
|
||||||
|
if (this != &rval) move(rval);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
String & String::operator = (StringSumHelper &&rval)
|
||||||
|
{
|
||||||
|
if (this != &rval) move(rval);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
String & String::operator = (const char *cstr)
|
||||||
|
{
|
||||||
|
if (cstr) copy(cstr, strlen(cstr));
|
||||||
|
else invalidate();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* concat */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
unsigned char String::concat(const String &s)
|
||||||
|
{
|
||||||
|
return concat(s.buffer, s.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::concat(const char *cstr, unsigned int length)
|
||||||
|
{
|
||||||
|
unsigned int newlen = len + length;
|
||||||
|
if (!cstr) return 0;
|
||||||
|
if (length == 0) return 1;
|
||||||
|
if (!reserve(newlen)) return 0;
|
||||||
|
strcpy(buffer + len, cstr);
|
||||||
|
len = newlen;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::concat(const char *cstr)
|
||||||
|
{
|
||||||
|
if (!cstr) return 0;
|
||||||
|
return concat(cstr, strlen(cstr));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::concat(char c)
|
||||||
|
{
|
||||||
|
char buf[2];
|
||||||
|
buf[0] = c;
|
||||||
|
buf[1] = 0;
|
||||||
|
return concat(buf, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::concat(unsigned char num)
|
||||||
|
{
|
||||||
|
char buf[4];
|
||||||
|
itoa(num, buf, 10);
|
||||||
|
return concat(buf, strlen(buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::concat(int num)
|
||||||
|
{
|
||||||
|
char buf[7];
|
||||||
|
itoa(num, buf, 10);
|
||||||
|
return concat(buf, strlen(buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::concat(unsigned int num)
|
||||||
|
{
|
||||||
|
char buf[6];
|
||||||
|
utoa(num, buf, 10);
|
||||||
|
return concat(buf, strlen(buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::concat(long num)
|
||||||
|
{
|
||||||
|
char buf[12];
|
||||||
|
ltoa(num, buf, 10);
|
||||||
|
return concat(buf, strlen(buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::concat(unsigned long num)
|
||||||
|
{
|
||||||
|
char buf[11];
|
||||||
|
ultoa(num, buf, 10);
|
||||||
|
return concat(buf, strlen(buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Concatenate */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, char c)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (!a.concat(c)) a.invalidate();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (!a.concat(num)) a.invalidate();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, int num)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (!a.concat(num)) a.invalidate();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (!a.concat(num)) a.invalidate();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, long num)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (!a.concat(num)) a.invalidate();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (!a.concat(num)) a.invalidate();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Comparison */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
int String::compareTo(const String &s) const
|
||||||
|
{
|
||||||
|
if (!buffer || !s.buffer) {
|
||||||
|
if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
|
||||||
|
if (buffer && len > 0) return *(unsigned char *)buffer;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return strcmp(buffer, s.buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::equals(const String &s2) const
|
||||||
|
{
|
||||||
|
return (len == s2.len && compareTo(s2) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::equals(const char *cstr) const
|
||||||
|
{
|
||||||
|
if (len == 0) return (cstr == NULL || *cstr == 0);
|
||||||
|
if (cstr == NULL) return buffer[0] == 0;
|
||||||
|
return strcmp(buffer, cstr) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::operator<(const String &rhs) const
|
||||||
|
{
|
||||||
|
return compareTo(rhs) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::operator>(const String &rhs) const
|
||||||
|
{
|
||||||
|
return compareTo(rhs) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::operator<=(const String &rhs) const
|
||||||
|
{
|
||||||
|
return compareTo(rhs) <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::operator>=(const String &rhs) const
|
||||||
|
{
|
||||||
|
return compareTo(rhs) >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::equalsIgnoreCase( const String &s2 ) const
|
||||||
|
{
|
||||||
|
if (this == &s2) return 1;
|
||||||
|
if (len != s2.len) return 0;
|
||||||
|
if (len == 0) return 1;
|
||||||
|
const char *p1 = buffer;
|
||||||
|
const char *p2 = s2.buffer;
|
||||||
|
while (*p1) {
|
||||||
|
if (tolower(*p1++) != tolower(*p2++)) return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::startsWith( const String &s2 ) const
|
||||||
|
{
|
||||||
|
if (len < s2.len) return 0;
|
||||||
|
return startsWith(s2, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::startsWith( const String &s2, unsigned int offset ) const
|
||||||
|
{
|
||||||
|
if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
|
||||||
|
return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::endsWith( const String &s2 ) const
|
||||||
|
{
|
||||||
|
if ( len < s2.len || !buffer || !s2.buffer) return 0;
|
||||||
|
return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Character Access */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
char String::charAt(unsigned int loc) const
|
||||||
|
{
|
||||||
|
return operator[](loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void String::setCharAt(unsigned int loc, char c)
|
||||||
|
{
|
||||||
|
if (loc < len) buffer[loc] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
char & String::operator[](unsigned int index)
|
||||||
|
{
|
||||||
|
static char dummy_writable_char;
|
||||||
|
if (index >= len || !buffer) {
|
||||||
|
dummy_writable_char = 0;
|
||||||
|
return dummy_writable_char;
|
||||||
|
}
|
||||||
|
return buffer[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
char String::operator[]( unsigned int index ) const
|
||||||
|
{
|
||||||
|
if (index >= len || !buffer) return 0;
|
||||||
|
return buffer[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
|
||||||
|
{
|
||||||
|
if (!bufsize || !buf) return;
|
||||||
|
if (index >= len) {
|
||||||
|
buf[0] = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unsigned int n = bufsize - 1;
|
||||||
|
if (n > len - index) n = len - index;
|
||||||
|
strncpy((char *)buf, buffer + index, n);
|
||||||
|
buf[n] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Search */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
int String::indexOf(char c) const
|
||||||
|
{
|
||||||
|
return indexOf(c, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::indexOf( char ch, unsigned int fromIndex ) const
|
||||||
|
{
|
||||||
|
if (fromIndex >= len) return -1;
|
||||||
|
const char* temp = strchr(buffer + fromIndex, ch);
|
||||||
|
if (temp == NULL) return -1;
|
||||||
|
return temp - buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::indexOf(const String &s2) const
|
||||||
|
{
|
||||||
|
return indexOf(s2, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::indexOf(const String &s2, unsigned int fromIndex) const
|
||||||
|
{
|
||||||
|
if (fromIndex >= len) return -1;
|
||||||
|
const char *found = strstr(buffer + fromIndex, s2.buffer);
|
||||||
|
if (found == NULL) return -1;
|
||||||
|
return found - buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::lastIndexOf( char theChar ) const
|
||||||
|
{
|
||||||
|
return lastIndexOf(theChar, len - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::lastIndexOf(char ch, unsigned int fromIndex) const
|
||||||
|
{
|
||||||
|
if (fromIndex >= len) return -1;
|
||||||
|
char tempchar = buffer[fromIndex + 1];
|
||||||
|
buffer[fromIndex + 1] = '\0';
|
||||||
|
char* temp = strrchr( buffer, ch );
|
||||||
|
buffer[fromIndex + 1] = tempchar;
|
||||||
|
if (temp == NULL) return -1;
|
||||||
|
return temp - buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::lastIndexOf(const String &s2) const
|
||||||
|
{
|
||||||
|
return lastIndexOf(s2, len - s2.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
|
||||||
|
{
|
||||||
|
if (s2.len == 0 || len == 0 || s2.len > len) return -1;
|
||||||
|
if (fromIndex >= len) fromIndex = len - 1;
|
||||||
|
int found = -1;
|
||||||
|
for (char *p = buffer; p <= buffer + fromIndex; p++) {
|
||||||
|
p = strstr(p, s2.buffer);
|
||||||
|
if (!p) break;
|
||||||
|
if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
|
||||||
|
}
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
String String::substring( unsigned int left ) const
|
||||||
|
{
|
||||||
|
return substring(left, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
String String::substring(unsigned int left, unsigned int right) const
|
||||||
|
{
|
||||||
|
if (left > right) {
|
||||||
|
unsigned int temp = right;
|
||||||
|
right = left;
|
||||||
|
left = temp;
|
||||||
|
}
|
||||||
|
String out;
|
||||||
|
if (left > len) return out;
|
||||||
|
if (right > len) right = len;
|
||||||
|
char temp = buffer[right]; // save the replaced character
|
||||||
|
buffer[right] = '\0';
|
||||||
|
out = buffer + left; // pointer arithmetic
|
||||||
|
buffer[right] = temp; //restore character
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Modification */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
void String::replace(char find, char replace)
|
||||||
|
{
|
||||||
|
if (!buffer) return;
|
||||||
|
for (char *p = buffer; *p; p++) {
|
||||||
|
if (*p == find) *p = replace;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void String::replace(const String& find, const String& replace)
|
||||||
|
{
|
||||||
|
if (len == 0 || find.len == 0) return;
|
||||||
|
int diff = replace.len - find.len;
|
||||||
|
char *readFrom = buffer;
|
||||||
|
char *foundAt;
|
||||||
|
if (diff == 0) {
|
||||||
|
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
||||||
|
memcpy(foundAt, replace.buffer, replace.len);
|
||||||
|
readFrom = foundAt + replace.len;
|
||||||
|
}
|
||||||
|
} else if (diff < 0) {
|
||||||
|
char *writeTo = buffer;
|
||||||
|
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
||||||
|
unsigned int n = foundAt - readFrom;
|
||||||
|
memcpy(writeTo, readFrom, n);
|
||||||
|
writeTo += n;
|
||||||
|
memcpy(writeTo, replace.buffer, replace.len);
|
||||||
|
writeTo += replace.len;
|
||||||
|
readFrom = foundAt + find.len;
|
||||||
|
len += diff;
|
||||||
|
}
|
||||||
|
strcpy(writeTo, readFrom);
|
||||||
|
} else {
|
||||||
|
unsigned int size = len; // compute size needed for result
|
||||||
|
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
||||||
|
readFrom = foundAt + find.len;
|
||||||
|
size += diff;
|
||||||
|
}
|
||||||
|
if (size == len) return;
|
||||||
|
if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
|
||||||
|
int index = len - 1;
|
||||||
|
while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
|
||||||
|
readFrom = buffer + index + find.len;
|
||||||
|
memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
|
||||||
|
len += diff;
|
||||||
|
buffer[len] = 0;
|
||||||
|
memcpy(buffer + index, replace.buffer, replace.len);
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void String::toLowerCase(void)
|
||||||
|
{
|
||||||
|
if (!buffer) return;
|
||||||
|
for (char *p = buffer; *p; p++) {
|
||||||
|
*p = tolower(*p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void String::toUpperCase(void)
|
||||||
|
{
|
||||||
|
if (!buffer) return;
|
||||||
|
for (char *p = buffer; *p; p++) {
|
||||||
|
*p = toupper(*p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void String::trim(void)
|
||||||
|
{
|
||||||
|
if (!buffer || len == 0) return;
|
||||||
|
char *begin = buffer;
|
||||||
|
while (isspace(*begin)) begin++;
|
||||||
|
char *end = buffer + len - 1;
|
||||||
|
while (isspace(*end) && end >= begin) end--;
|
||||||
|
len = end + 1 - begin;
|
||||||
|
if (begin > buffer) memcpy(buffer, begin, len);
|
||||||
|
buffer[len] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Parsing / Conversion */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
long String::toInt(void) const
|
||||||
|
{
|
||||||
|
if (buffer) return atol(buffer);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,205 @@
|
|||||||
|
/*
|
||||||
|
WString.h - String library for Wiring & Arduino
|
||||||
|
...mostly rewritten by Paul Stoffregen...
|
||||||
|
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
||||||
|
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef String_class_h
|
||||||
|
#define String_class_h
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
|
||||||
|
// When compiling programs with this class, the following gcc parameters
|
||||||
|
// dramatically increase performance and memory (RAM) efficiency, typically
|
||||||
|
// with little or no increase in code size.
|
||||||
|
// -felide-constructors
|
||||||
|
// -std=c++0x
|
||||||
|
|
||||||
|
class __FlashStringHelper;
|
||||||
|
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
|
||||||
|
|
||||||
|
// An inherited class for holding the result of a concatenation. These
|
||||||
|
// result objects are assumed to be writable by subsequent concatenations.
|
||||||
|
class StringSumHelper;
|
||||||
|
|
||||||
|
// The string class
|
||||||
|
class String
|
||||||
|
{
|
||||||
|
// use a function pointer to allow for "if (s)" without the
|
||||||
|
// complications of an operator bool(). for more information, see:
|
||||||
|
// http://www.artima.com/cppsource/safebool.html
|
||||||
|
typedef void (String::*StringIfHelperType)() const;
|
||||||
|
void StringIfHelper() const {}
|
||||||
|
|
||||||
|
public:
|
||||||
|
// constructors
|
||||||
|
// creates a copy of the initial value.
|
||||||
|
// if the initial value is null or invalid, or if memory allocation
|
||||||
|
// fails, the string will be marked as invalid (i.e. "if (s)" will
|
||||||
|
// be false).
|
||||||
|
String(const char *cstr = "");
|
||||||
|
String(const String &str);
|
||||||
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
|
String(String &&rval);
|
||||||
|
String(StringSumHelper &&rval);
|
||||||
|
#endif
|
||||||
|
explicit String(char c);
|
||||||
|
explicit String(unsigned char, unsigned char base=10);
|
||||||
|
explicit String(int, unsigned char base=10);
|
||||||
|
explicit String(unsigned int, unsigned char base=10);
|
||||||
|
explicit String(long, unsigned char base=10);
|
||||||
|
explicit String(unsigned long, unsigned char base=10);
|
||||||
|
~String(void);
|
||||||
|
|
||||||
|
// memory management
|
||||||
|
// return true on success, false on failure (in which case, the string
|
||||||
|
// is left unchanged). reserve(0), if successful, will validate an
|
||||||
|
// invalid string (i.e., "if (s)" will be true afterwards)
|
||||||
|
unsigned char reserve(unsigned int size);
|
||||||
|
inline unsigned int length(void) const {return len;}
|
||||||
|
|
||||||
|
// creates a copy of the assigned value. if the value is null or
|
||||||
|
// invalid, or if the memory allocation fails, the string will be
|
||||||
|
// marked as invalid ("if (s)" will be false).
|
||||||
|
String & operator = (const String &rhs);
|
||||||
|
String & operator = (const char *cstr);
|
||||||
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
|
String & operator = (String &&rval);
|
||||||
|
String & operator = (StringSumHelper &&rval);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// concatenate (works w/ built-in types)
|
||||||
|
|
||||||
|
// returns true on success, false on failure (in which case, the string
|
||||||
|
// is left unchanged). if the argument is null or invalid, the
|
||||||
|
// concatenation is considered unsucessful.
|
||||||
|
unsigned char concat(const String &str);
|
||||||
|
unsigned char concat(const char *cstr);
|
||||||
|
unsigned char concat(char c);
|
||||||
|
unsigned char concat(unsigned char c);
|
||||||
|
unsigned char concat(int num);
|
||||||
|
unsigned char concat(unsigned int num);
|
||||||
|
unsigned char concat(long num);
|
||||||
|
unsigned char concat(unsigned long num);
|
||||||
|
|
||||||
|
// if there's not enough memory for the concatenated value, the string
|
||||||
|
// will be left unchanged (but this isn't signalled in any way)
|
||||||
|
String & operator += (const String &rhs) {concat(rhs); return (*this);}
|
||||||
|
String & operator += (const char *cstr) {concat(cstr); return (*this);}
|
||||||
|
String & operator += (char c) {concat(c); return (*this);}
|
||||||
|
String & operator += (unsigned char num) {concat(num); return (*this);}
|
||||||
|
String & operator += (int num) {concat(num); return (*this);}
|
||||||
|
String & operator += (unsigned int num) {concat(num); return (*this);}
|
||||||
|
String & operator += (long num) {concat(num); return (*this);}
|
||||||
|
String & operator += (unsigned long num) {concat(num); return (*this);}
|
||||||
|
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
|
||||||
|
|
||||||
|
// comparison (only works w/ Strings and "strings")
|
||||||
|
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
|
||||||
|
int compareTo(const String &s) const;
|
||||||
|
unsigned char equals(const String &s) const;
|
||||||
|
unsigned char equals(const char *cstr) const;
|
||||||
|
unsigned char operator == (const String &rhs) const {return equals(rhs);}
|
||||||
|
unsigned char operator == (const char *cstr) const {return equals(cstr);}
|
||||||
|
unsigned char operator != (const String &rhs) const {return !equals(rhs);}
|
||||||
|
unsigned char operator != (const char *cstr) const {return !equals(cstr);}
|
||||||
|
unsigned char operator < (const String &rhs) const;
|
||||||
|
unsigned char operator > (const String &rhs) const;
|
||||||
|
unsigned char operator <= (const String &rhs) const;
|
||||||
|
unsigned char operator >= (const String &rhs) const;
|
||||||
|
unsigned char equalsIgnoreCase(const String &s) const;
|
||||||
|
unsigned char startsWith( const String &prefix) const;
|
||||||
|
unsigned char startsWith(const String &prefix, unsigned int offset) const;
|
||||||
|
unsigned char endsWith(const String &suffix) const;
|
||||||
|
|
||||||
|
// character acccess
|
||||||
|
char charAt(unsigned int index) const;
|
||||||
|
void setCharAt(unsigned int index, char c);
|
||||||
|
char operator [] (unsigned int index) const;
|
||||||
|
char& operator [] (unsigned int index);
|
||||||
|
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
|
||||||
|
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
|
||||||
|
{getBytes((unsigned char *)buf, bufsize, index);}
|
||||||
|
|
||||||
|
// search
|
||||||
|
int indexOf( char ch ) const;
|
||||||
|
int indexOf( char ch, unsigned int fromIndex ) const;
|
||||||
|
int indexOf( const String &str ) const;
|
||||||
|
int indexOf( const String &str, unsigned int fromIndex ) const;
|
||||||
|
int lastIndexOf( char ch ) const;
|
||||||
|
int lastIndexOf( char ch, unsigned int fromIndex ) const;
|
||||||
|
int lastIndexOf( const String &str ) const;
|
||||||
|
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
||||||
|
String substring( unsigned int beginIndex ) const;
|
||||||
|
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
||||||
|
|
||||||
|
// modification
|
||||||
|
void replace(char find, char replace);
|
||||||
|
void replace(const String& find, const String& replace);
|
||||||
|
void toLowerCase(void);
|
||||||
|
void toUpperCase(void);
|
||||||
|
void trim(void);
|
||||||
|
|
||||||
|
// parsing/conversion
|
||||||
|
long toInt(void) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
char *buffer; // the actual char array
|
||||||
|
unsigned int capacity; // the array length minus one (for the '\0')
|
||||||
|
unsigned int len; // the String length (not counting the '\0')
|
||||||
|
unsigned char flags; // unused, for future features
|
||||||
|
protected:
|
||||||
|
void init(void);
|
||||||
|
void invalidate(void);
|
||||||
|
unsigned char changeBuffer(unsigned int maxStrLen);
|
||||||
|
unsigned char concat(const char *cstr, unsigned int length);
|
||||||
|
|
||||||
|
// copy and move
|
||||||
|
String & copy(const char *cstr, unsigned int length);
|
||||||
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
|
void move(String &rhs);
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
class StringSumHelper : public String
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
StringSumHelper(const String &s) : String(s) {}
|
||||||
|
StringSumHelper(const char *p) : String(p) {}
|
||||||
|
StringSumHelper(char c) : String(c) {}
|
||||||
|
StringSumHelper(unsigned char num) : String(num) {}
|
||||||
|
StringSumHelper(int num) : String(num) {}
|
||||||
|
StringSumHelper(unsigned int num) : String(num) {}
|
||||||
|
StringSumHelper(long num) : String(num) {}
|
||||||
|
StringSumHelper(unsigned long num) : String(num) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
|
#endif // String_class_h
|
@ -0,0 +1,515 @@
|
|||||||
|
#ifndef Binary_h
|
||||||
|
#define Binary_h
|
||||||
|
|
||||||
|
#define B0 0
|
||||||
|
#define B00 0
|
||||||
|
#define B000 0
|
||||||
|
#define B0000 0
|
||||||
|
#define B00000 0
|
||||||
|
#define B000000 0
|
||||||
|
#define B0000000 0
|
||||||
|
#define B00000000 0
|
||||||
|
#define B1 1
|
||||||
|
#define B01 1
|
||||||
|
#define B001 1
|
||||||
|
#define B0001 1
|
||||||
|
#define B00001 1
|
||||||
|
#define B000001 1
|
||||||
|
#define B0000001 1
|
||||||
|
#define B00000001 1
|
||||||
|
#define B10 2
|
||||||
|
#define B010 2
|
||||||
|
#define B0010 2
|
||||||
|
#define B00010 2
|
||||||
|
#define B000010 2
|
||||||
|
#define B0000010 2
|
||||||
|
#define B00000010 2
|
||||||
|
#define B11 3
|
||||||
|
#define B011 3
|
||||||
|
#define B0011 3
|
||||||
|
#define B00011 3
|
||||||
|
#define B000011 3
|
||||||
|
#define B0000011 3
|
||||||
|
#define B00000011 3
|
||||||
|
#define B100 4
|
||||||
|
#define B0100 4
|
||||||
|
#define B00100 4
|
||||||
|
#define B000100 4
|
||||||
|
#define B0000100 4
|
||||||
|
#define B00000100 4
|
||||||
|
#define B101 5
|
||||||
|
#define B0101 5
|
||||||
|
#define B00101 5
|
||||||
|
#define B000101 5
|
||||||
|
#define B0000101 5
|
||||||
|
#define B00000101 5
|
||||||
|
#define B110 6
|
||||||
|
#define B0110 6
|
||||||
|
#define B00110 6
|
||||||
|
#define B000110 6
|
||||||
|
#define B0000110 6
|
||||||
|
#define B00000110 6
|
||||||
|
#define B111 7
|
||||||
|
#define B0111 7
|
||||||
|
#define B00111 7
|
||||||
|
#define B000111 7
|
||||||
|
#define B0000111 7
|
||||||
|
#define B00000111 7
|
||||||
|
#define B1000 8
|
||||||
|
#define B01000 8
|
||||||
|
#define B001000 8
|
||||||
|
#define B0001000 8
|
||||||
|
#define B00001000 8
|
||||||
|
#define B1001 9
|
||||||
|
#define B01001 9
|
||||||
|
#define B001001 9
|
||||||
|
#define B0001001 9
|
||||||
|
#define B00001001 9
|
||||||
|
#define B1010 10
|
||||||
|
#define B01010 10
|
||||||
|
#define B001010 10
|
||||||
|
#define B0001010 10
|
||||||
|
#define B00001010 10
|
||||||
|
#define B1011 11
|
||||||
|
#define B01011 11
|
||||||
|
#define B001011 11
|
||||||
|
#define B0001011 11
|
||||||
|
#define B00001011 11
|
||||||
|
#define B1100 12
|
||||||
|
#define B01100 12
|
||||||
|
#define B001100 12
|
||||||
|
#define B0001100 12
|
||||||
|
#define B00001100 12
|
||||||
|
#define B1101 13
|
||||||
|
#define B01101 13
|
||||||
|
#define B001101 13
|
||||||
|
#define B0001101 13
|
||||||
|
#define B00001101 13
|
||||||
|
#define B1110 14
|
||||||
|
#define B01110 14
|
||||||
|
#define B001110 14
|
||||||
|
#define B0001110 14
|
||||||
|
#define B00001110 14
|
||||||
|
#define B1111 15
|
||||||
|
#define B01111 15
|
||||||
|
#define B001111 15
|
||||||
|
#define B0001111 15
|
||||||
|
#define B00001111 15
|
||||||
|
#define B10000 16
|
||||||
|
#define B010000 16
|
||||||
|
#define B0010000 16
|
||||||
|
#define B00010000 16
|
||||||
|
#define B10001 17
|
||||||
|
#define B010001 17
|
||||||
|
#define B0010001 17
|
||||||
|
#define B00010001 17
|
||||||
|
#define B10010 18
|
||||||
|
#define B010010 18
|
||||||
|
#define B0010010 18
|
||||||
|
#define B00010010 18
|
||||||
|
#define B10011 19
|
||||||
|
#define B010011 19
|
||||||
|
#define B0010011 19
|
||||||
|
#define B00010011 19
|
||||||
|
#define B10100 20
|
||||||
|
#define B010100 20
|
||||||
|
#define B0010100 20
|
||||||
|
#define B00010100 20
|
||||||
|
#define B10101 21
|
||||||
|
#define B010101 21
|
||||||
|
#define B0010101 21
|
||||||
|
#define B00010101 21
|
||||||
|
#define B10110 22
|
||||||
|
#define B010110 22
|
||||||
|
#define B0010110 22
|
||||||
|
#define B00010110 22
|
||||||
|
#define B10111 23
|
||||||
|
#define B010111 23
|
||||||
|
#define B0010111 23
|
||||||
|
#define B00010111 23
|
||||||
|
#define B11000 24
|
||||||
|
#define B011000 24
|
||||||
|
#define B0011000 24
|
||||||
|
#define B00011000 24
|
||||||
|
#define B11001 25
|
||||||
|
#define B011001 25
|
||||||
|
#define B0011001 25
|
||||||
|
#define B00011001 25
|
||||||
|
#define B11010 26
|
||||||
|
#define B011010 26
|
||||||
|
#define B0011010 26
|
||||||
|
#define B00011010 26
|
||||||
|
#define B11011 27
|
||||||
|
#define B011011 27
|
||||||
|
#define B0011011 27
|
||||||
|
#define B00011011 27
|
||||||
|
#define B11100 28
|
||||||
|
#define B011100 28
|
||||||
|
#define B0011100 28
|
||||||
|
#define B00011100 28
|
||||||
|
#define B11101 29
|
||||||
|
#define B011101 29
|
||||||
|
#define B0011101 29
|
||||||
|
#define B00011101 29
|
||||||
|
#define B11110 30
|
||||||
|
#define B011110 30
|
||||||
|
#define B0011110 30
|
||||||
|
#define B00011110 30
|
||||||
|
#define B11111 31
|
||||||
|
#define B011111 31
|
||||||
|
#define B0011111 31
|
||||||
|
#define B00011111 31
|
||||||
|
#define B100000 32
|
||||||
|
#define B0100000 32
|
||||||
|
#define B00100000 32
|
||||||
|
#define B100001 33
|
||||||
|
#define B0100001 33
|
||||||
|
#define B00100001 33
|
||||||
|
#define B100010 34
|
||||||
|
#define B0100010 34
|
||||||
|
#define B00100010 34
|
||||||
|
#define B100011 35
|
||||||
|
#define B0100011 35
|
||||||
|
#define B00100011 35
|
||||||
|
#define B100100 36
|
||||||
|
#define B0100100 36
|
||||||
|
#define B00100100 36
|
||||||
|
#define B100101 37
|
||||||
|
#define B0100101 37
|
||||||
|
#define B00100101 37
|
||||||
|
#define B100110 38
|
||||||
|
#define B0100110 38
|
||||||
|
#define B00100110 38
|
||||||
|
#define B100111 39
|
||||||
|
#define B0100111 39
|
||||||
|
#define B00100111 39
|
||||||
|
#define B101000 40
|
||||||
|
#define B0101000 40
|
||||||
|
#define B00101000 40
|
||||||
|
#define B101001 41
|
||||||
|
#define B0101001 41
|
||||||
|
#define B00101001 41
|
||||||
|
#define B101010 42
|
||||||
|
#define B0101010 42
|
||||||
|
#define B00101010 42
|
||||||
|
#define B101011 43
|
||||||
|
#define B0101011 43
|
||||||
|
#define B00101011 43
|
||||||
|
#define B101100 44
|
||||||
|
#define B0101100 44
|
||||||
|
#define B00101100 44
|
||||||
|
#define B101101 45
|
||||||
|
#define B0101101 45
|
||||||
|
#define B00101101 45
|
||||||
|
#define B101110 46
|
||||||
|
#define B0101110 46
|
||||||
|
#define B00101110 46
|
||||||
|
#define B101111 47
|
||||||
|
#define B0101111 47
|
||||||
|
#define B00101111 47
|
||||||
|
#define B110000 48
|
||||||
|
#define B0110000 48
|
||||||
|
#define B00110000 48
|
||||||
|
#define B110001 49
|
||||||
|
#define B0110001 49
|
||||||
|
#define B00110001 49
|
||||||
|
#define B110010 50
|
||||||
|
#define B0110010 50
|
||||||
|
#define B00110010 50
|
||||||
|
#define B110011 51
|
||||||
|
#define B0110011 51
|
||||||
|
#define B00110011 51
|
||||||
|
#define B110100 52
|
||||||
|
#define B0110100 52
|
||||||
|
#define B00110100 52
|
||||||
|
#define B110101 53
|
||||||
|
#define B0110101 53
|
||||||
|
#define B00110101 53
|
||||||
|
#define B110110 54
|
||||||
|
#define B0110110 54
|
||||||
|
#define B00110110 54
|
||||||
|
#define B110111 55
|
||||||
|
#define B0110111 55
|
||||||
|
#define B00110111 55
|
||||||
|
#define B111000 56
|
||||||
|
#define B0111000 56
|
||||||
|
#define B00111000 56
|
||||||
|
#define B111001 57
|
||||||
|
#define B0111001 57
|
||||||
|
#define B00111001 57
|
||||||
|
#define B111010 58
|
||||||
|
#define B0111010 58
|
||||||
|
#define B00111010 58
|
||||||
|
#define B111011 59
|
||||||
|
#define B0111011 59
|
||||||
|
#define B00111011 59
|
||||||
|
#define B111100 60
|
||||||
|
#define B0111100 60
|
||||||
|
#define B00111100 60
|
||||||
|
#define B111101 61
|
||||||
|
#define B0111101 61
|
||||||
|
#define B00111101 61
|
||||||
|
#define B111110 62
|
||||||
|
#define B0111110 62
|
||||||
|
#define B00111110 62
|
||||||
|
#define B111111 63
|
||||||
|
#define B0111111 63
|
||||||
|
#define B00111111 63
|
||||||
|
#define B1000000 64
|
||||||
|
#define B01000000 64
|
||||||
|
#define B1000001 65
|
||||||
|
#define B01000001 65
|
||||||
|
#define B1000010 66
|
||||||
|
#define B01000010 66
|
||||||
|
#define B1000011 67
|
||||||
|
#define B01000011 67
|
||||||
|
#define B1000100 68
|
||||||
|
#define B01000100 68
|
||||||
|
#define B1000101 69
|
||||||
|
#define B01000101 69
|
||||||
|
#define B1000110 70
|
||||||
|
#define B01000110 70
|
||||||
|
#define B1000111 71
|
||||||
|
#define B01000111 71
|
||||||
|
#define B1001000 72
|
||||||
|
#define B01001000 72
|
||||||
|
#define B1001001 73
|
||||||
|
#define B01001001 73
|
||||||
|
#define B1001010 74
|
||||||
|
#define B01001010 74
|
||||||
|
#define B1001011 75
|
||||||
|
#define B01001011 75
|
||||||
|
#define B1001100 76
|
||||||
|
#define B01001100 76
|
||||||
|
#define B1001101 77
|
||||||
|
#define B01001101 77
|
||||||
|
#define B1001110 78
|
||||||
|
#define B01001110 78
|
||||||
|
#define B1001111 79
|
||||||
|
#define B01001111 79
|
||||||
|
#define B1010000 80
|
||||||
|
#define B01010000 80
|
||||||
|
#define B1010001 81
|
||||||
|
#define B01010001 81
|
||||||
|
#define B1010010 82
|
||||||
|
#define B01010010 82
|
||||||
|
#define B1010011 83
|
||||||
|
#define B01010011 83
|
||||||
|
#define B1010100 84
|
||||||
|
#define B01010100 84
|
||||||
|
#define B1010101 85
|
||||||
|
#define B01010101 85
|
||||||
|
#define B1010110 86
|
||||||
|
#define B01010110 86
|
||||||
|
#define B1010111 87
|
||||||
|
#define B01010111 87
|
||||||
|
#define B1011000 88
|
||||||
|
#define B01011000 88
|
||||||
|
#define B1011001 89
|
||||||
|
#define B01011001 89
|
||||||
|
#define B1011010 90
|
||||||
|
#define B01011010 90
|
||||||
|
#define B1011011 91
|
||||||
|
#define B01011011 91
|
||||||
|
#define B1011100 92
|
||||||
|
#define B01011100 92
|
||||||
|
#define B1011101 93
|
||||||
|
#define B01011101 93
|
||||||
|
#define B1011110 94
|
||||||
|
#define B01011110 94
|
||||||
|
#define B1011111 95
|
||||||
|
#define B01011111 95
|
||||||
|
#define B1100000 96
|
||||||
|
#define B01100000 96
|
||||||
|
#define B1100001 97
|
||||||
|
#define B01100001 97
|
||||||
|
#define B1100010 98
|
||||||
|
#define B01100010 98
|
||||||
|
#define B1100011 99
|
||||||
|
#define B01100011 99
|
||||||
|
#define B1100100 100
|
||||||
|
#define B01100100 100
|
||||||
|
#define B1100101 101
|
||||||
|
#define B01100101 101
|
||||||
|
#define B1100110 102
|
||||||
|
#define B01100110 102
|
||||||
|
#define B1100111 103
|
||||||
|
#define B01100111 103
|
||||||
|
#define B1101000 104
|
||||||
|
#define B01101000 104
|
||||||
|
#define B1101001 105
|
||||||
|
#define B01101001 105
|
||||||
|
#define B1101010 106
|
||||||
|
#define B01101010 106
|
||||||
|
#define B1101011 107
|
||||||
|
#define B01101011 107
|
||||||
|
#define B1101100 108
|
||||||
|
#define B01101100 108
|
||||||
|
#define B1101101 109
|
||||||
|
#define B01101101 109
|
||||||
|
#define B1101110 110
|
||||||
|
#define B01101110 110
|
||||||
|
#define B1101111 111
|
||||||
|
#define B01101111 111
|
||||||
|
#define B1110000 112
|
||||||
|
#define B01110000 112
|
||||||
|
#define B1110001 113
|
||||||
|
#define B01110001 113
|
||||||
|
#define B1110010 114
|
||||||
|
#define B01110010 114
|
||||||
|
#define B1110011 115
|
||||||
|
#define B01110011 115
|
||||||
|
#define B1110100 116
|
||||||
|
#define B01110100 116
|
||||||
|
#define B1110101 117
|
||||||
|
#define B01110101 117
|
||||||
|
#define B1110110 118
|
||||||
|
#define B01110110 118
|
||||||
|
#define B1110111 119
|
||||||
|
#define B01110111 119
|
||||||
|
#define B1111000 120
|
||||||
|
#define B01111000 120
|
||||||
|
#define B1111001 121
|
||||||
|
#define B01111001 121
|
||||||
|
#define B1111010 122
|
||||||
|
#define B01111010 122
|
||||||
|
#define B1111011 123
|
||||||
|
#define B01111011 123
|
||||||
|
#define B1111100 124
|
||||||
|
#define B01111100 124
|
||||||
|
#define B1111101 125
|
||||||
|
#define B01111101 125
|
||||||
|
#define B1111110 126
|
||||||
|
#define B01111110 126
|
||||||
|
#define B1111111 127
|
||||||
|
#define B01111111 127
|
||||||
|
#define B10000000 128
|
||||||
|
#define B10000001 129
|
||||||
|
#define B10000010 130
|
||||||
|
#define B10000011 131
|
||||||
|
#define B10000100 132
|
||||||
|
#define B10000101 133
|
||||||
|
#define B10000110 134
|
||||||
|
#define B10000111 135
|
||||||
|
#define B10001000 136
|
||||||
|
#define B10001001 137
|
||||||
|
#define B10001010 138
|
||||||
|
#define B10001011 139
|
||||||
|
#define B10001100 140
|
||||||
|
#define B10001101 141
|
||||||
|
#define B10001110 142
|
||||||
|
#define B10001111 143
|
||||||
|
#define B10010000 144
|
||||||
|
#define B10010001 145
|
||||||
|
#define B10010010 146
|
||||||
|
#define B10010011 147
|
||||||
|
#define B10010100 148
|
||||||
|
#define B10010101 149
|
||||||
|
#define B10010110 150
|
||||||
|
#define B10010111 151
|
||||||
|
#define B10011000 152
|
||||||
|
#define B10011001 153
|
||||||
|
#define B10011010 154
|
||||||
|
#define B10011011 155
|
||||||
|
#define B10011100 156
|
||||||
|
#define B10011101 157
|
||||||
|
#define B10011110 158
|
||||||
|
#define B10011111 159
|
||||||
|
#define B10100000 160
|
||||||
|
#define B10100001 161
|
||||||
|
#define B10100010 162
|
||||||
|
#define B10100011 163
|
||||||
|
#define B10100100 164
|
||||||
|
#define B10100101 165
|
||||||
|
#define B10100110 166
|
||||||
|
#define B10100111 167
|
||||||
|
#define B10101000 168
|
||||||
|
#define B10101001 169
|
||||||
|
#define B10101010 170
|
||||||
|
#define B10101011 171
|
||||||
|
#define B10101100 172
|
||||||
|
#define B10101101 173
|
||||||
|
#define B10101110 174
|
||||||
|
#define B10101111 175
|
||||||
|
#define B10110000 176
|
||||||
|
#define B10110001 177
|
||||||
|
#define B10110010 178
|
||||||
|
#define B10110011 179
|
||||||
|
#define B10110100 180
|
||||||
|
#define B10110101 181
|
||||||
|
#define B10110110 182
|
||||||
|
#define B10110111 183
|
||||||
|
#define B10111000 184
|
||||||
|
#define B10111001 185
|
||||||
|
#define B10111010 186
|
||||||
|
#define B10111011 187
|
||||||
|
#define B10111100 188
|
||||||
|
#define B10111101 189
|
||||||
|
#define B10111110 190
|
||||||
|
#define B10111111 191
|
||||||
|
#define B11000000 192
|
||||||
|
#define B11000001 193
|
||||||
|
#define B11000010 194
|
||||||
|
#define B11000011 195
|
||||||
|
#define B11000100 196
|
||||||
|
#define B11000101 197
|
||||||
|
#define B11000110 198
|
||||||
|
#define B11000111 199
|
||||||
|
#define B11001000 200
|
||||||
|
#define B11001001 201
|
||||||
|
#define B11001010 202
|
||||||
|
#define B11001011 203
|
||||||
|
#define B11001100 204
|
||||||
|
#define B11001101 205
|
||||||
|
#define B11001110 206
|
||||||
|
#define B11001111 207
|
||||||
|
#define B11010000 208
|
||||||
|
#define B11010001 209
|
||||||
|
#define B11010010 210
|
||||||
|
#define B11010011 211
|
||||||
|
#define B11010100 212
|
||||||
|
#define B11010101 213
|
||||||
|
#define B11010110 214
|
||||||
|
#define B11010111 215
|
||||||
|
#define B11011000 216
|
||||||
|
#define B11011001 217
|
||||||
|
#define B11011010 218
|
||||||
|
#define B11011011 219
|
||||||
|
#define B11011100 220
|
||||||
|
#define B11011101 221
|
||||||
|
#define B11011110 222
|
||||||
|
#define B11011111 223
|
||||||
|
#define B11100000 224
|
||||||
|
#define B11100001 225
|
||||||
|
#define B11100010 226
|
||||||
|
#define B11100011 227
|
||||||
|
#define B11100100 228
|
||||||
|
#define B11100101 229
|
||||||
|
#define B11100110 230
|
||||||
|
#define B11100111 231
|
||||||
|
#define B11101000 232
|
||||||
|
#define B11101001 233
|
||||||
|
#define B11101010 234
|
||||||
|
#define B11101011 235
|
||||||
|
#define B11101100 236
|
||||||
|
#define B11101101 237
|
||||||
|
#define B11101110 238
|
||||||
|
#define B11101111 239
|
||||||
|
#define B11110000 240
|
||||||
|
#define B11110001 241
|
||||||
|
#define B11110010 242
|
||||||
|
#define B11110011 243
|
||||||
|
#define B11110100 244
|
||||||
|
#define B11110101 245
|
||||||
|
#define B11110110 246
|
||||||
|
#define B11110111 247
|
||||||
|
#define B11111000 248
|
||||||
|
#define B11111001 249
|
||||||
|
#define B11111010 250
|
||||||
|
#define B11111011 251
|
||||||
|
#define B11111100 252
|
||||||
|
#define B11111101 253
|
||||||
|
#define B11111110 254
|
||||||
|
#define B11111111 255
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,20 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
|
||||||
|
#if defined(USBCON)
|
||||||
|
USBDevice.attach();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
setup();
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
loop();
|
||||||
|
if (serialEventRun) serialEventRun();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
|||||||
|
#include <new.h>
|
||||||
|
|
||||||
|
void * operator new(size_t size)
|
||||||
|
{
|
||||||
|
return malloc(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void * ptr)
|
||||||
|
{
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
|
||||||
|
void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
|
||||||
|
void __cxa_guard_abort (__guard *) {};
|
||||||
|
|
||||||
|
void __cxa_pure_virtual(void) {};
|
||||||
|
|
@ -0,0 +1,22 @@
|
|||||||
|
/* Header to define new/delete operators as they aren't provided by avr-gcc by default
|
||||||
|
Taken from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef NEW_H
|
||||||
|
#define NEW_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void * operator new(size_t size);
|
||||||
|
void operator delete(void * ptr);
|
||||||
|
|
||||||
|
__extension__ typedef int __guard __attribute__((mode (__DI__)));
|
||||||
|
|
||||||
|
extern "C" int __cxa_guard_acquire(__guard *);
|
||||||
|
extern "C" void __cxa_guard_release (__guard *);
|
||||||
|
extern "C" void __cxa_guard_abort (__guard *);
|
||||||
|
|
||||||
|
extern "C" void __cxa_pure_virtual(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -0,0 +1,324 @@
|
|||||||
|
/*
|
||||||
|
wiring.c - Partial implementation of the Wiring API for the ATmega8.
|
||||||
|
Part of Arduino - http://www.arduino.cc/
|
||||||
|
|
||||||
|
Copyright (c) 2005-2006 David A. Mellis
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General
|
||||||
|
Public License along with this library; if not, write to the
|
||||||
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "wiring_private.h"
|
||||||
|
|
||||||
|
// the prescaler is set so that timer0 ticks every 64 clock cycles, and the
|
||||||
|
// the overflow handler is called every 256 ticks.
|
||||||
|
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
|
||||||
|
|
||||||
|
// the whole number of milliseconds per timer0 overflow
|
||||||
|
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
|
||||||
|
|
||||||
|
// the fractional number of milliseconds per timer0 overflow. we shift right
|
||||||
|
// by three to fit these numbers into a byte. (for the clock speeds we care
|
||||||
|
// about - 8 and 16 MHz - this doesn't lose precision.)
|
||||||
|
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
|
||||||
|
#define FRACT_MAX (1000 >> 3)
|
||||||
|
|
||||||
|
volatile unsigned long timer0_overflow_count = 0;
|
||||||
|
volatile unsigned long timer0_millis = 0;
|
||||||
|
static unsigned char timer0_fract = 0;
|
||||||
|
|
||||||
|
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||||||
|
SIGNAL(TIM0_OVF_vect)
|
||||||
|
#else
|
||||||
|
SIGNAL(TIMER0_OVF_vect)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
// copy these to local variables so they can be stored in registers
|
||||||
|
// (volatile variables must be read from memory on every access)
|
||||||
|
unsigned long m = timer0_millis;
|
||||||
|
unsigned char f = timer0_fract;
|
||||||
|
|
||||||
|
m += MILLIS_INC;
|
||||||
|
f += FRACT_INC;
|
||||||
|
if (f >= FRACT_MAX) {
|
||||||
|
f -= FRACT_MAX;
|
||||||
|
m += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
timer0_fract = f;
|
||||||
|
timer0_millis = m;
|
||||||
|
timer0_overflow_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long millis()
|
||||||
|
{
|
||||||
|
unsigned long m;
|
||||||
|
uint8_t oldSREG = SREG;
|
||||||
|
|
||||||
|
// disable interrupts while we read timer0_millis or we might get an
|
||||||
|
// inconsistent value (e.g. in the middle of a write to timer0_millis)
|
||||||
|
cli();
|
||||||
|
m = timer0_millis;
|
||||||
|
SREG = oldSREG;
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long micros() {
|
||||||
|
unsigned long m;
|
||||||
|
uint8_t oldSREG = SREG, t;
|
||||||
|
|
||||||
|
cli();
|
||||||
|
m = timer0_overflow_count;
|
||||||
|
#if defined(TCNT0)
|
||||||
|
t = TCNT0;
|
||||||
|
#elif defined(TCNT0L)
|
||||||
|
t = TCNT0L;
|
||||||
|
#else
|
||||||
|
#error TIMER 0 not defined
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef TIFR0
|
||||||
|
if ((TIFR0 & _BV(TOV0)) && (t < 255))
|
||||||
|
m++;
|
||||||
|
#else
|
||||||
|
if ((TIFR & _BV(TOV0)) && (t < 255))
|
||||||
|
m++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
SREG = oldSREG;
|
||||||
|
|
||||||
|
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
|
||||||
|
}
|
||||||
|
|
||||||
|
void delay(unsigned long ms)
|
||||||
|
{
|
||||||
|
uint16_t start = (uint16_t)micros();
|
||||||
|
|
||||||
|
while (ms > 0) {
|
||||||
|
if (((uint16_t)micros() - start) >= 1000) {
|
||||||
|
ms--;
|
||||||
|
start += 1000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
|
||||||
|
void delayMicroseconds(unsigned int us)
|
||||||
|
{
|
||||||
|
// calling avrlib's delay_us() function with low values (e.g. 1 or
|
||||||
|
// 2 microseconds) gives delays longer than desired.
|
||||||
|
//delay_us(us);
|
||||||
|
#if F_CPU >= 20000000L
|
||||||
|
// for the 20 MHz clock on rare Arduino boards
|
||||||
|
|
||||||
|
// for a one-microsecond delay, simply wait 2 cycle and return. The overhead
|
||||||
|
// of the function call yields a delay of exactly a one microsecond.
|
||||||
|
__asm__ __volatile__ (
|
||||||
|
"nop" "\n\t"
|
||||||
|
"nop"); //just waiting 2 cycle
|
||||||
|
if (--us == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// the following loop takes a 1/5 of a microsecond (4 cycles)
|
||||||
|
// per iteration, so execute it five times for each microsecond of
|
||||||
|
// delay requested.
|
||||||
|
us = (us<<2) + us; // x5 us
|
||||||
|
|
||||||
|
// account for the time taken in the preceeding commands.
|
||||||
|
us -= 2;
|
||||||
|
|
||||||
|
#elif F_CPU >= 16000000L
|
||||||
|
// for the 16 MHz clock on most Arduino boards
|
||||||
|
|
||||||
|
// for a one-microsecond delay, simply return. the overhead
|
||||||
|
// of the function call yields a delay of approximately 1 1/8 us.
|
||||||
|
if (--us == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// the following loop takes a quarter of a microsecond (4 cycles)
|
||||||
|
// per iteration, so execute it four times for each microsecond of
|
||||||
|
// delay requested.
|
||||||
|
us <<= 2;
|
||||||
|
|
||||||
|
// account for the time taken in the preceeding commands.
|
||||||
|
us -= 2;
|
||||||
|
#else
|
||||||
|
// for the 8 MHz internal clock on the ATmega168
|
||||||
|
|
||||||
|
// for a one- or two-microsecond delay, simply return. the overhead of
|
||||||
|
// the function calls takes more than two microseconds. can't just
|
||||||
|
// subtract two, since us is unsigned; we'd overflow.
|
||||||
|
if (--us == 0)
|
||||||
|
return;
|
||||||
|
if (--us == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// the following loop takes half of a microsecond (4 cycles)
|
||||||
|
// per iteration, so execute it twice for each microsecond of
|
||||||
|
// delay requested.
|
||||||
|
us <<= 1;
|
||||||
|
|
||||||
|
// partially compensate for the time taken by the preceeding commands.
|
||||||
|
// we can't subtract any more than this or we'd overflow w/ small delays.
|
||||||
|
us--;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// busy wait
|
||||||
|
__asm__ __volatile__ (
|
||||||
|
"1: sbiw %0,1" "\n\t" // 2 cycles
|
||||||
|
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void init()
|
||||||
|
{
|
||||||
|
// this needs to be called before setup() or some functions won't
|
||||||
|
// work there
|
||||||
|
sei();
|
||||||
|
|
||||||
|
// on the ATmega168, timer 0 is also used for fast hardware pwm
|
||||||
|
// (using phase-correct PWM would mean that timer 0 overflowed half as often
|
||||||
|
// resulting in different millis() behavior on the ATmega8 and ATmega168)
|
||||||
|
#if defined(TCCR0A) && defined(WGM01)
|
||||||
|
sbi(TCCR0A, WGM01);
|
||||||
|
sbi(TCCR0A, WGM00);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// set timer 0 prescale factor to 64
|
||||||
|
#if defined(__AVR_ATmega128__)
|
||||||
|
// CPU specific: different values for the ATmega128
|
||||||
|
sbi(TCCR0, CS02);
|
||||||
|
#elif defined(TCCR0) && defined(CS01) && defined(CS00)
|
||||||
|
// this combination is for the standard atmega8
|
||||||
|
sbi(TCCR0, CS01);
|
||||||
|
sbi(TCCR0, CS00);
|
||||||
|
#elif defined(TCCR0B) && defined(CS01) && defined(CS00)
|
||||||
|
// this combination is for the standard 168/328/1280/2560
|
||||||
|
sbi(TCCR0B, CS01);
|
||||||
|
sbi(TCCR0B, CS00);
|
||||||
|
#elif defined(TCCR0A) && defined(CS01) && defined(CS00)
|
||||||
|
// this combination is for the __AVR_ATmega645__ series
|
||||||
|
sbi(TCCR0A, CS01);
|
||||||
|
sbi(TCCR0A, CS00);
|
||||||
|
#else
|
||||||
|
#error Timer 0 prescale factor 64 not set correctly
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// enable timer 0 overflow interrupt
|
||||||
|
#if defined(TIMSK) && defined(TOIE0)
|
||||||
|
sbi(TIMSK, TOIE0);
|
||||||
|
#elif defined(TIMSK0) && defined(TOIE0)
|
||||||
|
sbi(TIMSK0, TOIE0);
|
||||||
|
#else
|
||||||
|
#error Timer 0 overflow interrupt not set correctly
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// timers 1 and 2 are used for phase-correct hardware pwm
|
||||||
|
// this is better for motors as it ensures an even waveform
|
||||||
|
// note, however, that fast pwm mode can achieve a frequency of up
|
||||||
|
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
|
||||||
|
|
||||||
|
#if defined(TCCR1B) && defined(CS11) && defined(CS10)
|
||||||
|
TCCR1B = 0;
|
||||||
|
|
||||||
|
// set timer 1 prescale factor to 64
|
||||||
|
sbi(TCCR1B, CS11);
|
||||||
|
#if F_CPU >= 8000000L
|
||||||
|
sbi(TCCR1B, CS10);
|
||||||
|
#endif
|
||||||
|
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
|
||||||
|
sbi(TCCR1, CS11);
|
||||||
|
#if F_CPU >= 8000000L
|
||||||
|
sbi(TCCR1, CS10);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
// put timer 1 in 8-bit phase correct pwm mode
|
||||||
|
#if defined(TCCR1A) && defined(WGM10)
|
||||||
|
sbi(TCCR1A, WGM10);
|
||||||
|
#elif defined(TCCR1)
|
||||||
|
#warning this needs to be finished
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// set timer 2 prescale factor to 64
|
||||||
|
#if defined(TCCR2) && defined(CS22)
|
||||||
|
sbi(TCCR2, CS22);
|
||||||
|
#elif defined(TCCR2B) && defined(CS22)
|
||||||
|
sbi(TCCR2B, CS22);
|
||||||
|
#else
|
||||||
|
#warning Timer 2 not finished (may not be present on this CPU)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// configure timer 2 for phase correct pwm (8-bit)
|
||||||
|
#if defined(TCCR2) && defined(WGM20)
|
||||||
|
sbi(TCCR2, WGM20);
|
||||||
|
#elif defined(TCCR2A) && defined(WGM20)
|
||||||
|
sbi(TCCR2A, WGM20);
|
||||||
|
#else
|
||||||
|
#warning Timer 2 not finished (may not be present on this CPU)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
|
||||||
|
sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
|
||||||
|
sbi(TCCR3B, CS30);
|
||||||
|
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A) && defined(TCCR4B) && defined(TCCR4D) /* beginning of timer4 block for 32U4 and similar */
|
||||||
|
sbi(TCCR4B, CS42); // set timer4 prescale factor to 64
|
||||||
|
sbi(TCCR4B, CS41);
|
||||||
|
sbi(TCCR4B, CS40);
|
||||||
|
sbi(TCCR4D, WGM40); // put timer 4 in phase- and frequency-correct PWM mode
|
||||||
|
sbi(TCCR4A, PWM4A); // enable PWM mode for comparator OCR4A
|
||||||
|
sbi(TCCR4C, PWM4D); // enable PWM mode for comparator OCR4D
|
||||||
|
#else /* beginning of timer4 block for ATMEGA1280 and ATMEGA2560 */
|
||||||
|
#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
|
||||||
|
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
|
||||||
|
sbi(TCCR4B, CS40);
|
||||||
|
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
|
||||||
|
#endif
|
||||||
|
#endif /* end timer4 block for ATMEGA1280/2560 and similar */
|
||||||
|
|
||||||
|
#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
|
||||||
|
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
|
||||||
|
sbi(TCCR5B, CS50);
|
||||||
|
sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ADCSRA)
|
||||||
|
// set a2d prescale factor to 128
|
||||||
|
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
|
||||||
|
// XXX: this will not work properly for other clock speeds, and
|
||||||
|
// this code should use F_CPU to determine the prescale factor.
|
||||||
|
sbi(ADCSRA, ADPS2);
|
||||||
|
sbi(ADCSRA, ADPS1);
|
||||||
|
sbi(ADCSRA, ADPS0);
|
||||||
|
|
||||||
|
// enable a2d conversions
|
||||||
|
sbi(ADCSRA, ADEN);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// the bootloader connects pins 0 and 1 to the USART; disconnect them
|
||||||
|
// here so they can be used as normal digital i/o; they will be
|
||||||
|
// reconnected in Serial.begin()
|
||||||
|
#if defined(UCSRB)
|
||||||
|
UCSRB = 0;
|
||||||
|
#elif defined(UCSR0B)
|
||||||
|
UCSR0B = 0;
|
||||||
|
#endif
|
||||||
|
}
|
@ -0,0 +1,282 @@
|
|||||||
|
/*
|
||||||
|
wiring_analog.c - analog input and output
|
||||||
|
Part of Arduino - http://www.arduino.cc/
|
||||||
|
|
||||||
|
Copyright (c) 2005-2006 David A. Mellis
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General
|
||||||
|
Public License along with this library; if not, write to the
|
||||||
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Modified 28 September 2010 by Mark Sproul
|
||||||
|
|
||||||
|
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "wiring_private.h"
|
||||||
|
#include "pins_arduino.h"
|
||||||
|
|
||||||
|
uint8_t analog_reference = DEFAULT;
|
||||||
|
|
||||||
|
void analogReference(uint8_t mode)
|
||||||
|
{
|
||||||
|
// can't actually set the register here because the default setting
|
||||||
|
// will connect AVCC and the AREF pin, which would cause a short if
|
||||||
|
// there's something connected to AREF.
|
||||||
|
analog_reference = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
int analogRead(uint8_t pin)
|
||||||
|
{
|
||||||
|
uint8_t low, high;
|
||||||
|
|
||||||
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
||||||
|
if (pin >= 54) pin -= 54; // allow for channel or pin numbers
|
||||||
|
#elif defined(__AVR_ATmega32U4__)
|
||||||
|
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
|
||||||
|
#elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__)
|
||||||
|
if (pin >= 24) pin -= 24; // allow for channel or pin numbers
|
||||||
|
#else
|
||||||
|
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__AVR_ATmega32U4__)
|
||||||
|
pin = analogPinToChannel(pin);
|
||||||
|
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
|
||||||
|
#elif defined(ADCSRB) && defined(MUX5)
|
||||||
|
// the MUX5 bit of ADCSRB selects whether we're reading from channels
|
||||||
|
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
|
||||||
|
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// set the analog reference (high two bits of ADMUX) and select the
|
||||||
|
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
|
||||||
|
// to 0 (the default).
|
||||||
|
#if defined(ADMUX)
|
||||||
|
ADMUX = (analog_reference << 6) | (pin & 0x07);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// without a delay, we seem to read from the wrong channel
|
||||||
|
//delay(1);
|
||||||
|
|
||||||
|
#if defined(ADCSRA) && defined(ADCL)
|
||||||
|
// start the conversion
|
||||||
|
sbi(ADCSRA, ADSC);
|
||||||
|
|
||||||
|
// ADSC is cleared when the conversion finishes
|
||||||
|
while (bit_is_set(ADCSRA, ADSC));
|
||||||
|
|
||||||
|
// we have to read ADCL first; doing so locks both ADCL
|
||||||
|
// and ADCH until ADCH is read. reading ADCL second would
|
||||||
|
// cause the results of each conversion to be discarded,
|
||||||
|
// as ADCL and ADCH would be locked when it completed.
|
||||||
|
low = ADCL;
|
||||||
|
high = ADCH;
|
||||||
|
#else
|
||||||
|
// we dont have an ADC, return 0
|
||||||
|
low = 0;
|
||||||
|
high = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// combine the two bytes
|
||||||
|
return (high << 8) | low;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Right now, PWM output only works on the pins with
|
||||||
|
// hardware support. These are defined in the appropriate
|
||||||
|
// pins_*.c file. For the rest of the pins, we default
|
||||||
|
// to digital output.
|
||||||
|
void analogWrite(uint8_t pin, int val)
|
||||||
|
{
|
||||||
|
// We need to make sure the PWM output is enabled for those pins
|
||||||
|
// that support it, as we turn it off when digitally reading or
|
||||||
|
// writing with them. Also, make sure the pin is in output mode
|
||||||
|
// for consistenty with Wiring, which doesn't require a pinMode
|
||||||
|
// call for the analog output pins.
|
||||||
|
pinMode(pin, OUTPUT);
|
||||||
|
if (val == 0)
|
||||||
|
{
|
||||||
|
digitalWrite(pin, LOW);
|
||||||
|
}
|
||||||
|
else if (val == 255)
|
||||||
|
{
|
||||||
|
digitalWrite(pin, HIGH);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
switch(digitalPinToTimer(pin))
|
||||||
|
{
|
||||||
|
// XXX fix needed for atmega8
|
||||||
|
#if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
|
||||||
|
case TIMER0A:
|
||||||
|
// connect pwm to pin on timer 0
|
||||||
|
sbi(TCCR0, COM00);
|
||||||
|
OCR0 = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR0A) && defined(COM0A1)
|
||||||
|
case TIMER0A:
|
||||||
|
// connect pwm to pin on timer 0, channel A
|
||||||
|
sbi(TCCR0A, COM0A1);
|
||||||
|
OCR0A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR0A) && defined(COM0B1)
|
||||||
|
case TIMER0B:
|
||||||
|
// connect pwm to pin on timer 0, channel B
|
||||||
|
sbi(TCCR0A, COM0B1);
|
||||||
|
OCR0B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR1A) && defined(COM1A1)
|
||||||
|
case TIMER1A:
|
||||||
|
// connect pwm to pin on timer 1, channel A
|
||||||
|
sbi(TCCR1A, COM1A1);
|
||||||
|
OCR1A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR1A) && defined(COM1B1)
|
||||||
|
case TIMER1B:
|
||||||
|
// connect pwm to pin on timer 1, channel B
|
||||||
|
sbi(TCCR1A, COM1B1);
|
||||||
|
OCR1B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR2) && defined(COM21)
|
||||||
|
case TIMER2:
|
||||||
|
// connect pwm to pin on timer 2
|
||||||
|
sbi(TCCR2, COM21);
|
||||||
|
OCR2 = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR2A) && defined(COM2A1)
|
||||||
|
case TIMER2A:
|
||||||
|
// connect pwm to pin on timer 2, channel A
|
||||||
|
sbi(TCCR2A, COM2A1);
|
||||||
|
OCR2A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR2A) && defined(COM2B1)
|
||||||
|
case TIMER2B:
|
||||||
|
// connect pwm to pin on timer 2, channel B
|
||||||
|
sbi(TCCR2A, COM2B1);
|
||||||
|
OCR2B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3A) && defined(COM3A1)
|
||||||
|
case TIMER3A:
|
||||||
|
// connect pwm to pin on timer 3, channel A
|
||||||
|
sbi(TCCR3A, COM3A1);
|
||||||
|
OCR3A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3A) && defined(COM3B1)
|
||||||
|
case TIMER3B:
|
||||||
|
// connect pwm to pin on timer 3, channel B
|
||||||
|
sbi(TCCR3A, COM3B1);
|
||||||
|
OCR3B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3A) && defined(COM3C1)
|
||||||
|
case TIMER3C:
|
||||||
|
// connect pwm to pin on timer 3, channel C
|
||||||
|
sbi(TCCR3A, COM3C1);
|
||||||
|
OCR3C = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A)
|
||||||
|
case TIMER4A:
|
||||||
|
//connect pwm to pin on timer 4, channel A
|
||||||
|
sbi(TCCR4A, COM4A1);
|
||||||
|
#if defined(COM4A0) // only used on 32U4
|
||||||
|
cbi(TCCR4A, COM4A0);
|
||||||
|
#endif
|
||||||
|
OCR4A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A) && defined(COM4B1)
|
||||||
|
case TIMER4B:
|
||||||
|
// connect pwm to pin on timer 4, channel B
|
||||||
|
sbi(TCCR4A, COM4B1);
|
||||||
|
OCR4B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A) && defined(COM4C1)
|
||||||
|
case TIMER4C:
|
||||||
|
// connect pwm to pin on timer 4, channel C
|
||||||
|
sbi(TCCR4A, COM4C1);
|
||||||
|
OCR4C = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4C) && defined(COM4D1)
|
||||||
|
case TIMER4D:
|
||||||
|
// connect pwm to pin on timer 4, channel D
|
||||||
|
sbi(TCCR4C, COM4D1);
|
||||||
|
#if defined(COM4D0) // only used on 32U4
|
||||||
|
cbi(TCCR4C, COM4D0);
|
||||||
|
#endif
|
||||||
|
OCR4D = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(TCCR5A) && defined(COM5A1)
|
||||||
|
case TIMER5A:
|
||||||
|
// connect pwm to pin on timer 5, channel A
|
||||||
|
sbi(TCCR5A, COM5A1);
|
||||||
|
OCR5A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR5A) && defined(COM5B1)
|
||||||
|
case TIMER5B:
|
||||||
|
// connect pwm to pin on timer 5, channel B
|
||||||
|
sbi(TCCR5A, COM5B1);
|
||||||
|
OCR5B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR5A) && defined(COM5C1)
|
||||||
|
case TIMER5C:
|
||||||
|
// connect pwm to pin on timer 5, channel C
|
||||||
|
sbi(TCCR5A, COM5C1);
|
||||||
|
OCR5C = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
case NOT_ON_TIMER:
|
||||||
|
default:
|
||||||
|
if (val < 128) {
|
||||||
|
digitalWrite(pin, LOW);
|
||||||
|
} else {
|
||||||
|
digitalWrite(pin, HIGH);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,178 @@
|
|||||||
|
/*
|
||||||
|
wiring_digital.c - digital input and output functions
|
||||||
|
Part of Arduino - http://www.arduino.cc/
|
||||||
|
|
||||||
|
Copyright (c) 2005-2006 David A. Mellis
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General
|
||||||
|
Public License along with this library; if not, write to the
|
||||||
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Modified 28 September 2010 by Mark Sproul
|
||||||
|
|
||||||
|
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define ARDUINO_MAIN
|
||||||
|
#include "wiring_private.h"
|
||||||
|
#include "pins_arduino.h"
|
||||||
|
|
||||||
|
void pinMode(uint8_t pin, uint8_t mode)
|
||||||
|
{
|
||||||
|
uint8_t bit = digitalPinToBitMask(pin);
|
||||||
|
uint8_t port = digitalPinToPort(pin);
|
||||||
|
volatile uint8_t *reg, *out;
|
||||||
|
|
||||||
|
if (port == NOT_A_PIN) return;
|
||||||
|
|
||||||
|
// JWS: can I let the optimizer do this?
|
||||||
|
reg = portModeRegister(port);
|
||||||
|
out = portOutputRegister(port);
|
||||||
|
|
||||||
|
if (mode == INPUT) {
|
||||||
|
uint8_t oldSREG = SREG;
|
||||||
|
cli();
|
||||||
|
*reg &= ~bit;
|
||||||
|
*out &= ~bit;
|
||||||
|
SREG = oldSREG;
|
||||||
|
} else if (mode == INPUT_PULLUP) {
|
||||||
|
uint8_t oldSREG = SREG;
|
||||||
|
cli();
|
||||||
|
*reg &= ~bit;
|
||||||
|
*out |= bit;
|
||||||
|
SREG = oldSREG;
|
||||||
|
} else {
|
||||||
|
uint8_t oldSREG = SREG;
|
||||||
|
cli();
|
||||||
|
*reg |= bit;
|
||||||
|
SREG = oldSREG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forcing this inline keeps the callers from having to push their own stuff
|
||||||
|
// on the stack. It is a good performance win and only takes 1 more byte per
|
||||||
|
// user than calling. (It will take more bytes on the 168.)
|
||||||
|
//
|
||||||
|
// But shouldn't this be moved into pinMode? Seems silly to check and do on
|
||||||
|
// each digitalread or write.
|
||||||
|
//
|
||||||
|
// Mark Sproul:
|
||||||
|
// - Removed inline. Save 170 bytes on atmega1280
|
||||||
|
// - changed to a switch statment; added 32 bytes but much easier to read and maintain.
|
||||||
|
// - Added more #ifdefs, now compiles for atmega645
|
||||||
|
//
|
||||||
|
//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
|
||||||
|
//static inline void turnOffPWM(uint8_t timer)
|
||||||
|
static void turnOffPWM(uint8_t timer)
|
||||||
|
{
|
||||||
|
switch (timer)
|
||||||
|
{
|
||||||
|
#if defined(TCCR1A) && defined(COM1A1)
|
||||||
|
case TIMER1A: cbi(TCCR1A, COM1A1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR1A) && defined(COM1B1)
|
||||||
|
case TIMER1B: cbi(TCCR1A, COM1B1); break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR2) && defined(COM21)
|
||||||
|
case TIMER2: cbi(TCCR2, COM21); break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR0A) && defined(COM0A1)
|
||||||
|
case TIMER0A: cbi(TCCR0A, COM0A1); break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TIMER0B) && defined(COM0B1)
|
||||||
|
case TIMER0B: cbi(TCCR0A, COM0B1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR2A) && defined(COM2A1)
|
||||||
|
case TIMER2A: cbi(TCCR2A, COM2A1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR2A) && defined(COM2B1)
|
||||||
|
case TIMER2B: cbi(TCCR2A, COM2B1); break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3A) && defined(COM3A1)
|
||||||
|
case TIMER3A: cbi(TCCR3A, COM3A1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR3A) && defined(COM3B1)
|
||||||
|
case TIMER3B: cbi(TCCR3A, COM3B1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR3A) && defined(COM3C1)
|
||||||
|
case TIMER3C: cbi(TCCR3A, COM3C1); break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A) && defined(COM4A1)
|
||||||
|
case TIMER4A: cbi(TCCR4A, COM4A1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR4A) && defined(COM4B1)
|
||||||
|
case TIMER4B: cbi(TCCR4A, COM4B1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR4A) && defined(COM4C1)
|
||||||
|
case TIMER4C: cbi(TCCR4A, COM4C1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR4C) && defined(COM4D1)
|
||||||
|
case TIMER4D: cbi(TCCR4C, COM4D1); break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR5A)
|
||||||
|
case TIMER5A: cbi(TCCR5A, COM5A1); break;
|
||||||
|
case TIMER5B: cbi(TCCR5A, COM5B1); break;
|
||||||
|
case TIMER5C: cbi(TCCR5A, COM5C1); break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void digitalWrite(uint8_t pin, uint8_t val)
|
||||||
|
{
|
||||||
|
uint8_t timer = digitalPinToTimer(pin);
|
||||||
|
uint8_t bit = digitalPinToBitMask(pin);
|
||||||
|
uint8_t port = digitalPinToPort(pin);
|
||||||
|
volatile uint8_t *out;
|
||||||
|
|
||||||
|
if (port == NOT_A_PIN) return;
|
||||||
|
|
||||||
|
// If the pin that support PWM output, we need to turn it off
|
||||||
|
// before doing a digital write.
|
||||||
|
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
|
||||||
|
|
||||||
|
out = portOutputRegister(port);
|
||||||
|
|
||||||
|
uint8_t oldSREG = SREG;
|
||||||
|
cli();
|
||||||
|
|
||||||
|
if (val == LOW) {
|
||||||
|
*out &= ~bit;
|
||||||
|
} else {
|
||||||
|
*out |= bit;
|
||||||
|
}
|
||||||
|
|
||||||
|
SREG = oldSREG;
|
||||||
|
}
|
||||||
|
|
||||||
|
int digitalRead(uint8_t pin)
|
||||||
|
{
|
||||||
|
uint8_t timer = digitalPinToTimer(pin);
|
||||||
|
uint8_t bit = digitalPinToBitMask(pin);
|
||||||
|
uint8_t port = digitalPinToPort(pin);
|
||||||
|
|
||||||
|
if (port == NOT_A_PIN) return LOW;
|
||||||
|
|
||||||
|
// If the pin that support PWM output, we need to turn it off
|
||||||
|
// before getting a digital reading.
|
||||||
|
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
|
||||||
|
|
||||||
|
if (*portInputRegister(port) & bit) return HIGH;
|
||||||
|
return LOW;
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
wiring_private.h - Internal header file.
|
||||||
|
Part of Arduino - http://www.arduino.cc/
|
||||||
|
|
||||||
|
Copyright (c) 2005-2006 David A. Mellis
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General
|
||||||
|
Public License along with this library; if not, write to the
|
||||||
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
$Id: wiring.h 239 2007-01-12 17:58:39Z mellis $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef WiringPrivate_h
|
||||||
|
#define WiringPrivate_h
|
||||||
|
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef cbi
|
||||||
|
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
||||||
|
#endif
|
||||||
|
#ifndef sbi
|
||||||
|
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define EXTERNAL_INT_0 0
|
||||||
|
#define EXTERNAL_INT_1 1
|
||||||
|
#define EXTERNAL_INT_2 2
|
||||||
|
#define EXTERNAL_INT_3 3
|
||||||
|
#define EXTERNAL_INT_4 4
|
||||||
|
#define EXTERNAL_INT_5 5
|
||||||
|
#define EXTERNAL_INT_6 6
|
||||||
|
#define EXTERNAL_INT_7 7
|
||||||
|
|
||||||
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
||||||
|
#define EXTERNAL_NUM_INTERRUPTS 8
|
||||||
|
#elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__)
|
||||||
|
#define EXTERNAL_NUM_INTERRUPTS 3
|
||||||
|
#elif defined(__AVR_ATmega32U4__)
|
||||||
|
#define EXTERNAL_NUM_INTERRUPTS 4
|
||||||
|
#else
|
||||||
|
#define EXTERNAL_NUM_INTERRUPTS 2
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef void (*voidFuncPtr)(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
wiring_pulse.c - pulseIn() function
|
||||||
|
Part of Arduino - http://www.arduino.cc/
|
||||||
|
|
||||||
|
Copyright (c) 2005-2006 David A. Mellis
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General
|
||||||
|
Public License along with this library; if not, write to the
|
||||||
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "wiring_private.h"
|
||||||
|
#include "pins_arduino.h"
|
||||||
|
|
||||||
|
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
|
||||||
|
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
|
||||||
|
* to 3 minutes in length, but must be called at least a few dozen microseconds
|
||||||
|
* before the start of the pulse. */
|
||||||
|
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
|
||||||
|
{
|
||||||
|
// cache the port and bit of the pin in order to speed up the
|
||||||
|
// pulse width measuring loop and achieve finer resolution. calling
|
||||||
|
// digitalRead() instead yields much coarser resolution.
|
||||||
|
uint8_t bit = digitalPinToBitMask(pin);
|
||||||
|
uint8_t port = digitalPinToPort(pin);
|
||||||
|
uint8_t stateMask = (state ? bit : 0);
|
||||||
|
unsigned long width = 0; // keep initialization out of time critical area
|
||||||
|
|
||||||
|
// convert the timeout from microseconds to a number of times through
|
||||||
|
// the initial loop; it takes 16 clock cycles per iteration.
|
||||||
|
unsigned long numloops = 0;
|
||||||
|
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
|
||||||
|
|
||||||
|
// wait for any previous pulse to end
|
||||||
|
while ((*portInputRegister(port) & bit) == stateMask)
|
||||||
|
if (numloops++ == maxloops)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// wait for the pulse to start
|
||||||
|
while ((*portInputRegister(port) & bit) != stateMask)
|
||||||
|
if (numloops++ == maxloops)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// wait for the pulse to stop
|
||||||
|
while ((*portInputRegister(port) & bit) == stateMask) {
|
||||||
|
if (numloops++ == maxloops)
|
||||||
|
return 0;
|
||||||
|
width++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert the reading to microseconds. The loop has been determined
|
||||||
|
// to be 20 clock cycles long and have about 16 clocks between the edge
|
||||||
|
// and the start of the loop. There will be some error introduced by
|
||||||
|
// the interrupt handlers.
|
||||||
|
return clockCyclesToMicroseconds(width * 21 + 16);
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
wiring_shift.c - shiftOut() function
|
||||||
|
Part of Arduino - http://www.arduino.cc/
|
||||||
|
|
||||||
|
Copyright (c) 2005-2006 David A. Mellis
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General
|
||||||
|
Public License along with this library; if not, write to the
|
||||||
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "wiring_private.h"
|
||||||
|
|
||||||
|
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
|
||||||
|
uint8_t value = 0;
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < 8; ++i) {
|
||||||
|
digitalWrite(clockPin, HIGH);
|
||||||
|
if (bitOrder == LSBFIRST)
|
||||||
|
value |= digitalRead(dataPin) << i;
|
||||||
|
else
|
||||||
|
value |= digitalRead(dataPin) << (7 - i);
|
||||||
|
digitalWrite(clockPin, LOW);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < 8; i++) {
|
||||||
|
if (bitOrder == LSBFIRST)
|
||||||
|
digitalWrite(dataPin, !!(val & (1 << i)));
|
||||||
|
else
|
||||||
|
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
|
||||||
|
|
||||||
|
digitalWrite(clockPin, HIGH);
|
||||||
|
digitalWrite(clockPin, LOW);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,411 @@
|
|||||||
|
/*
|
||||||
|
pins_arduino.h - Pin definition functions for Arduino
|
||||||
|
Part of Arduino - http://www.arduino.cc/
|
||||||
|
|
||||||
|
Copyright (c) 2007 David A. Mellis
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General
|
||||||
|
Public License along with this library; if not, write to the
|
||||||
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
$Id: wiring.h 249 2007-02-03 16:52:51Z mellis $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef Pins_Arduino_h
|
||||||
|
#define Pins_Arduino_h
|
||||||
|
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
|
||||||
|
#define NUM_DIGITAL_PINS 82
|
||||||
|
#define NUM_ANALOG_INPUTS 16
|
||||||
|
#define analogInputToDigitalPin(p) ((p < 16) ? (p) + 54 : -1)
|
||||||
|
#define digitalPinHasPWM(p) (((p) >= 2 && (p) <= 13) || ((p) >= 44 && (p)<= 46))
|
||||||
|
|
||||||
|
static const uint8_t SS = 53;
|
||||||
|
static const uint8_t MOSI = 51;
|
||||||
|
static const uint8_t MISO = 50;
|
||||||
|
static const uint8_t SCK = 52;
|
||||||
|
|
||||||
|
static const uint8_t SDA = 20;
|
||||||
|
static const uint8_t SCL = 21;
|
||||||
|
static const uint8_t LED_BUILTIN = 13;
|
||||||
|
|
||||||
|
static const uint8_t A0 = 54;
|
||||||
|
static const uint8_t A1 = 55;
|
||||||
|
static const uint8_t A2 = 56;
|
||||||
|
static const uint8_t A3 = 57;
|
||||||
|
static const uint8_t A4 = 58;
|
||||||
|
static const uint8_t A5 = 59;
|
||||||
|
static const uint8_t A6 = 60;
|
||||||
|
static const uint8_t A7 = 61;
|
||||||
|
static const uint8_t A8 = 62;
|
||||||
|
static const uint8_t A9 = 63;
|
||||||
|
static const uint8_t A10 = 64;
|
||||||
|
static const uint8_t A11 = 65;
|
||||||
|
static const uint8_t A12 = 66;
|
||||||
|
static const uint8_t A13 = 67;
|
||||||
|
static const uint8_t A14 = 68;
|
||||||
|
static const uint8_t A15 = 69;
|
||||||
|
|
||||||
|
// A majority of the pins are NOT PCINTs, SO BE WARNED (i.e. you cannot use them as receive pins)
|
||||||
|
// Only pins available for RECEIVE (TRANSMIT can be on any pin):
|
||||||
|
// (I've deliberately left out pin mapping to the Hardware USARTs - seems senseless to me)
|
||||||
|
// Pins: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
|
||||||
|
|
||||||
|
#define digitalPinToPCICR(p) ( (((p) >= 10) && ((p) <= 13)) || \
|
||||||
|
(((p) >= 50) && ((p) <= 53)) || \
|
||||||
|
(((p) >= 62) && ((p) <= 69)) ? (&PCICR) : ((uint8_t *)0) )
|
||||||
|
|
||||||
|
#define digitalPinToPCICRbit(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? 0 : \
|
||||||
|
( (((p) >= 62) && ((p) <= 69)) ? 2 : \
|
||||||
|
0 ) )
|
||||||
|
|
||||||
|
#define digitalPinToPCMSK(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? (&PCMSK0) : \
|
||||||
|
( (((p) >= 62) && ((p) <= 69)) ? (&PCMSK2) : \
|
||||||
|
((uint8_t *)0) ) )
|
||||||
|
|
||||||
|
#define digitalPinToPCMSKbit(p) ( (((p) >= 10) && ((p) <= 13)) ? ((p) - 6) : \
|
||||||
|
( ((p) == 50) ? 3 : \
|
||||||
|
( ((p) == 51) ? 2 : \
|
||||||
|
( ((p) == 52) ? 1 : \
|
||||||
|
( ((p) == 53) ? 0 : \
|
||||||
|
( (((p) >= 62) && ((p) <= 69)) ? ((p) - 62) : \
|
||||||
|
0 ) ) ) ) ) )
|
||||||
|
|
||||||
|
#ifdef ARDUINO_MAIN
|
||||||
|
|
||||||
|
const uint16_t PROGMEM port_to_mode_PGM[] = {
|
||||||
|
NOT_A_PORT,
|
||||||
|
(uint16_t) &DDRA,
|
||||||
|
(uint16_t) &DDRB,
|
||||||
|
(uint16_t) &DDRC,
|
||||||
|
(uint16_t) &DDRD,
|
||||||
|
(uint16_t) &DDRE,
|
||||||
|
(uint16_t) &DDRF,
|
||||||
|
(uint16_t) &DDRG,
|
||||||
|
(uint16_t) &DDRH,
|
||||||
|
NOT_A_PORT,
|
||||||
|
(uint16_t) &DDRJ,
|
||||||
|
(uint16_t) &DDRK,
|
||||||
|
(uint16_t) &DDRL,
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint16_t PROGMEM port_to_output_PGM[] = {
|
||||||
|
NOT_A_PORT,
|
||||||
|
(uint16_t) &PORTA,
|
||||||
|
(uint16_t) &PORTB,
|
||||||
|
(uint16_t) &PORTC,
|
||||||
|
(uint16_t) &PORTD,
|
||||||
|
(uint16_t) &PORTE,
|
||||||
|
(uint16_t) &PORTF,
|
||||||
|
(uint16_t) &PORTG,
|
||||||
|
(uint16_t) &PORTH,
|
||||||
|
NOT_A_PORT,
|
||||||
|
(uint16_t) &PORTJ,
|
||||||
|
(uint16_t) &PORTK,
|
||||||
|
(uint16_t) &PORTL,
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint16_t PROGMEM port_to_input_PGM[] = {
|
||||||
|
NOT_A_PIN,
|
||||||
|
(uint16_t) &PINA,
|
||||||
|
(uint16_t) &PINB,
|
||||||
|
(uint16_t) &PINC,
|
||||||
|
(uint16_t) &PIND,
|
||||||
|
(uint16_t) &PINE,
|
||||||
|
(uint16_t) &PINF,
|
||||||
|
(uint16_t) &PING,
|
||||||
|
(uint16_t) &PINH,
|
||||||
|
NOT_A_PIN,
|
||||||
|
(uint16_t) &PINJ,
|
||||||
|
(uint16_t) &PINK,
|
||||||
|
(uint16_t) &PINL,
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
|
||||||
|
// PORTLIST
|
||||||
|
// -------------------------------------------
|
||||||
|
PE , // PE 0 ** 0 ** USART0_RX
|
||||||
|
PE , // PE 1 ** 1 ** USART0_TX
|
||||||
|
PE , // PE 4 ** 2 ** PWM2
|
||||||
|
PE , // PE 5 ** 3 ** PWM3
|
||||||
|
PG , // PG 5 ** 4 ** PWM4
|
||||||
|
PE , // PE 3 ** 5 ** PWM5
|
||||||
|
PH , // PH 3 ** 6 ** PWM6
|
||||||
|
PH , // PH 4 ** 7 ** PWM7
|
||||||
|
PH , // PH 5 ** 8 ** PWM8
|
||||||
|
PH , // PH 6 ** 9 ** PWM9
|
||||||
|
PB , // PB 4 ** 10 ** PWM10
|
||||||
|
PB , // PB 5 ** 11 ** PWM11
|
||||||
|
PB , // PB 6 ** 12 ** PWM12
|
||||||
|
PB , // PB 7 ** 13 ** PWM13
|
||||||
|
PJ , // PJ 1 ** 14 ** USART3_TX
|
||||||
|
PJ , // PJ 0 ** 15 ** USART3_RX
|
||||||
|
PH , // PH 1 ** 16 ** USART2_TX
|
||||||
|
PH , // PH 0 ** 17 ** USART2_RX
|
||||||
|
PD , // PD 3 ** 18 ** USART1_TX
|
||||||
|
PD , // PD 2 ** 19 ** USART1_RX
|
||||||
|
PD , // PD 1 ** 20 ** I2C_SDA
|
||||||
|
PD , // PD 0 ** 21 ** I2C_SCL
|
||||||
|
PA , // PA 0 ** 22 ** D22
|
||||||
|
PA , // PA 1 ** 23 ** D23
|
||||||
|
PA , // PA 2 ** 24 ** D24
|
||||||
|
PA , // PA 3 ** 25 ** D25
|
||||||
|
PA , // PA 4 ** 26 ** D26
|
||||||
|
PA , // PA 5 ** 27 ** D27
|
||||||
|
PA , // PA 6 ** 28 ** D28
|
||||||
|
PA , // PA 7 ** 29 ** D29
|
||||||
|
PC , // PC 7 ** 30 ** D30
|
||||||
|
PC , // PC 6 ** 31 ** D31
|
||||||
|
PC , // PC 5 ** 32 ** D32
|
||||||
|
PC , // PC 4 ** 33 ** D33
|
||||||
|
PC , // PC 3 ** 34 ** D34
|
||||||
|
PC , // PC 2 ** 35 ** D35
|
||||||
|
PC , // PC 1 ** 36 ** D36
|
||||||
|
PC , // PC 0 ** 37 ** D37
|
||||||
|
PD , // PD 7 ** 38 ** D38
|
||||||
|
PG , // PG 2 ** 39 ** D39
|
||||||
|
PG , // PG 1 ** 40 ** D40
|
||||||
|
PG , // PG 0 ** 41 ** D41
|
||||||
|
PL , // PL 7 ** 42 ** D42
|
||||||
|
PL , // PL 6 ** 43 ** D43
|
||||||
|
PL , // PL 5 ** 44 ** D44
|
||||||
|
PL , // PL 4 ** 45 ** D45
|
||||||
|
PL , // PL 3 ** 46 ** D46
|
||||||
|
PL , // PL 2 ** 47 ** D47
|
||||||
|
PL , // PL 1 ** 48 ** D48
|
||||||
|
PL , // PL 0 ** 49 ** D49
|
||||||
|
PB , // PB 3 ** 50 ** SPI_MISO
|
||||||
|
PB , // PB 2 ** 51 ** SPI_MOSI
|
||||||
|
PB , // PB 1 ** 52 ** SPI_SCK
|
||||||
|
PB , // PB 0 ** 53 ** SPI_SS
|
||||||
|
PF , // PF 0 ** 54 ** A0
|
||||||
|
PF , // PF 1 ** 55 ** A1
|
||||||
|
PF , // PF 2 ** 56 ** A2
|
||||||
|
PF , // PF 3 ** 57 ** A3
|
||||||
|
PF , // PF 4 ** 58 ** A4
|
||||||
|
PF , // PF 5 ** 59 ** A5
|
||||||
|
PF , // PF 6 ** 60 ** A6
|
||||||
|
PF , // PF 7 ** 61 ** A7
|
||||||
|
PK , // PK 0 ** 62 ** A8
|
||||||
|
PK , // PK 1 ** 63 ** A9
|
||||||
|
PK , // PK 2 ** 64 ** A10
|
||||||
|
PK , // PK 3 ** 65 ** A11
|
||||||
|
PK , // PK 4 ** 66 ** A12
|
||||||
|
PK , // PK 5 ** 67 ** A13
|
||||||
|
PK , // PK 6 ** 68 ** A14
|
||||||
|
PK , // PK 7 ** 69 ** A15
|
||||||
|
PG , // PG 4 ** 70 ** D70
|
||||||
|
PG , // PG 3 ** 71 ** D71
|
||||||
|
PJ , // PJ 2 ** 72 ** D72
|
||||||
|
PJ , // PJ 3 ** 73 ** D73
|
||||||
|
PJ , // PJ 7 ** 74 ** D74
|
||||||
|
PJ , // PJ 4 ** 75 ** D75
|
||||||
|
PJ , // PJ 5 ** 76 ** D76
|
||||||
|
PJ , // PJ 6 ** 77 ** D77
|
||||||
|
PE , // PE 2 ** 78 ** D78
|
||||||
|
PE , // PE 6 ** 79 ** D79
|
||||||
|
PE , // PE 7 ** 80 ** D80
|
||||||
|
PD , // PD 4 ** 81 ** D81
|
||||||
|
PD , // PD 5 ** 82 ** D82
|
||||||
|
PD , // PD 6 ** 83 ** D83
|
||||||
|
PH , // PH 2 ** 84 ** D84
|
||||||
|
PH , // PH 7 ** 85 ** D85
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
|
||||||
|
// PIN IN PORT
|
||||||
|
// -------------------------------------------
|
||||||
|
_BV( 0 ) , // PE 0 ** 0 ** USART0_RX
|
||||||
|
_BV( 1 ) , // PE 1 ** 1 ** USART0_TX
|
||||||
|
_BV( 4 ) , // PE 4 ** 2 ** PWM2
|
||||||
|
_BV( 5 ) , // PE 5 ** 3 ** PWM3
|
||||||
|
_BV( 5 ) , // PG 5 ** 4 ** PWM4
|
||||||
|
_BV( 3 ) , // PE 3 ** 5 ** PWM5
|
||||||
|
_BV( 3 ) , // PH 3 ** 6 ** PWM6
|
||||||
|
_BV( 4 ) , // PH 4 ** 7 ** PWM7
|
||||||
|
_BV( 5 ) , // PH 5 ** 8 ** PWM8
|
||||||
|
_BV( 6 ) , // PH 6 ** 9 ** PWM9
|
||||||
|
_BV( 4 ) , // PB 4 ** 10 ** PWM10
|
||||||
|
_BV( 5 ) , // PB 5 ** 11 ** PWM11
|
||||||
|
_BV( 6 ) , // PB 6 ** 12 ** PWM12
|
||||||
|
_BV( 7 ) , // PB 7 ** 13 ** PWM13
|
||||||
|
_BV( 1 ) , // PJ 1 ** 14 ** USART3_TX
|
||||||
|
_BV( 0 ) , // PJ 0 ** 15 ** USART3_RX
|
||||||
|
_BV( 1 ) , // PH 1 ** 16 ** USART2_TX
|
||||||
|
_BV( 0 ) , // PH 0 ** 17 ** USART2_RX
|
||||||
|
_BV( 3 ) , // PD 3 ** 18 ** USART1_TX
|
||||||
|
_BV( 2 ) , // PD 2 ** 19 ** USART1_RX
|
||||||
|
_BV( 1 ) , // PD 1 ** 20 ** I2C_SDA
|
||||||
|
_BV( 0 ) , // PD 0 ** 21 ** I2C_SCL
|
||||||
|
_BV( 0 ) , // PA 0 ** 22 ** D22
|
||||||
|
_BV( 1 ) , // PA 1 ** 23 ** D23
|
||||||
|
_BV( 2 ) , // PA 2 ** 24 ** D24
|
||||||
|
_BV( 3 ) , // PA 3 ** 25 ** D25
|
||||||
|
_BV( 4 ) , // PA 4 ** 26 ** D26
|
||||||
|
_BV( 5 ) , // PA 5 ** 27 ** D27
|
||||||
|
_BV( 6 ) , // PA 6 ** 28 ** D28
|
||||||
|
_BV( 7 ) , // PA 7 ** 29 ** D29
|
||||||
|
_BV( 7 ) , // PC 7 ** 30 ** D30
|
||||||
|
_BV( 6 ) , // PC 6 ** 31 ** D31
|
||||||
|
_BV( 5 ) , // PC 5 ** 32 ** D32
|
||||||
|
_BV( 4 ) , // PC 4 ** 33 ** D33
|
||||||
|
_BV( 3 ) , // PC 3 ** 34 ** D34
|
||||||
|
_BV( 2 ) , // PC 2 ** 35 ** D35
|
||||||
|
_BV( 1 ) , // PC 1 ** 36 ** D36
|
||||||
|
_BV( 0 ) , // PC 0 ** 37 ** D37
|
||||||
|
_BV( 7 ) , // PD 7 ** 38 ** D38
|
||||||
|
_BV( 2 ) , // PG 2 ** 39 ** D39
|
||||||
|
_BV( 1 ) , // PG 1 ** 40 ** D40
|
||||||
|
_BV( 0 ) , // PG 0 ** 41 ** D41
|
||||||
|
_BV( 7 ) , // PL 7 ** 42 ** D42
|
||||||
|
_BV( 6 ) , // PL 6 ** 43 ** D43
|
||||||
|
_BV( 5 ) , // PL 5 ** 44 ** D44
|
||||||
|
_BV( 4 ) , // PL 4 ** 45 ** D45
|
||||||
|
_BV( 3 ) , // PL 3 ** 46 ** D46
|
||||||
|
_BV( 2 ) , // PL 2 ** 47 ** D47
|
||||||
|
_BV( 1 ) , // PL 1 ** 48 ** D48
|
||||||
|
_BV( 0 ) , // PL 0 ** 49 ** D49
|
||||||
|
_BV( 3 ) , // PB 3 ** 50 ** SPI_MISO
|
||||||
|
_BV( 2 ) , // PB 2 ** 51 ** SPI_MOSI
|
||||||
|
_BV( 1 ) , // PB 1 ** 52 ** SPI_SCK
|
||||||
|
_BV( 0 ) , // PB 0 ** 53 ** SPI_SS
|
||||||
|
_BV( 0 ) , // PF 0 ** 54 ** A0
|
||||||
|
_BV( 1 ) , // PF 1 ** 55 ** A1
|
||||||
|
_BV( 2 ) , // PF 2 ** 56 ** A2
|
||||||
|
_BV( 3 ) , // PF 3 ** 57 ** A3
|
||||||
|
_BV( 4 ) , // PF 4 ** 58 ** A4
|
||||||
|
_BV( 5 ) , // PF 5 ** 59 ** A5
|
||||||
|
_BV( 6 ) , // PF 6 ** 60 ** A6
|
||||||
|
_BV( 7 ) , // PF 7 ** 61 ** A7
|
||||||
|
_BV( 0 ) , // PK 0 ** 62 ** A8
|
||||||
|
_BV( 1 ) , // PK 1 ** 63 ** A9
|
||||||
|
_BV( 2 ) , // PK 2 ** 64 ** A10
|
||||||
|
_BV( 3 ) , // PK 3 ** 65 ** A11
|
||||||
|
_BV( 4 ) , // PK 4 ** 66 ** A12
|
||||||
|
_BV( 5 ) , // PK 5 ** 67 ** A13
|
||||||
|
_BV( 6 ) , // PK 6 ** 68 ** A14
|
||||||
|
_BV( 7 ) , // PK 7 ** 69 ** A15
|
||||||
|
_BV( 4 ) , // PG 4 ** 70 ** D70
|
||||||
|
_BV( 3 ) , // PG 3 ** 71 ** D71
|
||||||
|
_BV( 2 ) , // PJ 2 ** 72 ** D72
|
||||||
|
_BV( 3 ) , // PJ 3 ** 73 ** D73
|
||||||
|
_BV( 7 ) , // PJ 7 ** 74 ** D74
|
||||||
|
_BV( 4 ) , // PJ 4 ** 75 ** D75
|
||||||
|
_BV( 5 ) , // PJ 5 ** 76 ** D76
|
||||||
|
_BV( 6 ) , // PJ 6 ** 77 ** D77
|
||||||
|
_BV( 2 ) , // PE 2 ** 78 ** D78
|
||||||
|
_BV( 6 ) , // PE 6 ** 79 ** D79
|
||||||
|
_BV( 7 ) , // PE 7 ** 80 ** D80
|
||||||
|
_BV( 4 ) , // PD 4 ** 81 ** D81
|
||||||
|
_BV( 5 ) , // PD 5 ** 82 ** D82
|
||||||
|
_BV( 6 ) , // PD 6 ** 83 ** D83
|
||||||
|
_BV( 2 ) , // PH 2 ** 84 ** D84
|
||||||
|
_BV( 7 ) , // PH 7 ** 85 ** D85
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {
|
||||||
|
// TIMERS
|
||||||
|
// -------------------------------------------
|
||||||
|
NOT_ON_TIMER , // PE 0 ** 0 ** USART0_RX
|
||||||
|
NOT_ON_TIMER , // PE 1 ** 1 ** USART0_TX
|
||||||
|
TIMER3B , // PE 4 ** 2 ** PWM2
|
||||||
|
TIMER3C , // PE 5 ** 3 ** PWM3
|
||||||
|
TIMER0B , // PG 5 ** 4 ** PWM4
|
||||||
|
TIMER3A , // PE 3 ** 5 ** PWM5
|
||||||
|
TIMER4A , // PH 3 ** 6 ** PWM6
|
||||||
|
TIMER4B , // PH 4 ** 7 ** PWM7
|
||||||
|
TIMER4C , // PH 5 ** 8 ** PWM8
|
||||||
|
TIMER2B , // PH 6 ** 9 ** PWM9
|
||||||
|
TIMER2A , // PB 4 ** 10 ** PWM10
|
||||||
|
TIMER1A , // PB 5 ** 11 ** PWM11
|
||||||
|
TIMER1B , // PB 6 ** 12 ** PWM12
|
||||||
|
TIMER0A , // PB 7 ** 13 ** PWM13
|
||||||
|
NOT_ON_TIMER , // PJ 1 ** 14 ** USART3_TX
|
||||||
|
NOT_ON_TIMER , // PJ 0 ** 15 ** USART3_RX
|
||||||
|
NOT_ON_TIMER , // PH 1 ** 16 ** USART2_TX
|
||||||
|
NOT_ON_TIMER , // PH 0 ** 17 ** USART2_RX
|
||||||
|
NOT_ON_TIMER , // PD 3 ** 18 ** USART1_TX
|
||||||
|
NOT_ON_TIMER , // PD 2 ** 19 ** USART1_RX
|
||||||
|
NOT_ON_TIMER , // PD 1 ** 20 ** I2C_SDA
|
||||||
|
NOT_ON_TIMER , // PD 0 ** 21 ** I2C_SCL
|
||||||
|
NOT_ON_TIMER , // PA 0 ** 22 ** D22
|
||||||
|
NOT_ON_TIMER , // PA 1 ** 23 ** D23
|
||||||
|
NOT_ON_TIMER , // PA 2 ** 24 ** D24
|
||||||
|
NOT_ON_TIMER , // PA 3 ** 25 ** D25
|
||||||
|
NOT_ON_TIMER , // PA 4 ** 26 ** D26
|
||||||
|
NOT_ON_TIMER , // PA 5 ** 27 ** D27
|
||||||
|
NOT_ON_TIMER , // PA 6 ** 28 ** D28
|
||||||
|
NOT_ON_TIMER , // PA 7 ** 29 ** D29
|
||||||
|
NOT_ON_TIMER , // PC 7 ** 30 ** D30
|
||||||
|
NOT_ON_TIMER , // PC 6 ** 31 ** D31
|
||||||
|
NOT_ON_TIMER , // PC 5 ** 32 ** D32
|
||||||
|
NOT_ON_TIMER , // PC 4 ** 33 ** D33
|
||||||
|
NOT_ON_TIMER , // PC 3 ** 34 ** D34
|
||||||
|
NOT_ON_TIMER , // PC 2 ** 35 ** D35
|
||||||
|
NOT_ON_TIMER , // PC 1 ** 36 ** D36
|
||||||
|
NOT_ON_TIMER , // PC 0 ** 37 ** D37
|
||||||
|
NOT_ON_TIMER , // PD 7 ** 38 ** D38
|
||||||
|
NOT_ON_TIMER , // PG 2 ** 39 ** D39
|
||||||
|
NOT_ON_TIMER , // PG 1 ** 40 ** D40
|
||||||
|
NOT_ON_TIMER , // PG 0 ** 41 ** D41
|
||||||
|
NOT_ON_TIMER , // PL 7 ** 42 ** D42
|
||||||
|
NOT_ON_TIMER , // PL 6 ** 43 ** D43
|
||||||
|
TIMER5C , // PL 5 ** 44 ** D44
|
||||||
|
TIMER5B , // PL 4 ** 45 ** D45
|
||||||
|
TIMER5A , // PL 3 ** 46 ** D46
|
||||||
|
NOT_ON_TIMER , // PL 2 ** 47 ** D47
|
||||||
|
NOT_ON_TIMER , // PL 1 ** 48 ** D48
|
||||||
|
NOT_ON_TIMER , // PL 0 ** 49 ** D49
|
||||||
|
NOT_ON_TIMER , // PB 3 ** 50 ** SPI_MISO
|
||||||
|
NOT_ON_TIMER , // PB 2 ** 51 ** SPI_MOSI
|
||||||
|
NOT_ON_TIMER , // PB 1 ** 52 ** SPI_SCK
|
||||||
|
NOT_ON_TIMER , // PB 0 ** 53 ** SPI_SS
|
||||||
|
NOT_ON_TIMER , // PF 0 ** 54 ** A0
|
||||||
|
NOT_ON_TIMER , // PF 1 ** 55 ** A1
|
||||||
|
NOT_ON_TIMER , // PF 2 ** 56 ** A2
|
||||||
|
NOT_ON_TIMER , // PF 3 ** 57 ** A3
|
||||||
|
NOT_ON_TIMER , // PF 4 ** 58 ** A4
|
||||||
|
NOT_ON_TIMER , // PF 5 ** 59 ** A5
|
||||||
|
NOT_ON_TIMER , // PF 6 ** 60 ** A6
|
||||||
|
NOT_ON_TIMER , // PF 7 ** 61 ** A7
|
||||||
|
NOT_ON_TIMER , // PK 0 ** 62 ** A8
|
||||||
|
NOT_ON_TIMER , // PK 1 ** 63 ** A9
|
||||||
|
NOT_ON_TIMER , // PK 2 ** 64 ** A10
|
||||||
|
NOT_ON_TIMER , // PK 3 ** 65 ** A11
|
||||||
|
NOT_ON_TIMER , // PK 4 ** 66 ** A12
|
||||||
|
NOT_ON_TIMER , // PK 5 ** 67 ** A13
|
||||||
|
NOT_ON_TIMER , // PK 6 ** 68 ** A14
|
||||||
|
NOT_ON_TIMER , // PK 7 ** 69 ** A15
|
||||||
|
NOT_ON_TIMER , // PG 4 ** 70 ** D70
|
||||||
|
NOT_ON_TIMER , // PG 3 ** 71 ** D71
|
||||||
|
NOT_ON_TIMER , // PJ 2 ** 72 ** D72
|
||||||
|
NOT_ON_TIMER , // PJ 3 ** 73 ** D73
|
||||||
|
NOT_ON_TIMER , // PJ 7 ** 74 ** D74
|
||||||
|
NOT_ON_TIMER , // PJ 4 ** 75 ** D75
|
||||||
|
NOT_ON_TIMER , // PJ 5 ** 76 ** D76
|
||||||
|
NOT_ON_TIMER , // PJ 6 ** 77 ** D77
|
||||||
|
NOT_ON_TIMER , // PE 2 ** 78 ** D78
|
||||||
|
NOT_ON_TIMER , // PE 6 ** 79 ** D79
|
||||||
|
NOT_ON_TIMER , // PE 7 ** 80 ** D80
|
||||||
|
NOT_ON_TIMER , // PD 4 ** 81 ** D81
|
||||||
|
NOT_ON_TIMER , // PD 5 ** 82 ** D82
|
||||||
|
NOT_ON_TIMER , // PD 6 ** 83 ** D83
|
||||||
|
NOT_ON_TIMER , // PH 2 ** 84 ** D84
|
||||||
|
NOT_ON_TIMER , // PH 7 ** 85 ** D85
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
BlinkM.cpp - Library for controlling a BlinkM over i2c
|
||||||
|
Created by Tim Koster, August 21 2013.
|
||||||
|
*/
|
||||||
|
#include "Marlin.h"
|
||||||
|
#ifdef BLINKM
|
||||||
|
|
||||||
|
#if (ARDUINO >= 100)
|
||||||
|
# include "Arduino.h"
|
||||||
|
#else
|
||||||
|
# include "WProgram.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "BlinkM.h"
|
||||||
|
|
||||||
|
void SendColors(byte red, byte grn, byte blu)
|
||||||
|
{
|
||||||
|
Wire.begin();
|
||||||
|
Wire.beginTransmission(0x09);
|
||||||
|
Wire.write('o'); //to disable ongoing script, only needs to be used once
|
||||||
|
Wire.write('n');
|
||||||
|
Wire.write(red);
|
||||||
|
Wire.write(grn);
|
||||||
|
Wire.write(blu);
|
||||||
|
Wire.endTransmission();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //BLINKM
|
||||||
|
|
@ -0,0 +1,14 @@
|
|||||||
|
/*
|
||||||
|
BlinkM.h
|
||||||
|
Library header file for BlinkM library
|
||||||
|
*/
|
||||||
|
#if (ARDUINO >= 100)
|
||||||
|
# include "Arduino.h"
|
||||||
|
#else
|
||||||
|
# include "WProgram.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "Wire.h"
|
||||||
|
|
||||||
|
void SendColors(byte red, byte grn, byte blu);
|
||||||
|
|
Binary file not shown.
@ -0,0 +1,56 @@
|
|||||||
|
/* -*- c++ -*- */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Reprap firmware based on Sprinter and grbl.
|
||||||
|
Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
This firmware is a mashup between Sprinter and grbl.
|
||||||
|
(https://github.com/kliment/Sprinter)
|
||||||
|
(https://github.com/simen/grbl/tree)
|
||||||
|
|
||||||
|
It has preliminary support for Matthew Roberts advance algorithm
|
||||||
|
http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* All the implementation is done in *.cpp files to get better compatibility with avr-gcc without the Arduino IDE */
|
||||||
|
/* Use this file to help the Arduino IDE find which Arduino libraries are needed and to keep documentation on GCode */
|
||||||
|
|
||||||
|
#include "Configuration.h"
|
||||||
|
#include "pins.h"
|
||||||
|
|
||||||
|
#ifdef ULTRA_LCD
|
||||||
|
#if defined(LCD_I2C_TYPE_PCF8575)
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <LiquidCrystal_I2C.h>
|
||||||
|
#elif defined(LCD_I2C_TYPE_MCP23017) || defined(LCD_I2C_TYPE_MCP23008)
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <LiquidTWI2.h>
|
||||||
|
#elif defined(DOGLCD)
|
||||||
|
#include <U8glib.h> // library for graphics LCD by Oli Kraus (https://code.google.com/p/u8glib/)
|
||||||
|
#else
|
||||||
|
#include <LiquidCrystal.h> // library for character LCD
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
|
||||||
|
#include <SPI.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(DIGIPOT_I2C)
|
||||||
|
#include <Wire.h>
|
||||||
|
#endif
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@ -0,0 +1,59 @@
|
|||||||
|
#include "Configuration.h"
|
||||||
|
|
||||||
|
#ifdef DIGIPOT_I2C
|
||||||
|
#include "Stream.h"
|
||||||
|
#include "utility/twi.h"
|
||||||
|
#include "Wire.h"
|
||||||
|
|
||||||
|
// Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
|
||||||
|
#if MOTHERBOARD == 88
|
||||||
|
#define DIGIPOT_I2C_FACTOR 117.96
|
||||||
|
#define DIGIPOT_I2C_MAX_CURRENT 1.736
|
||||||
|
#else
|
||||||
|
#define DIGIPOT_I2C_FACTOR 106.7
|
||||||
|
#define DIGIPOT_I2C_MAX_CURRENT 2.5
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static byte current_to_wiper( float current ){
|
||||||
|
return byte(ceil(float((DIGIPOT_I2C_FACTOR*current))));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void i2c_send(byte addr, byte a, byte b)
|
||||||
|
{
|
||||||
|
Wire.beginTransmission(addr);
|
||||||
|
Wire.write(a);
|
||||||
|
Wire.write(b);
|
||||||
|
Wire.endTransmission();
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is for the MCP4451 I2C based digipot
|
||||||
|
void digipot_i2c_set_current( int channel, float current )
|
||||||
|
{
|
||||||
|
current = min( (float) max( current, 0.0f ), DIGIPOT_I2C_MAX_CURRENT);
|
||||||
|
// these addresses are specific to Azteeg X3 Pro, can be set to others,
|
||||||
|
// In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
|
||||||
|
byte addr= 0x2C; // channel 0-3
|
||||||
|
if(channel >= 4) {
|
||||||
|
addr= 0x2E; // channel 4-7
|
||||||
|
channel-= 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initial setup
|
||||||
|
i2c_send( addr, 0x40, 0xff );
|
||||||
|
i2c_send( addr, 0xA0, 0xff );
|
||||||
|
|
||||||
|
// Set actual wiper value
|
||||||
|
byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
|
||||||
|
i2c_send( addr, addresses[channel], current_to_wiper(current) );
|
||||||
|
}
|
||||||
|
|
||||||
|
void digipot_i2c_init()
|
||||||
|
{
|
||||||
|
const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
|
||||||
|
Wire.begin();
|
||||||
|
// setup initial currents as defined in Configuration_adv.h
|
||||||
|
for(int i=0;i<=sizeof(digipot_motor_current)/sizeof(float);i++) {
|
||||||
|
digipot_i2c_set_current(i, digipot_motor_current[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
@ -0,0 +1,725 @@
|
|||||||
|
#ifndef CONFIGURATION_H
|
||||||
|
#define CONFIGURATION_H
|
||||||
|
|
||||||
|
// This configuration file contains the basic settings.
|
||||||
|
// Advanced settings can be found in Configuration_adv.h
|
||||||
|
// BASIC SETTINGS: select your board type, temperature sensor type, axis scaling, and endstop configuration
|
||||||
|
|
||||||
|
//===========================================================================
|
||||||
|
//============================= DELTA Printer ===============================
|
||||||
|
//===========================================================================
|
||||||
|
// For a Delta printer replace the configuration files with the files in the
|
||||||
|
// example_configurations/delta directory.
|
||||||
|
//
|
||||||
|
|
||||||
|
// User-specified version info of this build to display in [Pronterface, etc] terminal window during
|
||||||
|
// startup. Implementation of an idea by Prof Braino to inform user that any changes made to this
|
||||||
|
// build by the user have been successfully uploaded into firmware.
|
||||||
|
#define STRING_VERSION_CONFIG_H __DATE__ " " __TIME__ // build date and time
|
||||||
|
#define STRING_CONFIG_H_AUTHOR "(none, default config)" // Who made the changes.
|
||||||
|
|
||||||
|
// SERIAL_PORT selects which serial port should be used for communication with the host.
|
||||||
|
// This allows the connection of wireless adapters (for instance) to non-default port pins.
|
||||||
|
// Serial port 0 is still used by the Arduino bootloader regardless of this setting.
|
||||||
|
#define SERIAL_PORT 0
|
||||||
|
|
||||||
|
// This determines the communication speed of the printer
|
||||||
|
// This determines the communication speed of the printer
|
||||||
|
#define BAUDRATE 250000
|
||||||
|
|
||||||
|
// This enables the serial port associated to the Bluetooth interface
|
||||||
|
//#define BTENABLED // Enable BT interface on AT90USB devices
|
||||||
|
|
||||||
|
|
||||||
|
//// The following define selects which electronics board you have. Please choose the one that matches your setup
|
||||||
|
// 10 = Gen7 custom (Alfons3 Version) "https://github.com/Alfons3/Generation_7_Electronics"
|
||||||
|
// 11 = Gen7 v1.1, v1.2 = 11
|
||||||
|
// 12 = Gen7 v1.3
|
||||||
|
// 13 = Gen7 v1.4
|
||||||
|
// 2 = Cheaptronic v1.0
|
||||||
|
// 20 = Sethi 3D_1
|
||||||
|
// 3 = MEGA/RAMPS up to 1.2 = 3
|
||||||
|
// 33 = RAMPS 1.3 / 1.4 (Power outputs: Extruder, Fan, Bed)
|
||||||
|
// 34 = RAMPS 1.3 / 1.4 (Power outputs: Extruder0, Extruder1, Bed)
|
||||||
|
// 35 = RAMPS 1.3 / 1.4 (Power outputs: Extruder, Fan, Fan)
|
||||||
|
// 4 = Duemilanove w/ ATMega328P pin assignment
|
||||||
|
// 5 = Gen6
|
||||||
|
// 51 = Gen6 deluxe
|
||||||
|
// 6 = Sanguinololu < 1.2
|
||||||
|
// 62 = Sanguinololu 1.2 and above
|
||||||
|
// 63 = Melzi
|
||||||
|
// 64 = STB V1.1
|
||||||
|
// 65 = Azteeg X1
|
||||||
|
// 66 = Melzi with ATmega1284 (MaKr3d version)
|
||||||
|
// 67 = Azteeg X3
|
||||||
|
// 68 = Azteeg X3 Pro
|
||||||
|
// 7 = Ultimaker
|
||||||
|
// 71 = Ultimaker (Older electronics. Pre 1.5.4. This is rare)
|
||||||
|
// 72 = Ultimainboard 2.x (Uses TEMP_SENSOR 20)
|
||||||
|
// 77 = 3Drag Controller
|
||||||
|
// 8 = Teensylu
|
||||||
|
// 80 = Rumba
|
||||||
|
// 81 = Printrboard (AT90USB1286)
|
||||||
|
// 82 = Brainwave (AT90USB646)
|
||||||
|
// 83 = SAV Mk-I (AT90USB1286)
|
||||||
|
// 9 = Gen3+
|
||||||
|
// 70 = Megatronics
|
||||||
|
// 701= Megatronics v2.0
|
||||||
|
// 702= Minitronics v1.0
|
||||||
|
// 90 = Alpha OMCA board
|
||||||
|
// 91 = Final OMCA board
|
||||||
|
// 301= Rambo
|
||||||
|
// 21 = Elefu Ra Board (v3)
|
||||||
|
// 88 = 5DPrint D8 Driver Board
|
||||||
|
|
||||||
|
#ifndef MOTHERBOARD
|
||||||
|
#define MOTHERBOARD 88
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Define this to set a custom name for your generic Mendel,
|
||||||
|
// #define CUSTOM_MENDEL_NAME "This Mendel"
|
||||||
|
|
||||||
|
// Define this to set a unique identifier for this printer, (Used by some programs to differentiate between machines)
|
||||||
|
// You can use an online service to generate a random UUID. (eg http://www.uuidgenerator.net/version4)
|
||||||
|
// #define MACHINE_UUID "00000000-0000-0000-0000-000000000000"
|
||||||
|
|
||||||
|
// This defines the number of extruders
|
||||||
|
#define EXTRUDERS 1
|
||||||
|
|
||||||
|
//// The following define selects which power supply you have. Please choose the one that matches your setup
|
||||||
|
// 1 = ATX
|
||||||
|
// 2 = X-Box 360 203Watts (the blue wire connected to PS_ON and the red wire to VCC)
|
||||||
|
|
||||||
|
#define POWER_SUPPLY 1
|
||||||
|
|
||||||
|
// Define this to have the electronics keep the power supply off on startup. If you don't know what this is leave it.
|
||||||
|
// #define PS_DEFAULT_OFF
|
||||||
|
|
||||||
|
//===========================================================================
|
||||||
|
//=============================Thermal Settings ============================
|
||||||
|
//===========================================================================
|
||||||
|
//
|
||||||
|
//--NORMAL IS 4.7kohm PULLUP!-- 1kohm pullup can be used on hotend sensor, using correct resistor and table
|
||||||
|
//
|
||||||
|
//// Temperature sensor settings:
|
||||||
|
// -2 is thermocouple with MAX6675 (only for sensor 0)
|
||||||
|
// -1 is thermocouple with AD595
|
||||||
|
// 0 is not used
|
||||||
|
// 1 is 100k thermistor - best choice for EPCOS 100k (4.7k pullup)
|
||||||
|
// 2 is 200k thermistor - ATC Semitec 204GT-2 (4.7k pullup)
|
||||||
|
// 3 is Mendel-parts thermistor (4.7k pullup)
|
||||||
|
// 4 is 10k thermistor !! do not use it for a hotend. It gives bad resolution at high temp. !!
|
||||||
|
// 5 is 100K thermistor - ATC Semitec 104GT-2 (Used in ParCan & J-Head) (4.7k pullup)
|
||||||
|
// 6 is 100k EPCOS - Not as accurate as table 1 (created using a fluke thermocouple) (4.7k pullup)
|
||||||
|
// 7 is 100k Honeywell thermistor 135-104LAG-J01 (4.7k pullup)
|
||||||
|
// 71 is 100k Honeywell thermistor 135-104LAF-J01 (4.7k pullup)
|
||||||
|
// 8 is 100k 0603 SMD Vishay NTCS0603E3104FXT (4.7k pullup)
|
||||||
|
// 9 is 100k GE Sensing AL03006-58.2K-97-G1 (4.7k pullup)
|
||||||
|
// 10 is 100k RS thermistor 198-961 (4.7k pullup)
|
||||||
|
// 11 is 100k beta 3950 1% thermistor (4.7k pullup)
|
||||||
|
// 12 is 100k 0603 SMD Vishay NTCS0603E3104FXT (4.7k pullup) (calibrated for Makibox hot bed)
|
||||||
|
// 20 is the PT100 circuit found in the Ultimainboard V2.x
|
||||||
|
// 60 is 100k Maker's Tool Works Kapton Bed Thermistor beta=3950
|
||||||
|
//
|
||||||
|
// 1k ohm pullup tables - This is not normal, you would have to have changed out your 4.7k for 1k
|
||||||
|
// (but gives greater accuracy and more stable PID)
|
||||||
|
// 51 is 100k thermistor - EPCOS (1k pullup)
|
||||||
|
// 52 is 200k thermistor - ATC Semitec 204GT-2 (1k pullup)
|
||||||
|
// 55 is 100k thermistor - ATC Semitec 104GT-2 (Used in ParCan & J-Head) (1k pullup)
|
||||||
|
//
|
||||||
|
// 1047 is Pt1000 with 4k7 pullup
|
||||||
|
// 1010 is Pt1000 with 1k pullup (non standard)
|
||||||
|
// 147 is Pt100 with 4k7 pullup
|
||||||
|
// 110 is Pt100 with 1k pullup (non standard)
|
||||||
|
|
||||||
|
#define TEMP_SENSOR_0 1
|
||||||
|
#define TEMP_SENSOR_1 0
|
||||||
|
#define TEMP_SENSOR_2 0
|
||||||
|
#define TEMP_SENSOR_BED 12
|
||||||
|
|
||||||
|
// This makes temp sensor 1 a redundant sensor for sensor 0. If the temperatures difference between these sensors is to high the print will be aborted.
|
||||||
|
//#define TEMP_SENSOR_1_AS_REDUNDANT
|
||||||
|
#define MAX_REDUNDANT_TEMP_SENSOR_DIFF 10
|
||||||
|
|
||||||
|
// Actual temperature must be close to target for this long before M109 returns success
|
||||||
|
#define TEMP_RESIDENCY_TIME 10 // (seconds)
|
||||||
|
#define TEMP_HYSTERESIS 3 // (degC) range of +/- temperatures considered "close" to the target one
|
||||||
|
#define TEMP_WINDOW 1 // (degC) Window around target to start the residency timer x degC early.
|
||||||
|
|
||||||
|
// The minimal temperature defines the temperature below which the heater will not be enabled It is used
|
||||||
|
// to check that the wiring to the thermistor is not broken.
|
||||||
|
// Otherwise this would lead to the heater being powered on all the time.
|
||||||
|
#define HEATER_0_MINTEMP 5
|
||||||
|
#define HEATER_1_MINTEMP 5
|
||||||
|
#define HEATER_2_MINTEMP 5
|
||||||
|
#define BED_MINTEMP 5
|
||||||
|
|
||||||
|
// When temperature exceeds max temp, your heater will be switched off.
|
||||||
|
// This feature exists to protect your hotend from overheating accidentally, but *NOT* from thermistor short/failure!
|
||||||
|
// You should use MINTEMP for thermistor short/failure protection.
|
||||||
|
#define HEATER_0_MAXTEMP 275
|
||||||
|
#define HEATER_1_MAXTEMP 275
|
||||||
|
#define HEATER_2_MAXTEMP 275
|
||||||
|
#define BED_MAXTEMP 150
|
||||||
|
|
||||||
|
// If your bed has low resistance e.g. .6 ohm and throws the fuse you can duty cycle it to reduce the
|
||||||
|
// average current. The value should be an integer and the heat bed will be turned on for 1 interval of
|
||||||
|
// HEATER_BED_DUTY_CYCLE_DIVIDER intervals.
|
||||||
|
//#define HEATER_BED_DUTY_CYCLE_DIVIDER 4
|
||||||
|
|
||||||
|
// If you want the M105 heater power reported in watts, define the BED_WATTS, and (shared for all extruders) EXTRUDER_WATTS
|
||||||
|
//#define EXTRUDER_WATTS (12.0*12.0/6.7) // P=I^2/R
|
||||||
|
//#define BED_WATTS (12.0*12.0/1.1) // P=I^2/R
|
||||||
|
|
||||||
|
// PID settings:
|
||||||
|
// Comment the following line to disable PID and enable bang-bang.
|
||||||
|
#define PIDTEMP
|
||||||
|
#define BANG_MAX 255 // limits current to nozzle while in bang-bang mode; 255=full current
|
||||||
|
#define PID_MAX 255 // limits current to nozzle while PID is active (see PID_FUNCTIONAL_RANGE below); 255=full current
|
||||||
|
#ifdef PIDTEMP
|
||||||
|
//#define PID_DEBUG // Sends debug data to the serial port.
|
||||||
|
//#define PID_OPENLOOP 1 // Puts PID in open loop. M104/M140 sets the output power from 0 to PID_MAX
|
||||||
|
#define PID_FUNCTIONAL_RANGE 10 // If the temperature difference between the target temperature and the actual temperature
|
||||||
|
// is more then PID_FUNCTIONAL_RANGE then the PID will be shut off and the heater will be set to min/max.
|
||||||
|
#define PID_INTEGRAL_DRIVE_MAX 255 //limit for the integral term
|
||||||
|
#define K1 0.95 //smoothing factor within the PID
|
||||||
|
#define PID_dT ((OVERSAMPLENR * 8.0)/(F_CPU / 64.0 / 256.0)) //sampling period of the temperature routine
|
||||||
|
|
||||||
|
// If you are using a pre-configured hotend then you can use one of the value sets by uncommenting it
|
||||||
|
// Ultimaker
|
||||||
|
#define DEFAULT_Kp 22.2
|
||||||
|
#define DEFAULT_Ki 1.08
|
||||||
|
#define DEFAULT_Kd 114
|
||||||
|
|
||||||
|
// MakerGear
|
||||||
|
// #define DEFAULT_Kp 7.0
|
||||||
|
// #define DEFAULT_Ki 0.1
|
||||||
|
// #define DEFAULT_Kd 12
|
||||||
|
|
||||||
|
// Mendel Parts V9 on 12V
|
||||||
|
// #define DEFAULT_Kp 63.0
|
||||||
|
// #define DEFAULT_Ki 2.25
|
||||||
|
// #define DEFAULT_Kd 440
|
||||||
|
#endif // PIDTEMP
|
||||||
|
|
||||||
|
// Bed Temperature Control
|
||||||
|
// Select PID or bang-bang with PIDTEMPBED. If bang-bang, BED_LIMIT_SWITCHING will enable hysteresis
|
||||||
|
//
|
||||||
|
// Uncomment this to enable PID on the bed. It uses the same frequency PWM as the extruder.
|
||||||
|
// If your PID_dT above is the default, and correct for your hardware/configuration, that means 7.689Hz,
|
||||||
|
// which is fine for driving a square wave into a resistive load and does not significantly impact you FET heating.
|
||||||
|
// This also works fine on a Fotek SSR-10DA Solid State Relay into a 250W heater.
|
||||||
|
// If your configuration is significantly different than this and you don't understand the issues involved, you probably
|
||||||
|
// shouldn't use bed PID until someone else verifies your hardware works.
|
||||||
|
// If this is enabled, find your own PID constants below.
|
||||||
|
//#define PIDTEMPBED
|
||||||
|
//
|
||||||
|
//#define BED_LIMIT_SWITCHING
|
||||||
|
|
||||||
|
// This sets the max power delivered to the bed, and replaces the HEATER_BED_DUTY_CYCLE_DIVIDER option.
|
||||||
|
// all forms of bed control obey this (PID, bang-bang, bang-bang with hysteresis)
|
||||||
|
// setting this to anything other than 255 enables a form of PWM to the bed just like HEATER_BED_DUTY_CYCLE_DIVIDER did,
|
||||||
|
// so you shouldn't use it unless you are OK with PWM on your bed. (see the comment on enabling PIDTEMPBED)
|
||||||
|
#define MAX_BED_POWER 175 // limits duty cycle to bed; 255=full current
|
||||||
|
// This limit is set to 175 by default in the Makibox configuration and it can adjusted
|
||||||
|
// to increase the heat up rate. However, if changed, user must be aware of the safety concerns
|
||||||
|
// of drawing too much current from the power supply.
|
||||||
|
|
||||||
|
#ifdef PIDTEMPBED
|
||||||
|
//120v 250W silicone heater into 4mm borosilicate (MendelMax 1.5+)
|
||||||
|
//from FOPDT model - kp=.39 Tp=405 Tdead=66, Tc set to 79.2, aggressive factor of .15 (vs .1, 1, 10)
|
||||||
|
#define DEFAULT_bedKp 10.00
|
||||||
|
#define DEFAULT_bedKi .023
|
||||||
|
#define DEFAULT_bedKd 305.4
|
||||||
|
|
||||||
|
//120v 250W silicone heater into 4mm borosilicate (MendelMax 1.5+)
|
||||||
|
//from pidautotune
|
||||||
|
// #define DEFAULT_bedKp 97.1
|
||||||
|
// #define DEFAULT_bedKi 1.41
|
||||||
|
// #define DEFAULT_bedKd 1675.16
|
||||||
|
|
||||||
|
// FIND YOUR OWN: "M303 E-1 C8 S90" to run autotune on the bed at 90 degreesC for 8 cycles.
|
||||||
|
#endif // PIDTEMPBED
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//this prevents dangerous Extruder moves, i.e. if the temperature is under the limit
|
||||||
|
//can be software-disabled for whatever purposes by
|
||||||
|
#define PREVENT_DANGEROUS_EXTRUDE
|
||||||
|
//if PREVENT_DANGEROUS_EXTRUDE is on, you can still disable (uncomment) very long bits of extrusion separately.
|
||||||
|
#define PREVENT_LENGTHY_EXTRUDE
|
||||||
|
|
||||||
|
#define EXTRUDE_MINTEMP 170
|
||||||
|
#define EXTRUDE_MAXLENGTH (X_MAX_LENGTH+Y_MAX_LENGTH) //prevent extrusion of very large distances.
|
||||||
|
|
||||||
|
//===========================================================================
|
||||||
|
//=============================Mechanical Settings===========================
|
||||||
|
//===========================================================================
|
||||||
|
|
||||||
|
// Uncomment the following line to enable CoreXY kinematics
|
||||||
|
// #define COREXY
|
||||||
|
|
||||||
|
// coarse Endstop Settings
|
||||||
|
#define ENDSTOPPULLUPS // Comment this out (using // at the start of the line) to disable the endstop pullup resistors
|
||||||
|
|
||||||
|
#ifndef ENDSTOPPULLUPS
|
||||||
|
// fine endstop settings: Individual pullups. will be ignored if ENDSTOPPULLUPS is defined
|
||||||
|
// #define ENDSTOPPULLUP_XMAX
|
||||||
|
// #define ENDSTOPPULLUP_YMAX
|
||||||
|
// #define ENDSTOPPULLUP_ZMAX
|
||||||
|
// #define ENDSTOPPULLUP_XMIN
|
||||||
|
// #define ENDSTOPPULLUP_YMIN
|
||||||
|
// #define ENDSTOPPULLUP_ZMIN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ENDSTOPPULLUPS
|
||||||
|
#define ENDSTOPPULLUP_XMAX
|
||||||
|
#define ENDSTOPPULLUP_YMAX
|
||||||
|
#define ENDSTOPPULLUP_ZMAX
|
||||||
|
#define ENDSTOPPULLUP_XMIN
|
||||||
|
#define ENDSTOPPULLUP_YMIN
|
||||||
|
#define ENDSTOPPULLUP_ZMIN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// The pullups are needed if you directly connect a mechanical endswitch between the signal and ground pins.
|
||||||
|
const bool X_MIN_ENDSTOP_INVERTING = false; // set to true to invert the logic of the endstop.
|
||||||
|
const bool Y_MIN_ENDSTOP_INVERTING = false; // set to true to invert the logic of the endstop.
|
||||||
|
const bool Z_MIN_ENDSTOP_INVERTING = false; // set to true to invert the logic of the endstop.
|
||||||
|
const bool X_MAX_ENDSTOP_INVERTING = true; // set to true to invert the logic of the endstop.
|
||||||
|
const bool Y_MAX_ENDSTOP_INVERTING = true; // set to true to invert the logic of the endstop.
|
||||||
|
const bool Z_MAX_ENDSTOP_INVERTING = true; // set to true to invert the logic of the endstop.
|
||||||
|
//#define DISABLE_MAX_ENDSTOPS
|
||||||
|
//#define DISABLE_MIN_ENDSTOPS
|
||||||
|
|
||||||
|
// Disable max endstops for compatibility with endstop checking routine
|
||||||
|
#if defined(COREXY) && !defined(DISABLE_MAX_ENDSTOPS)
|
||||||
|
#define DISABLE_MAX_ENDSTOPS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// For Inverting Stepper Enable Pins (Active Low) use 0, Non Inverting (Active High) use 1
|
||||||
|
#define X_ENABLE_ON 0
|
||||||
|
#define Y_ENABLE_ON 0
|
||||||
|
#define Z_ENABLE_ON 0
|
||||||
|
#define E_ENABLE_ON 0 // For all extruders
|
||||||
|
|
||||||
|
// Disables axis when it's not being used.
|
||||||
|
#define DISABLE_X false
|
||||||
|
#define DISABLE_Y false
|
||||||
|
#define DISABLE_Z false
|
||||||
|
#define DISABLE_E false // For all extruders
|
||||||
|
|
||||||
|
#define INVERT_X_DIR false // for Mendel set to false, for Orca set to true
|
||||||
|
#define INVERT_Y_DIR false // for Mendel set to true, for Orca set to false
|
||||||
|
#define INVERT_Z_DIR false // for Mendel set to false, for Orca set to true
|
||||||
|
#define INVERT_E0_DIR true // for direct drive extruder v9 set to true, for geared extruder set to false
|
||||||
|
#define INVERT_E1_DIR false // for direct drive extruder v9 set to true, for geared extruder set to false
|
||||||
|
#define INVERT_E2_DIR false // for direct drive extruder v9 set to true, for geared extruder set to false
|
||||||
|
|
||||||
|
// ENDSTOP SETTINGS:
|
||||||
|
// Sets direction of endstops when homing; 1=MAX, -1=MIN
|
||||||
|
#define X_HOME_DIR -1
|
||||||
|
#define Y_HOME_DIR -1
|
||||||
|
#define Z_HOME_DIR -1
|
||||||
|
|
||||||
|
#define min_software_endstops true // If true, axis won't move to coordinates less than HOME_POS.
|
||||||
|
#define max_software_endstops true // If true, axis won't move to coordinates greater than the defined lengths below.
|
||||||
|
|
||||||
|
// Travel limits after homing
|
||||||
|
#define X_MAX_POS 110
|
||||||
|
#define X_MIN_POS 0
|
||||||
|
#define Y_MAX_POS 150
|
||||||
|
#define Y_MIN_POS 0
|
||||||
|
#define Z_MAX_POS 86
|
||||||
|
#define Z_MIN_POS 0
|
||||||
|
|
||||||
|
#define X_MAX_LENGTH (X_MAX_POS - X_MIN_POS)
|
||||||
|
#define Y_MAX_LENGTH (Y_MAX_POS - Y_MIN_POS)
|
||||||
|
#define Z_MAX_LENGTH (Z_MAX_POS - Z_MIN_POS)
|
||||||
|
//============================= Bed Auto Leveling ===========================
|
||||||
|
|
||||||
|
//#define ENABLE_AUTO_BED_LEVELING // Delete the comment to enable (remove // at the start of the line)
|
||||||
|
|
||||||
|
#ifdef ENABLE_AUTO_BED_LEVELING
|
||||||
|
|
||||||
|
// There are 2 different ways to pick the X and Y locations to probe:
|
||||||
|
|
||||||
|
// - "grid" mode
|
||||||
|
// Probe every point in a rectangular grid
|
||||||
|
// You must specify the rectangle, and the density of sample points
|
||||||
|
// This mode is preferred because there are more measurements.
|
||||||
|
// It used to be called ACCURATE_BED_LEVELING but "grid" is more descriptive
|
||||||
|
|
||||||
|
// - "3-point" mode
|
||||||
|
// Probe 3 arbitrary points on the bed (that aren't colinear)
|
||||||
|
// You must specify the X & Y coordinates of all 3 points
|
||||||
|
|
||||||
|
#define AUTO_BED_LEVELING_GRID
|
||||||
|
// with AUTO_BED_LEVELING_GRID, the bed is sampled in a
|
||||||
|
// AUTO_BED_LEVELING_GRID_POINTSxAUTO_BED_LEVELING_GRID_POINTS grid
|
||||||
|
// and least squares solution is calculated
|
||||||
|
// Note: this feature occupies 10'206 byte
|
||||||
|
#ifdef AUTO_BED_LEVELING_GRID
|
||||||
|
|
||||||
|
// set the rectangle in which to probe
|
||||||
|
#define LEFT_PROBE_BED_POSITION 15
|
||||||
|
#define RIGHT_PROBE_BED_POSITION 170
|
||||||
|
#define BACK_PROBE_BED_POSITION 180
|
||||||
|
#define FRONT_PROBE_BED_POSITION 20
|
||||||
|
|
||||||
|
// set the number of grid points per dimension
|
||||||
|
// I wouldn't see a reason to go above 3 (=9 probing points on the bed)
|
||||||
|
#define AUTO_BED_LEVELING_GRID_POINTS 2
|
||||||
|
|
||||||
|
|
||||||
|
#else // not AUTO_BED_LEVELING_GRID
|
||||||
|
// with no grid, just probe 3 arbitrary points. A simple cross-product
|
||||||
|
// is used to esimate the plane of the print bed
|
||||||
|
|
||||||
|
#define ABL_PROBE_PT_1_X 15
|
||||||
|
#define ABL_PROBE_PT_1_Y 180
|
||||||
|
#define ABL_PROBE_PT_2_X 15
|
||||||
|
#define ABL_PROBE_PT_2_Y 20
|
||||||
|
#define ABL_PROBE_PT_3_X 170
|
||||||
|
#define ABL_PROBE_PT_3_Y 20
|
||||||
|
|
||||||
|
#endif // AUTO_BED_LEVELING_GRID
|
||||||
|
|
||||||
|
|
||||||
|
// these are the offsets to the probe relative to the extruder tip (Hotend - Probe)
|
||||||
|
#define X_PROBE_OFFSET_FROM_EXTRUDER -25
|
||||||
|
#define Y_PROBE_OFFSET_FROM_EXTRUDER -29
|
||||||
|
#define Z_PROBE_OFFSET_FROM_EXTRUDER -12.35
|
||||||
|
|
||||||
|
#define Z_RAISE_BEFORE_HOMING 4 // (in mm) Raise Z before homing (G28) for Probe Clearance.
|
||||||
|
// Be sure you have this distance over your Z_MAX_POS in case
|
||||||
|
|
||||||
|
#define XY_TRAVEL_SPEED 8000 // X and Y axis travel speed between probes, in mm/min
|
||||||
|
|
||||||
|
#define Z_RAISE_BEFORE_PROBING 15 //How much the extruder will be raised before traveling to the first probing point.
|
||||||
|
#define Z_RAISE_BETWEEN_PROBINGS 5 //How much the extruder will be raised when traveling from between next probing points
|
||||||
|
|
||||||
|
|
||||||
|
//If defined, the Probe servo will be turned on only during movement and then turned off to avoid jerk
|
||||||
|
//The value is the delay to turn the servo off after powered on - depends on the servo speed; 300ms is good value, but you can try lower it.
|
||||||
|
// You MUST HAVE the SERVO_ENDSTOPS defined to use here a value higher than zero otherwise your code will not compile.
|
||||||
|
|
||||||
|
// #define PROBE_SERVO_DEACTIVATION_DELAY 300
|
||||||
|
|
||||||
|
|
||||||
|
//If you have enabled the Bed Auto Leveling and are using the same Z Probe for Z Homing,
|
||||||
|
//it is highly recommended you let this Z_SAFE_HOMING enabled!!!
|
||||||
|
|
||||||
|
#define Z_SAFE_HOMING // This feature is meant to avoid Z homing with probe outside the bed area.
|
||||||
|
// When defined, it will:
|
||||||
|
// - Allow Z homing only after X and Y homing AND stepper drivers still enabled
|
||||||
|
// - If stepper drivers timeout, it will need X and Y homing again before Z homing
|
||||||
|
// - Position the probe in a defined XY point before Z Homing when homing all axis (G28)
|
||||||
|
// - Block Z homing only when the probe is outside bed area.
|
||||||
|
|
||||||
|
#ifdef Z_SAFE_HOMING
|
||||||
|
|
||||||
|
#define Z_SAFE_HOMING_X_POINT (X_MAX_LENGTH/2) // X point for Z homing when homing all axis (G28)
|
||||||
|
#define Z_SAFE_HOMING_Y_POINT (Y_MAX_LENGTH/2) // Y point for Z homing when homing all axis (G28)
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // ENABLE_AUTO_BED_LEVELING
|
||||||
|
|
||||||
|
|
||||||
|
// The position of the homing switches
|
||||||
|
//#define MANUAL_HOME_POSITIONS // If defined, MANUAL_*_HOME_POS below will be used
|
||||||
|
//#define BED_CENTER_AT_0_0 // If defined, the center of the bed is at (X=0, Y=0)
|
||||||
|
|
||||||
|
//Manual homing switch locations:
|
||||||
|
// For deltabots this means top and center of the Cartesian print volume.
|
||||||
|
#define MANUAL_X_HOME_POS 0
|
||||||
|
#define MANUAL_Y_HOME_POS 0
|
||||||
|
#define MANUAL_Z_HOME_POS 0
|
||||||
|
//#define MANUAL_Z_HOME_POS 402 // For delta: Distance between nozzle and print surface after homing.
|
||||||
|
|
||||||
|
//// MOVEMENT SETTINGS
|
||||||
|
#define NUM_AXIS 4 // The axis order in all axis related arrays is X, Y, Z, E
|
||||||
|
#define HOMING_FEEDRATE {1500, 1500, 120, 0} // set the homing speeds (mm/min) ***** MakiBox A6 *****
|
||||||
|
|
||||||
|
// default settings
|
||||||
|
|
||||||
|
#define DEFAULT_AXIS_STEPS_PER_UNIT {400, 400, 400, 163} // default steps per unit for ***** MakiBox A6 *****
|
||||||
|
#define DEFAULT_MAX_FEEDRATE {60, 60, 20, 45} // (mm/sec)
|
||||||
|
#define DEFAULT_MAX_ACCELERATION {2000,2000,30,10000} // X, Y, Z, E maximum start speed for accelerated moves. E default values are good for skeinforge 40+, for older versions raise them a lot.
|
||||||
|
|
||||||
|
#define DEFAULT_ACCELERATION 3000 // X, Y, Z and E max acceleration in mm/s^2 for printing moves
|
||||||
|
#define DEFAULT_RETRACT_ACCELERATION 3000 // X, Y, Z and E max acceleration in mm/s^2 for retracts
|
||||||
|
|
||||||
|
// Offset of the extruders (uncomment if using more than one and relying on firmware to position when changing).
|
||||||
|
// The offset has to be X=0, Y=0 for the extruder 0 hotend (default extruder).
|
||||||
|
// For the other hotends it is their distance from the extruder 0 hotend.
|
||||||
|
// #define EXTRUDER_OFFSET_X {0.0, 20.00} // (in mm) for each extruder, offset of the hotend on the X axis
|
||||||
|
// #define EXTRUDER_OFFSET_Y {0.0, 5.00} // (in mm) for each extruder, offset of the hotend on the Y axis
|
||||||
|
|
||||||
|
// The speed change that does not require acceleration (i.e. the software might assume it can be done instantaneously)
|
||||||
|
#define DEFAULT_XYJERK 20.0 // (mm/sec)
|
||||||
|
#define DEFAULT_ZJERK 0.4 // (mm/sec)
|
||||||
|
#define DEFAULT_EJERK 5.0 // (mm/sec)
|
||||||
|
|
||||||
|
//===========================================================================
|
||||||
|
//=============================Additional Features===========================
|
||||||
|
//===========================================================================
|
||||||
|
|
||||||
|
// Custom M code points
|
||||||
|
#define CUSTOM_M_CODES
|
||||||
|
#ifdef CUSTOM_M_CODES
|
||||||
|
#define CUSTOM_M_CODE_SET_Z_PROBE_OFFSET 851
|
||||||
|
#define Z_PROBE_OFFSET_RANGE_MIN -15
|
||||||
|
#define Z_PROBE_OFFSET_RANGE_MAX -5
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// EEPROM
|
||||||
|
// The microcontroller can store settings in the EEPROM, e.g. max velocity...
|
||||||
|
// M500 - stores parameters in EEPROM
|
||||||
|
// M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
|
||||||
|
// M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
|
||||||
|
//define this to enable EEPROM support
|
||||||
|
#define EEPROM_SETTINGS
|
||||||
|
//to disable EEPROM Serial responses and decrease program space by ~1700 byte: comment this out:
|
||||||
|
// please keep turned on if you can.
|
||||||
|
//#define EEPROM_CHITCHAT
|
||||||
|
|
||||||
|
// Preheat Constants
|
||||||
|
#define PLA_PREHEAT_HOTEND_TEMP 180
|
||||||
|
#define PLA_PREHEAT_HPB_TEMP 70
|
||||||
|
#define PLA_PREHEAT_FAN_SPEED 255 // Insert Value between 0 and 255
|
||||||
|
|
||||||
|
#define ABS_PREHEAT_HOTEND_TEMP 240
|
||||||
|
#define ABS_PREHEAT_HPB_TEMP 100
|
||||||
|
#define ABS_PREHEAT_FAN_SPEED 255 // Insert Value between 0 and 255
|
||||||
|
|
||||||
|
//LCD and SD support
|
||||||
|
//#define ULTRA_LCD //general LCD support, also 16x2
|
||||||
|
//#define DOGLCD // Support for SPI LCD 128x64 (Controller ST7565R graphic Display Family)
|
||||||
|
#define SDSUPPORT // Enable SD Card Support in Hardware Console
|
||||||
|
#define SDSLOW // Use slower SD transfer mode (not normally needed - uncomment if you're getting volume init error)
|
||||||
|
//#define ENCODER_PULSES_PER_STEP 1 // Increase if you have a high resolution encoder
|
||||||
|
//#define ENCODER_STEPS_PER_MENU_ITEM 5 // Set according to ENCODER_PULSES_PER_STEP or your liking
|
||||||
|
//#define ULTIMAKERCONTROLLER //as available from the Ultimaker online store.
|
||||||
|
//#define ULTIPANEL //the UltiPanel as on Thingiverse
|
||||||
|
//#define LCD_FEEDBACK_FREQUENCY_HZ 1000 // this is the tone frequency the buzzer plays when on UI feedback. ie Screen Click
|
||||||
|
//#define LCD_FEEDBACK_FREQUENCY_DURATION_MS 100 // the duration the buzzer plays the UI feedback sound. ie Screen Click
|
||||||
|
|
||||||
|
// The MaKr3d Makr-Panel with graphic controller and SD support
|
||||||
|
// http://reprap.org/wiki/MaKr3d_MaKrPanel
|
||||||
|
//#define MAKRPANEL
|
||||||
|
|
||||||
|
// The RepRapDiscount Smart Controller (white PCB)
|
||||||
|
// http://reprap.org/wiki/RepRapDiscount_Smart_Controller
|
||||||
|
//#define REPRAP_DISCOUNT_SMART_CONTROLLER
|
||||||
|
|
||||||
|
// The GADGETS3D G3D LCD/SD Controller (blue PCB)
|
||||||
|
// http://reprap.org/wiki/RAMPS_1.3/1.4_GADGETS3D_Shield_with_Panel
|
||||||
|
//#define G3D_PANEL
|
||||||
|
|
||||||
|
// The RepRapDiscount FULL GRAPHIC Smart Controller (quadratic white PCB)
|
||||||
|
// http://reprap.org/wiki/RepRapDiscount_Full_Graphic_Smart_Controller
|
||||||
|
//
|
||||||
|
// ==> REMEMBER TO INSTALL U8glib to your ARDUINO library folder: http://code.google.com/p/u8glib/wiki/u8glib
|
||||||
|
//#define REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER
|
||||||
|
|
||||||
|
// The RepRapWorld REPRAPWORLD_KEYPAD v1.1
|
||||||
|
// http://reprapworld.com/?products_details&products_id=202&cPath=1591_1626
|
||||||
|
//#define REPRAPWORLD_KEYPAD
|
||||||
|
//#define REPRAPWORLD_KEYPAD_MOVE_STEP 10.0 // how much should be moved when a key is pressed, eg 10.0 means 10mm per click
|
||||||
|
|
||||||
|
// The Elefu RA Board Control Panel
|
||||||
|
// http://www.elefu.com/index.php?route=product/product&product_id=53
|
||||||
|
// REMEMBER TO INSTALL LiquidCrystal_I2C.h in your ARUDINO library folder: https://github.com/kiyoshigawa/LiquidCrystal_I2C
|
||||||
|
//#define RA_CONTROL_PANEL
|
||||||
|
|
||||||
|
//automatic expansion
|
||||||
|
#if defined (MAKRPANEL)
|
||||||
|
#define DOGLCD
|
||||||
|
#define SDSUPPORT
|
||||||
|
#define ULTIPANEL
|
||||||
|
#define NEWPANEL
|
||||||
|
#define DEFAULT_LCD_CONTRAST 17
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined (REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER)
|
||||||
|
#define DOGLCD
|
||||||
|
#define U8GLIB_ST7920
|
||||||
|
#define REPRAP_DISCOUNT_SMART_CONTROLLER
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ULTIMAKERCONTROLLER) || defined(REPRAP_DISCOUNT_SMART_CONTROLLER) || defined(G3D_PANEL)
|
||||||
|
#define ULTIPANEL
|
||||||
|
#define NEWPANEL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(REPRAPWORLD_KEYPAD)
|
||||||
|
#define NEWPANEL
|
||||||
|
#define ULTIPANEL
|
||||||
|
#endif
|
||||||
|
#if defined(RA_CONTROL_PANEL)
|
||||||
|
#define ULTIPANEL
|
||||||
|
#define NEWPANEL
|
||||||
|
#define LCD_I2C_TYPE_PCA8574
|
||||||
|
#define LCD_I2C_ADDRESS 0x27 // I2C Address of the port expander
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//I2C PANELS
|
||||||
|
|
||||||
|
//#define LCD_I2C_SAINSMART_YWROBOT
|
||||||
|
#ifdef LCD_I2C_SAINSMART_YWROBOT
|
||||||
|
// This uses the LiquidCrystal_I2C library ( https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home )
|
||||||
|
// Make sure it is placed in the Arduino libraries directory.
|
||||||
|
#define LCD_I2C_TYPE_PCF8575
|
||||||
|
#define LCD_I2C_ADDRESS 0x27 // I2C Address of the port expander
|
||||||
|
#define NEWPANEL
|
||||||
|
#define ULTIPANEL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// PANELOLU2 LCD with status LEDs, separate encoder and click inputs
|
||||||
|
//#define LCD_I2C_PANELOLU2
|
||||||
|
#ifdef LCD_I2C_PANELOLU2
|
||||||
|
// This uses the LiquidTWI2 library v1.2.3 or later ( https://github.com/lincomatic/LiquidTWI2 )
|
||||||
|
// Make sure the LiquidTWI2 directory is placed in the Arduino or Sketchbook libraries subdirectory.
|
||||||
|
// (v1.2.3 no longer requires you to define PANELOLU in the LiquidTWI2.h library header file)
|
||||||
|
// Note: The PANELOLU2 encoder click input can either be directly connected to a pin
|
||||||
|
// (if BTN_ENC defined to != -1) or read through I2C (when BTN_ENC == -1).
|
||||||
|
#define LCD_I2C_TYPE_MCP23017
|
||||||
|
#define LCD_I2C_ADDRESS 0x20 // I2C Address of the port expander
|
||||||
|
#define LCD_USE_I2C_BUZZER //comment out to disable buzzer on LCD
|
||||||
|
#define NEWPANEL
|
||||||
|
#define ULTIPANEL
|
||||||
|
|
||||||
|
#ifndef ENCODER_PULSES_PER_STEP
|
||||||
|
#define ENCODER_PULSES_PER_STEP 4
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ENCODER_STEPS_PER_MENU_ITEM
|
||||||
|
#define ENCODER_STEPS_PER_MENU_ITEM 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef LCD_USE_I2C_BUZZER
|
||||||
|
#define LCD_FEEDBACK_FREQUENCY_HZ 1000
|
||||||
|
#define LCD_FEEDBACK_FREQUENCY_DURATION_MS 100
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Panucatt VIKI LCD with status LEDs, integrated click & L/R/U/P buttons, separate encoder inputs
|
||||||
|
//#define LCD_I2C_VIKI
|
||||||
|
#ifdef LCD_I2C_VIKI
|
||||||
|
// This uses the LiquidTWI2 library v1.2.3 or later ( https://github.com/lincomatic/LiquidTWI2 )
|
||||||
|
// Make sure the LiquidTWI2 directory is placed in the Arduino or Sketchbook libraries subdirectory.
|
||||||
|
// Note: The pause/stop/resume LCD button pin should be connected to the Arduino
|
||||||
|
// BTN_ENC pin (or set BTN_ENC to -1 if not used)
|
||||||
|
#define LCD_I2C_TYPE_MCP23017
|
||||||
|
#define LCD_I2C_ADDRESS 0x20 // I2C Address of the port expander
|
||||||
|
#define LCD_USE_I2C_BUZZER //comment out to disable buzzer on LCD (requires LiquidTWI2 v1.2.3 or later)
|
||||||
|
#define NEWPANEL
|
||||||
|
#define ULTIPANEL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Shift register panels
|
||||||
|
// ---------------------
|
||||||
|
// 2 wire Non-latching LCD SR from:
|
||||||
|
// https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/schematics#!shiftregister-connection
|
||||||
|
//#define SR_LCD
|
||||||
|
#ifdef SR_LCD
|
||||||
|
#define SR_LCD_2W_NL // Non latching 2 wire shift register
|
||||||
|
//#define NEWPANEL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef ULTIPANEL
|
||||||
|
// #define NEWPANEL //enable this if you have a click-encoder panel
|
||||||
|
#define SDSUPPORT
|
||||||
|
#define ULTRA_LCD
|
||||||
|
#ifdef DOGLCD // Change number of lines to match the DOG graphic display
|
||||||
|
#define LCD_WIDTH 20
|
||||||
|
#define LCD_HEIGHT 5
|
||||||
|
#else
|
||||||
|
#define LCD_WIDTH 20
|
||||||
|
#define LCD_HEIGHT 4
|
||||||
|
#endif
|
||||||
|
#else //no panel but just LCD
|
||||||
|
#ifdef ULTRA_LCD
|
||||||
|
#ifdef DOGLCD // Change number of lines to match the 128x64 graphics display
|
||||||
|
#define LCD_WIDTH 20
|
||||||
|
#define LCD_HEIGHT 5
|
||||||
|
#else
|
||||||
|
#define LCD_WIDTH 16
|
||||||
|
#define LCD_HEIGHT 2
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// default LCD contrast for dogm-like LCD displays
|
||||||
|
#ifdef DOGLCD
|
||||||
|
# ifndef DEFAULT_LCD_CONTRAST
|
||||||
|
# define DEFAULT_LCD_CONTRAST 32
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Increase the FAN pwm frequency. Removes the PWM noise but increases heating in the FET/Arduino
|
||||||
|
//#define FAST_PWM_FAN
|
||||||
|
|
||||||
|
// Temperature status LEDs that display the hotend and bet temperature.
|
||||||
|
// If all hotends and bed temperature and temperature setpoint are < 54C then the BLUE led is on.
|
||||||
|
// Otherwise the RED led is on. There is 1C hysteresis.
|
||||||
|
//#define TEMP_STAT_LEDS
|
||||||
|
|
||||||
|
// Use software PWM to drive the fan, as for the heaters. This uses a very low frequency
|
||||||
|
// which is not ass annoying as with the hardware PWM. On the other hand, if this frequency
|
||||||
|
// is too low, you should also increment SOFT_PWM_SCALE.
|
||||||
|
//#define FAN_SOFT_PWM
|
||||||
|
|
||||||
|
// Incrementing this by 1 will double the software PWM frequency,
|
||||||
|
// affecting heaters, and the fan if FAN_SOFT_PWM is enabled.
|
||||||
|
// However, control resolution will be halved for each increment;
|
||||||
|
// at zero value, there are 128 effective control positions.
|
||||||
|
#define SOFT_PWM_SCALE 0
|
||||||
|
|
||||||
|
// M240 Triggers a camera by emulating a Canon RC-1 Remote
|
||||||
|
// Data from: http://www.doc-diy.net/photo/rc-1_hacked/
|
||||||
|
// #define PHOTOGRAPH_PIN 23
|
||||||
|
|
||||||
|
// SF send wrong arc g-codes when using Arc Point as fillet procedure
|
||||||
|
//#define SF_ARC_FIX
|
||||||
|
|
||||||
|
// Support for the BariCUDA Paste Extruder.
|
||||||
|
//#define BARICUDA
|
||||||
|
|
||||||
|
//define BlinkM/CyzRgb Support
|
||||||
|
//#define BLINKM
|
||||||
|
|
||||||
|
/*********************************************************************\
|
||||||
|
* R/C SERVO support
|
||||||
|
* Sponsored by TrinityLabs, Reworked by codexmas
|
||||||
|
**********************************************************************/
|
||||||
|
|
||||||
|
// Number of servos
|
||||||
|
//
|
||||||
|
// If you select a configuration below, this will receive a default value and does not need to be set manually
|
||||||
|
// set it manually if you have more servos than extruders and wish to manually control some
|
||||||
|
// leaving it undefined or defining as 0 will disable the servo subsystem
|
||||||
|
// If unsure, leave commented / disabled
|
||||||
|
//
|
||||||
|
//#define NUM_SERVOS 3 // Servo index starts with 0 for M280 command
|
||||||
|
|
||||||
|
// Servo Endstops
|
||||||
|
//
|
||||||
|
// This allows for servo actuated endstops, primary usage is for the Z Axis to eliminate calibration or bed height changes.
|
||||||
|
// Use M206 command to correct for switch height offset to actual nozzle height. Store that setting with M500.
|
||||||
|
//
|
||||||
|
|
||||||
|
#define DIGIPOT_I2C
|
||||||
|
|
||||||
|
//#define SERVO_ENDSTOPS {-1, -1, 0} // Servo index for X, Y, Z. Disable with -1
|
||||||
|
//#define SERVO_ENDSTOP_ANGLES {0,0, 0,0, 70,0} // X,Y,Z Axis Extend and Retract angles
|
||||||
|
|
||||||
|
#include "Configuration_adv.h"
|
||||||
|
#include "thermistortables.h"
|
||||||
|
|
||||||
|
#endif //__CONFIGURATION_H
|
@ -0,0 +1,498 @@
|
|||||||
|
#ifndef CONFIGURATION_ADV_H
|
||||||
|
#define CONFIGURATION_ADV_H
|
||||||
|
|
||||||
|
//===========================================================================
|
||||||
|
//=============================Thermal Settings ============================
|
||||||
|
//===========================================================================
|
||||||
|
|
||||||
|
#ifdef BED_LIMIT_SWITCHING
|
||||||
|
#define BED_HYSTERESIS 2 //only disable heating if T>target+BED_HYSTERESIS and enable heating if T>target-BED_HYSTERESIS
|
||||||
|
#endif
|
||||||
|
#define BED_CHECK_INTERVAL 5000 //ms between checks in bang-bang control
|
||||||
|
|
||||||
|
//// Heating sanity check:
|
||||||
|
// This waits for the watch period in milliseconds whenever an M104 or M109 increases the target temperature
|
||||||
|
// If the temperature has not increased at the end of that period, the target temperature is set to zero.
|
||||||
|
// It can be reset with another M104/M109. This check is also only triggered if the target temperature and the current temperature
|
||||||
|
// differ by at least 2x WATCH_TEMP_INCREASE
|
||||||
|
//#define WATCH_TEMP_PERIOD 40000 //40 seconds
|
||||||
|
//#define WATCH_TEMP_INCREASE 10 //Heat up at least 10 degree in 20 seconds
|
||||||
|
|
||||||
|
#ifdef PIDTEMP
|
||||||
|
// this adds an experimental additional term to the heating power, proportional to the extrusion speed.
|
||||||
|
// if Kc is chosen well, the additional required power due to increased melting should be compensated.
|
||||||
|
#define PID_ADD_EXTRUSION_RATE
|
||||||
|
#ifdef PID_ADD_EXTRUSION_RATE
|
||||||
|
#define DEFAULT_Kc (1) //heating power=Kc*(e_speed)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
//automatic temperature: The hot end target temperature is calculated by all the buffered lines of gcode.
|
||||||
|
//The maximum buffered steps/sec of the extruder motor are called "se".
|
||||||
|
//You enter the autotemp mode by a M109 S<mintemp> T<maxtemp> F<factor>
|
||||||
|
// the target temperature is set to mintemp+factor*se[steps/sec] and limited by mintemp and maxtemp
|
||||||
|
// you exit the value by any M109 without F*
|
||||||
|
// Also, if the temperature is set to a value <mintemp, it is not changed by autotemp.
|
||||||
|
// on an Ultimaker, some initial testing worked with M109 S215 B260 F1 in the start.gcode
|
||||||
|
#define AUTOTEMP
|
||||||
|
#ifdef AUTOTEMP
|
||||||
|
#define AUTOTEMP_OLDWEIGHT 0.98
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//Show Temperature ADC value
|
||||||
|
//The M105 command return, besides traditional information, the ADC value read from temperature sensors.
|
||||||
|
//#define SHOW_TEMP_ADC_VALUES
|
||||||
|
|
||||||
|
// extruder run-out prevention.
|
||||||
|
//if the machine is idle, and the temperature over MINTEMP, every couple of SECONDS some filament is extruded
|
||||||
|
//#define EXTRUDER_RUNOUT_PREVENT
|
||||||
|
#define EXTRUDER_RUNOUT_MINTEMP 190
|
||||||
|
#define EXTRUDER_RUNOUT_SECONDS 30.
|
||||||
|
#define EXTRUDER_RUNOUT_ESTEPS 14. //mm filament
|
||||||
|
#define EXTRUDER_RUNOUT_SPEED 1500. //extrusion speed
|
||||||
|
#define EXTRUDER_RUNOUT_EXTRUDE 100
|
||||||
|
|
||||||
|
//These defines help to calibrate the AD595 sensor in case you get wrong temperature measurements.
|
||||||
|
//The measured temperature is defined as "actualTemp = (measuredTemp * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET"
|
||||||
|
#define TEMP_SENSOR_AD595_OFFSET 0.0
|
||||||
|
#define TEMP_SENSOR_AD595_GAIN 1.0
|
||||||
|
|
||||||
|
//This is for controlling a fan to cool down the stepper drivers
|
||||||
|
//it will turn on when any driver is enabled
|
||||||
|
//and turn off after the set amount of seconds from last driver being disabled again
|
||||||
|
#define CONTROLLERFAN_PIN -1 //Pin used for the fan to cool controller (-1 to disable)
|
||||||
|
#define CONTROLLERFAN_SECS 60 //How many seconds, after all motors were disabled, the fan should run
|
||||||
|
#define CONTROLLERFAN_SPEED 255 // == full speed
|
||||||
|
|
||||||
|
// When first starting the main fan, run it at full speed for the
|
||||||
|
// given number of milliseconds. This gets the fan spinning reliably
|
||||||
|
// before setting a PWM value. (Does not work with software PWM for fan on Sanguinololu)
|
||||||
|
//#define FAN_KICKSTART_TIME 100
|
||||||
|
|
||||||
|
// Extruder cooling fans
|
||||||
|
// Configure fan pin outputs to automatically turn on/off when the associated
|
||||||
|
// extruder temperature is above/below EXTRUDER_AUTO_FAN_TEMPERATURE.
|
||||||
|
// Multiple extruders can be assigned to the same pin in which case
|
||||||
|
// the fan will turn on when any selected extruder is above the threshold.
|
||||||
|
#define EXTRUDER_0_AUTO_FAN_PIN -1
|
||||||
|
#define EXTRUDER_1_AUTO_FAN_PIN -1
|
||||||
|
#define EXTRUDER_2_AUTO_FAN_PIN -1
|
||||||
|
#define EXTRUDER_AUTO_FAN_TEMPERATURE 50
|
||||||
|
#define EXTRUDER_AUTO_FAN_SPEED 255 // == full speed
|
||||||
|
|
||||||
|
|
||||||
|
//===========================================================================
|
||||||
|
//=============================Mechanical Settings===========================
|
||||||
|
//===========================================================================
|
||||||
|
|
||||||
|
#define ENDSTOPS_ONLY_FOR_HOMING // If defined the endstops will only be used for homing
|
||||||
|
|
||||||
|
|
||||||
|
//// AUTOSET LOCATIONS OF LIMIT SWITCHES
|
||||||
|
//// Added by ZetaPhoenix 09-15-2012
|
||||||
|
#ifdef MANUAL_HOME_POSITIONS // Use manual limit switch locations
|
||||||
|
#define X_HOME_POS MANUAL_X_HOME_POS
|
||||||
|
#define Y_HOME_POS MANUAL_Y_HOME_POS
|
||||||
|
#define Z_HOME_POS MANUAL_Z_HOME_POS
|
||||||
|
#else //Set min/max homing switch positions based upon homing direction and min/max travel limits
|
||||||
|
//X axis
|
||||||
|
#if X_HOME_DIR == -1
|
||||||
|
#ifdef BED_CENTER_AT_0_0
|
||||||
|
#define X_HOME_POS X_MAX_LENGTH * -0.5
|
||||||
|
#else
|
||||||
|
#define X_HOME_POS X_MIN_POS
|
||||||
|
#endif //BED_CENTER_AT_0_0
|
||||||
|
#else
|
||||||
|
#ifdef BED_CENTER_AT_0_0
|
||||||
|
#define X_HOME_POS X_MAX_LENGTH * 0.5
|
||||||
|
#else
|
||||||
|
#define X_HOME_POS X_MAX_POS
|
||||||
|
#endif //BED_CENTER_AT_0_0
|
||||||
|
#endif //X_HOME_DIR == -1
|
||||||
|
|
||||||
|
//Y axis
|
||||||
|
#if Y_HOME_DIR == -1
|
||||||
|
#ifdef BED_CENTER_AT_0_0
|
||||||
|
#define Y_HOME_POS Y_MAX_LENGTH * -0.5
|
||||||
|
#else
|
||||||
|
#define Y_HOME_POS Y_MIN_POS
|
||||||
|
#endif //BED_CENTER_AT_0_0
|
||||||
|
#else
|
||||||
|
#ifdef BED_CENTER_AT_0_0
|
||||||
|
#define Y_HOME_POS Y_MAX_LENGTH * 0.5
|
||||||
|
#else
|
||||||
|
#define Y_HOME_POS Y_MAX_POS
|
||||||
|
#endif //BED_CENTER_AT_0_0
|
||||||
|
#endif //Y_HOME_DIR == -1
|
||||||
|
|
||||||
|
// Z axis
|
||||||
|
#if Z_HOME_DIR == -1 //BED_CENTER_AT_0_0 not used
|
||||||
|
#define Z_HOME_POS Z_MIN_POS
|
||||||
|
#else
|
||||||
|
#define Z_HOME_POS Z_MAX_POS
|
||||||
|
#endif //Z_HOME_DIR == -1
|
||||||
|
#endif //End auto min/max positions
|
||||||
|
//END AUTOSET LOCATIONS OF LIMIT SWITCHES -ZP
|
||||||
|
|
||||||
|
|
||||||
|
//#define Z_LATE_ENABLE // Enable Z the last moment. Needed if your Z driver overheats.
|
||||||
|
|
||||||
|
// A single Z stepper driver is usually used to drive 2 stepper motors.
|
||||||
|
// Uncomment this define to utilize a separate stepper driver for each Z axis motor.
|
||||||
|
// Only a few motherboards support this, like RAMPS, which have dual extruder support (the 2nd, often unused, extruder driver is used
|
||||||
|
// to control the 2nd Z axis stepper motor). The pins are currently only defined for a RAMPS motherboards.
|
||||||
|
// On a RAMPS (or other 5 driver) motherboard, using this feature will limit you to using 1 extruder.
|
||||||
|
//#define Z_DUAL_STEPPER_DRIVERS
|
||||||
|
|
||||||
|
#ifdef Z_DUAL_STEPPER_DRIVERS
|
||||||
|
#undef EXTRUDERS
|
||||||
|
#define EXTRUDERS 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Same again but for Y Axis.
|
||||||
|
//#define Y_DUAL_STEPPER_DRIVERS
|
||||||
|
|
||||||
|
// Define if the two Y drives need to rotate in opposite directions
|
||||||
|
#define INVERT_Y2_VS_Y_DIR true
|
||||||
|
|
||||||
|
#ifdef Y_DUAL_STEPPER_DRIVERS
|
||||||
|
#undef EXTRUDERS
|
||||||
|
#define EXTRUDERS 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined (Z_DUAL_STEPPER_DRIVERS) && defined (Y_DUAL_STEPPER_DRIVERS)
|
||||||
|
#error "You cannot have dual drivers for both Y and Z"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Enable this for dual x-carriage printers.
|
||||||
|
// A dual x-carriage design has the advantage that the inactive extruder can be parked which
|
||||||
|
// prevents hot-end ooze contaminating the print. It also reduces the weight of each x-carriage
|
||||||
|
// allowing faster printing speeds.
|
||||||
|
//#define DUAL_X_CARRIAGE
|
||||||
|
#ifdef DUAL_X_CARRIAGE
|
||||||
|
// Configuration for second X-carriage
|
||||||
|
// Note: the first x-carriage is defined as the x-carriage which homes to the minimum endstop;
|
||||||
|
// the second x-carriage always homes to the maximum endstop.
|
||||||
|
#define X2_MIN_POS 80 // set minimum to ensure second x-carriage doesn't hit the parked first X-carriage
|
||||||
|
#define X2_MAX_POS 353 // set maximum to the distance between toolheads when both heads are homed
|
||||||
|
#define X2_HOME_DIR 1 // the second X-carriage always homes to the maximum endstop position
|
||||||
|
#define X2_HOME_POS X2_MAX_POS // default home position is the maximum carriage position
|
||||||
|
// However: In this mode the EXTRUDER_OFFSET_X value for the second extruder provides a software
|
||||||
|
// override for X2_HOME_POS. This also allow recalibration of the distance between the two endstops
|
||||||
|
// without modifying the firmware (through the "M218 T1 X???" command).
|
||||||
|
// Remember: you should set the second extruder x-offset to 0 in your slicer.
|
||||||
|
|
||||||
|
// Pins for second x-carriage stepper driver (defined here to avoid further complicating pins.h)
|
||||||
|
#define X2_ENABLE_PIN 29
|
||||||
|
#define X2_STEP_PIN 25
|
||||||
|
#define X2_DIR_PIN 23
|
||||||
|
|
||||||
|
// There are a few selectable movement modes for dual x-carriages using M605 S<mode>
|
||||||
|
// Mode 0: Full control. The slicer has full control over both x-carriages and can achieve optimal travel results
|
||||||
|
// as long as it supports dual x-carriages. (M605 S0)
|
||||||
|
// Mode 1: Auto-park mode. The firmware will automatically park and unpark the x-carriages on tool changes so
|
||||||
|
// that additional slicer support is not required. (M605 S1)
|
||||||
|
// Mode 2: Duplication mode. The firmware will transparently make the second x-carriage and extruder copy all
|
||||||
|
// actions of the first x-carriage. This allows the printer to print 2 arbitrary items at
|
||||||
|
// once. (2nd extruder x offset and temp offset are set using: M605 S2 [Xnnn] [Rmmm])
|
||||||
|
|
||||||
|
// This is the default power-up mode which can be later using M605.
|
||||||
|
#define DEFAULT_DUAL_X_CARRIAGE_MODE 0
|
||||||
|
|
||||||
|
// As the x-carriages are independent we can now account for any relative Z offset
|
||||||
|
#define EXTRUDER1_Z_OFFSET 0.0 // z offset relative to extruder 0
|
||||||
|
|
||||||
|
// Default settings in "Auto-park Mode"
|
||||||
|
#define TOOLCHANGE_PARK_ZLIFT 0.2 // the distance to raise Z axis when parking an extruder
|
||||||
|
#define TOOLCHANGE_UNPARK_ZLIFT 1 // the distance to raise Z axis when unparking an extruder
|
||||||
|
|
||||||
|
// Default x offset in duplication mode (typically set to half print bed width)
|
||||||
|
#define DEFAULT_DUPLICATION_X_OFFSET 100
|
||||||
|
|
||||||
|
#endif //DUAL_X_CARRIAGE
|
||||||
|
|
||||||
|
//homing hits the endstop, then retracts by this distance, before it tries to slowly bump again:
|
||||||
|
#define X_HOME_RETRACT_MM 5
|
||||||
|
#define Y_HOME_RETRACT_MM 5
|
||||||
|
#define Z_HOME_RETRACT_MM 2
|
||||||
|
//#define QUICK_HOME //if this is defined, if both x and y are to be homed, a diagonal move will be performed initially.
|
||||||
|
|
||||||
|
#define AXIS_RELATIVE_MODES {false, false, false, false}
|
||||||
|
|
||||||
|
#define MAX_STEP_FREQUENCY 40000 // Max step frequency for Ultimaker (5000 pps / half step)
|
||||||
|
|
||||||
|
//By default pololu step drivers require an active high signal. However, some high power drivers require an active low signal as step.
|
||||||
|
#define INVERT_X_STEP_PIN false
|
||||||
|
#define INVERT_Y_STEP_PIN false
|
||||||
|
#define INVERT_Z_STEP_PIN false
|
||||||
|
#define INVERT_E_STEP_PIN false
|
||||||
|
|
||||||
|
//default stepper release if idle
|
||||||
|
#define DEFAULT_STEPPER_DEACTIVE_TIME 60
|
||||||
|
|
||||||
|
#define DEFAULT_MINIMUMFEEDRATE 0.0 // minimum feedrate
|
||||||
|
#define DEFAULT_MINTRAVELFEEDRATE 0.0
|
||||||
|
|
||||||
|
// Feedrates for manual moves along X, Y, Z, E from panel
|
||||||
|
#ifdef ULTIPANEL
|
||||||
|
#define MANUAL_FEEDRATE {50*60, 50*60, 4*60, 60} // set the speeds for manual moves (mm/min)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//Comment to disable setting feedrate multiplier via encoder
|
||||||
|
#ifdef ULTIPANEL
|
||||||
|
#define ULTIPANEL_FEEDMULTIPLY
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// minimum time in microseconds that a movement needs to take if the buffer is emptied.
|
||||||
|
#define DEFAULT_MINSEGMENTTIME 20000
|
||||||
|
|
||||||
|
// If defined the movements slow down when the look ahead buffer is only half full
|
||||||
|
#define SLOWDOWN
|
||||||
|
|
||||||
|
// Frequency limit
|
||||||
|
// See nophead's blog for more info
|
||||||
|
// Not working O
|
||||||
|
//#define XY_FREQUENCY_LIMIT 15
|
||||||
|
|
||||||
|
// Minimum planner junction speed. Sets the default minimum speed the planner plans for at the end
|
||||||
|
// of the buffer and all stops. This should not be much greater than zero and should only be changed
|
||||||
|
// if unwanted behavior is observed on a user's machine when running at very slow speeds.
|
||||||
|
#define MINIMUM_PLANNER_SPEED 0.05// (mm/sec)
|
||||||
|
|
||||||
|
// MS1 MS2 Stepper Driver Microstepping mode table
|
||||||
|
#define MICROSTEP1 LOW,LOW
|
||||||
|
#define MICROSTEP2 HIGH,LOW
|
||||||
|
#define MICROSTEP4 LOW,HIGH
|
||||||
|
#define MICROSTEP8 HIGH,HIGH
|
||||||
|
#define MICROSTEP16 HIGH,HIGH
|
||||||
|
|
||||||
|
// Microstep setting (Only functional when stepper driver microstep pins are connected to MCU.
|
||||||
|
#define MICROSTEP_MODES {16,16,16,16,16} // [1,2,4,8,16]
|
||||||
|
|
||||||
|
// Motor Current setting (Only functional when motor driver current ref pins are connected to a digital trimpot on supported boards)
|
||||||
|
#define DIGIPOT_MOTOR_CURRENT {135,135,135,135,135} // Values 0-255 (RAMBO 135 = ~0.75A, 185 = ~1A)
|
||||||
|
|
||||||
|
// uncomment to enable an I2C based DIGIPOT like on the Azteeg X3 Pro
|
||||||
|
//#define DIGIPOT_I2C
|
||||||
|
// Number of channels available for I2C digipot, For Azteeg X3 Pro we have 8
|
||||||
|
#define DIGIPOT_I2C_NUM_CHANNELS 4
|
||||||
|
// actual motor currents in Amps, need as many here as DIGIPOT_I2C_NUM_CHANNELS
|
||||||
|
//#define DIGIPOT_I2C_MOTOR_CURRENTS {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}
|
||||||
|
#define DIGIPOT_I2C_MOTOR_CURRENTS {1.7, 1.7, 1.7, 1.7}
|
||||||
|
|
||||||
|
//===========================================================================
|
||||||
|
//=============================Additional Features===========================
|
||||||
|
//===========================================================================
|
||||||
|
|
||||||
|
//#define CHDK 4 //Pin for triggering CHDK to take a picture see how to use it here http://captain-slow.dk/2014/03/09/3d-printing-timelapses/
|
||||||
|
#define CHDK_DELAY 50 //How long in ms the pin should stay HIGH before going LOW again
|
||||||
|
|
||||||
|
#define SD_FINISHED_STEPPERRELEASE true //if sd support and the file is finished: disable steppers?
|
||||||
|
#define SD_FINISHED_RELEASECOMMAND "M84 X Y Z E" // You might want to keep the z enabled so your bed stays in place.
|
||||||
|
|
||||||
|
#define SDCARD_RATHERRECENTFIRST //reverse file order of sd card menu display. Its sorted practically after the file system block order.
|
||||||
|
// if a file is deleted, it frees a block. hence, the order is not purely chronological. To still have auto0.g accessible, there is again the option to do that.
|
||||||
|
// using:
|
||||||
|
//#define MENU_ADDAUTOSTART
|
||||||
|
|
||||||
|
// The hardware watchdog should reset the microcontroller disabling all outputs, in case the firmware gets stuck and doesn't do temperature regulation.
|
||||||
|
//#define USE_WATCHDOG
|
||||||
|
|
||||||
|
#ifdef USE_WATCHDOG
|
||||||
|
// If you have a watchdog reboot in an ArduinoMega2560 then the device will hang forever, as a watchdog reset will leave the watchdog on.
|
||||||
|
// The "WATCHDOG_RESET_MANUAL" goes around this by not using the hardware reset.
|
||||||
|
// However, THIS FEATURE IS UNSAFE!, as it will only work if interrupts are disabled. And the code could hang in an interrupt routine with interrupts disabled.
|
||||||
|
//#define WATCHDOG_RESET_MANUAL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Enable the option to stop SD printing when hitting and endstops, needs to be enabled from the LCD menu when this option is enabled.
|
||||||
|
//#define ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
|
||||||
|
|
||||||
|
// Babystepping enables the user to control the axis in tiny amounts, independently from the normal printing process
|
||||||
|
// it can e.g. be used to change z-positions in the print startup phase in real-time
|
||||||
|
// does not respect endstops!
|
||||||
|
//#define BABYSTEPPING
|
||||||
|
#ifdef BABYSTEPPING
|
||||||
|
#define BABYSTEP_XY //not only z, but also XY in the menu. more clutter, more functions
|
||||||
|
#define BABYSTEP_INVERT_Z false //true for inverse movements in Z
|
||||||
|
#define BABYSTEP_Z_MULTIPLICATOR 2 //faster z movements
|
||||||
|
|
||||||
|
#ifdef COREXY
|
||||||
|
#error BABYSTEPPING not implemented for COREXY yet.
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef DELTA
|
||||||
|
#ifdef BABYSTEP_XY
|
||||||
|
#error BABYSTEPPING only implemented for Z axis on deltabots.
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// extruder advance constant (s2/mm3)
|
||||||
|
//
|
||||||
|
// advance (steps) = STEPS_PER_CUBIC_MM_E * EXTUDER_ADVANCE_K * cubic mm per second ^ 2
|
||||||
|
//
|
||||||
|
// Hooke's law says: force = k * distance
|
||||||
|
// Bernoulli's principle says: v ^ 2 / 2 + g . h + pressure / density = constant
|
||||||
|
// so: v ^ 2 is proportional to number of steps we advance the extruder
|
||||||
|
//#define ADVANCE
|
||||||
|
|
||||||
|
#ifdef ADVANCE
|
||||||
|
#define EXTRUDER_ADVANCE_K .0
|
||||||
|
|
||||||
|
#define D_FILAMENT 2.85
|
||||||
|
#define STEPS_MM_E 836
|
||||||
|
#define EXTRUTION_AREA (0.25 * D_FILAMENT * D_FILAMENT * 3.14159)
|
||||||
|
#define STEPS_PER_CUBIC_MM_E (axis_steps_per_unit[E_AXIS]/ EXTRUTION_AREA)
|
||||||
|
|
||||||
|
#endif // ADVANCE
|
||||||
|
|
||||||
|
// Arc interpretation settings:
|
||||||
|
#define MM_PER_ARC_SEGMENT 1
|
||||||
|
#define N_ARC_CORRECTION 25
|
||||||
|
|
||||||
|
const unsigned int dropsegments=5; //everything with less than this number of steps will be ignored as move and joined with the next movement
|
||||||
|
|
||||||
|
// If you are using a RAMPS board or cheap E-bay purchased boards that do not detect when an SD card is inserted
|
||||||
|
// You can get round this by connecting a push button or single throw switch to the pin defined as SDCARDCARDDETECT
|
||||||
|
// in the pins.h file. When using a push button pulling the pin to ground this will need inverted. This setting should
|
||||||
|
// be commented out otherwise
|
||||||
|
//#define SDCARDDETECTINVERTED
|
||||||
|
|
||||||
|
#ifdef ULTIPANEL
|
||||||
|
#undef SDCARDDETECTINVERTED
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Power Signal Control Definitions
|
||||||
|
// By default use ATX definition
|
||||||
|
#ifndef POWER_SUPPLY
|
||||||
|
#define POWER_SUPPLY 1
|
||||||
|
#endif
|
||||||
|
// 1 = ATX
|
||||||
|
#if (POWER_SUPPLY == 1)
|
||||||
|
#define PS_ON_AWAKE LOW
|
||||||
|
#define PS_ON_ASLEEP HIGH
|
||||||
|
#endif
|
||||||
|
// 2 = X-Box 360 203W
|
||||||
|
#if (POWER_SUPPLY == 2)
|
||||||
|
#define PS_ON_AWAKE HIGH
|
||||||
|
#define PS_ON_ASLEEP LOW
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Control heater 0 and heater 1 in parallel.
|
||||||
|
//#define HEATERS_PARALLEL
|
||||||
|
|
||||||
|
//===========================================================================
|
||||||
|
//=============================Buffers ============================
|
||||||
|
//===========================================================================
|
||||||
|
|
||||||
|
// The number of linear motions that can be in the plan at any give time.
|
||||||
|
// THE BLOCK_BUFFER_SIZE NEEDS TO BE A POWER OF 2, i.g. 8,16,32 because shifts and ors are used to do the ring-buffering.
|
||||||
|
#if defined SDSUPPORT
|
||||||
|
#define BLOCK_BUFFER_SIZE 16 // SD,LCD,Buttons take more memory, block buffer needs to be smaller
|
||||||
|
#else
|
||||||
|
#define BLOCK_BUFFER_SIZE 16 // maximize block buffer
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
//The ASCII buffer for receiving from the serial:
|
||||||
|
#define MAX_CMD_SIZE 96
|
||||||
|
#define BUFSIZE 4
|
||||||
|
|
||||||
|
|
||||||
|
// Firmware based and LCD controlled retract
|
||||||
|
// M207 and M208 can be used to define parameters for the retraction.
|
||||||
|
// The retraction can be called by the slicer using G10 and G11
|
||||||
|
// until then, intended retractions can be detected by moves that only extrude and the direction.
|
||||||
|
// the moves are than replaced by the firmware controlled ones.
|
||||||
|
|
||||||
|
// #define FWRETRACT //ONLY PARTIALLY TESTED
|
||||||
|
#ifdef FWRETRACT
|
||||||
|
#define MIN_RETRACT 0.1 //minimum extruded mm to accept a automatic gcode retraction attempt
|
||||||
|
#define RETRACT_LENGTH 3 //default retract length (positive mm)
|
||||||
|
#define RETRACT_FEEDRATE 45 //default feedrate for retracting (mm/s)
|
||||||
|
#define RETRACT_ZLIFT 0 //default retract Z-lift
|
||||||
|
#define RETRACT_RECOVER_LENGTH 0 //default additional recover length (mm, added to retract length when recovering)
|
||||||
|
#define RETRACT_RECOVER_FEEDRATE 8 //default feedrate for recovering from retraction (mm/s)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//adds support for experimental filament exchange support M600; requires display
|
||||||
|
#ifdef ULTIPANEL
|
||||||
|
#define FILAMENTCHANGEENABLE
|
||||||
|
#ifdef FILAMENTCHANGEENABLE
|
||||||
|
#define FILAMENTCHANGE_XPOS 3
|
||||||
|
#define FILAMENTCHANGE_YPOS 3
|
||||||
|
#define FILAMENTCHANGE_ZADD 10
|
||||||
|
#define FILAMENTCHANGE_FIRSTRETRACT -2
|
||||||
|
#define FILAMENTCHANGE_FINALRETRACT -100
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef FILAMENTCHANGEENABLE
|
||||||
|
#ifdef EXTRUDER_RUNOUT_PREVENT
|
||||||
|
#error EXTRUDER_RUNOUT_PREVENT currently incompatible with FILAMENTCHANGE
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//===========================================================================
|
||||||
|
//============================= Define Defines ============================
|
||||||
|
//===========================================================================
|
||||||
|
#if EXTRUDERS > 1 && defined TEMP_SENSOR_1_AS_REDUNDANT
|
||||||
|
#error "You cannot use TEMP_SENSOR_1_AS_REDUNDANT if EXTRUDERS > 1"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if EXTRUDERS > 1 && defined HEATERS_PARALLEL
|
||||||
|
#error "You cannot use HEATERS_PARALLEL if EXTRUDERS > 1"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if TEMP_SENSOR_0 > 0
|
||||||
|
#define THERMISTORHEATER_0 TEMP_SENSOR_0
|
||||||
|
#define HEATER_0_USES_THERMISTOR
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_1 > 0
|
||||||
|
#define THERMISTORHEATER_1 TEMP_SENSOR_1
|
||||||
|
#define HEATER_1_USES_THERMISTOR
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_2 > 0
|
||||||
|
#define THERMISTORHEATER_2 TEMP_SENSOR_2
|
||||||
|
#define HEATER_2_USES_THERMISTOR
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_BED > 0
|
||||||
|
#define THERMISTORBED TEMP_SENSOR_BED
|
||||||
|
#define BED_USES_THERMISTOR
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_0 == -1
|
||||||
|
#define HEATER_0_USES_AD595
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_1 == -1
|
||||||
|
#define HEATER_1_USES_AD595
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_2 == -1
|
||||||
|
#define HEATER_2_USES_AD595
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_BED == -1
|
||||||
|
#define BED_USES_AD595
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_0 == -2
|
||||||
|
#define HEATER_0_USES_MAX6675
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_0 == 0
|
||||||
|
#undef HEATER_0_MINTEMP
|
||||||
|
#undef HEATER_0_MAXTEMP
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_1 == 0
|
||||||
|
#undef HEATER_1_MINTEMP
|
||||||
|
#undef HEATER_1_MAXTEMP
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_2 == 0
|
||||||
|
#undef HEATER_2_MINTEMP
|
||||||
|
#undef HEATER_2_MAXTEMP
|
||||||
|
#endif
|
||||||
|
#if TEMP_SENSOR_BED == 0
|
||||||
|
#undef BED_MINTEMP
|
||||||
|
#undef BED_MAXTEMP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif //__CONFIGURATION_ADV_H
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,1930 @@
|
|||||||
|
#include "qr_solve.h"
|
||||||
|
|
||||||
|
#ifdef AUTO_BED_LEVELING_GRID
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
//# include "r8lib.h"
|
||||||
|
|
||||||
|
int i4_min ( int i1, int i2 )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
I4_MIN returns the smaller of two I4's.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
29 August 2006
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
John Burkardt
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, int I1, I2, two integers to be compared.
|
||||||
|
|
||||||
|
Output, int I4_MIN, the smaller of I1 and I2.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
|
||||||
|
if ( i1 < i2 )
|
||||||
|
{
|
||||||
|
value = i1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = i2;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
double r8_epsilon ( void )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
R8_EPSILON returns the R8 round off unit.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
R8_EPSILON is a number R which is a power of 2 with the property that,
|
||||||
|
to the precision of the computer's arithmetic,
|
||||||
|
1 < 1 + R
|
||||||
|
but
|
||||||
|
1 = ( 1 + R / 2 )
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
01 September 2012
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
John Burkardt
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Output, double R8_EPSILON, the R8 round-off unit.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
const double value = 2.220446049250313E-016;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
double r8_max ( double x, double y )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
R8_MAX returns the maximum of two R8's.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
07 May 2006
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
John Burkardt
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, double X, Y, the quantities to compare.
|
||||||
|
|
||||||
|
Output, double R8_MAX, the maximum of X and Y.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
double value;
|
||||||
|
|
||||||
|
if ( y < x )
|
||||||
|
{
|
||||||
|
value = x;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = y;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
double r8_abs ( double x )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
R8_ABS returns the absolute value of an R8.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
07 May 2006
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
John Burkardt
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, double X, the quantity whose absolute value is desired.
|
||||||
|
|
||||||
|
Output, double R8_ABS, the absolute value of X.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
double value;
|
||||||
|
|
||||||
|
if ( 0.0 <= x )
|
||||||
|
{
|
||||||
|
value = + x;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = - x;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
double r8_sign ( double x )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
R8_SIGN returns the sign of an R8.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
08 May 2006
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
John Burkardt
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, double X, the number whose sign is desired.
|
||||||
|
|
||||||
|
Output, double R8_SIGN, the sign of X.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
double value;
|
||||||
|
|
||||||
|
if ( x < 0.0 )
|
||||||
|
{
|
||||||
|
value = - 1.0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = + 1.0;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
double r8mat_amax ( int m, int n, double a[] )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
R8MAT_AMAX returns the maximum absolute value entry of an R8MAT.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
|
||||||
|
in column-major order.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
07 September 2012
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
John Burkardt
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, int M, the number of rows in A.
|
||||||
|
|
||||||
|
Input, int N, the number of columns in A.
|
||||||
|
|
||||||
|
Input, double A[M*N], the M by N matrix.
|
||||||
|
|
||||||
|
Output, double R8MAT_AMAX, the maximum absolute value entry of A.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
double value;
|
||||||
|
|
||||||
|
value = r8_abs ( a[0+0*m] );
|
||||||
|
|
||||||
|
for ( j = 0; j < n; j++ )
|
||||||
|
{
|
||||||
|
for ( i = 0; i < m; i++ )
|
||||||
|
{
|
||||||
|
if ( value < r8_abs ( a[i+j*m] ) )
|
||||||
|
{
|
||||||
|
value = r8_abs ( a[i+j*m] );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
double *r8mat_copy_new ( int m, int n, double a1[] )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
R8MAT_COPY_NEW copies one R8MAT to a "new" R8MAT.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
|
||||||
|
in column-major order.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
26 July 2008
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
John Burkardt
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, int M, N, the number of rows and columns.
|
||||||
|
|
||||||
|
Input, double A1[M*N], the matrix to be copied.
|
||||||
|
|
||||||
|
Output, double R8MAT_COPY_NEW[M*N], the copy of A1.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
double *a2;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
a2 = ( double * ) malloc ( m * n * sizeof ( double ) );
|
||||||
|
|
||||||
|
for ( j = 0; j < n; j++ )
|
||||||
|
{
|
||||||
|
for ( i = 0; i < m; i++ )
|
||||||
|
{
|
||||||
|
a2[i+j*m] = a1[i+j*m];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return a2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
void daxpy ( int n, double da, double dx[], int incx, double dy[], int incy )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DAXPY computes constant times a vector plus a vector.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
This routine uses unrolled loops for increments equal to one.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
30 March 2007
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
|
||||||
|
LINPACK User's Guide,
|
||||||
|
SIAM, 1979.
|
||||||
|
|
||||||
|
Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
|
||||||
|
Basic Linear Algebra Subprograms for Fortran Usage,
|
||||||
|
Algorithm 539,
|
||||||
|
ACM Transactions on Mathematical Software,
|
||||||
|
Volume 5, Number 3, September 1979, pages 308-323.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, int N, the number of elements in DX and DY.
|
||||||
|
|
||||||
|
Input, double DA, the multiplier of DX.
|
||||||
|
|
||||||
|
Input, double DX[*], the first vector.
|
||||||
|
|
||||||
|
Input, int INCX, the increment between successive entries of DX.
|
||||||
|
|
||||||
|
Input/output, double DY[*], the second vector.
|
||||||
|
On output, DY[*] has been replaced by DY[*] + DA * DX[*].
|
||||||
|
|
||||||
|
Input, int INCY, the increment between successive entries of DY.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int ix;
|
||||||
|
int iy;
|
||||||
|
int m;
|
||||||
|
|
||||||
|
if ( n <= 0 )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( da == 0.0 )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Code for unequal increments or equal increments
|
||||||
|
not equal to 1.
|
||||||
|
*/
|
||||||
|
if ( incx != 1 || incy != 1 )
|
||||||
|
{
|
||||||
|
if ( 0 <= incx )
|
||||||
|
{
|
||||||
|
ix = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ix = ( - n + 1 ) * incx;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 0 <= incy )
|
||||||
|
{
|
||||||
|
iy = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
iy = ( - n + 1 ) * incy;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = 0; i < n; i++ )
|
||||||
|
{
|
||||||
|
dy[iy] = dy[iy] + da * dx[ix];
|
||||||
|
ix = ix + incx;
|
||||||
|
iy = iy + incy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Code for both increments equal to 1.
|
||||||
|
*/
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m = n % 4;
|
||||||
|
|
||||||
|
for ( i = 0; i < m; i++ )
|
||||||
|
{
|
||||||
|
dy[i] = dy[i] + da * dx[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = m; i < n; i = i + 4 )
|
||||||
|
{
|
||||||
|
dy[i ] = dy[i ] + da * dx[i ];
|
||||||
|
dy[i+1] = dy[i+1] + da * dx[i+1];
|
||||||
|
dy[i+2] = dy[i+2] + da * dx[i+2];
|
||||||
|
dy[i+3] = dy[i+3] + da * dx[i+3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
double ddot ( int n, double dx[], int incx, double dy[], int incy )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DDOT forms the dot product of two vectors.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
This routine uses unrolled loops for increments equal to one.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
30 March 2007
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
|
||||||
|
LINPACK User's Guide,
|
||||||
|
SIAM, 1979.
|
||||||
|
|
||||||
|
Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
|
||||||
|
Basic Linear Algebra Subprograms for Fortran Usage,
|
||||||
|
Algorithm 539,
|
||||||
|
ACM Transactions on Mathematical Software,
|
||||||
|
Volume 5, Number 3, September 1979, pages 308-323.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, int N, the number of entries in the vectors.
|
||||||
|
|
||||||
|
Input, double DX[*], the first vector.
|
||||||
|
|
||||||
|
Input, int INCX, the increment between successive entries in DX.
|
||||||
|
|
||||||
|
Input, double DY[*], the second vector.
|
||||||
|
|
||||||
|
Input, int INCY, the increment between successive entries in DY.
|
||||||
|
|
||||||
|
Output, double DDOT, the sum of the product of the corresponding
|
||||||
|
entries of DX and DY.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
double dtemp;
|
||||||
|
int i;
|
||||||
|
int ix;
|
||||||
|
int iy;
|
||||||
|
int m;
|
||||||
|
|
||||||
|
dtemp = 0.0;
|
||||||
|
|
||||||
|
if ( n <= 0 )
|
||||||
|
{
|
||||||
|
return dtemp;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Code for unequal increments or equal increments
|
||||||
|
not equal to 1.
|
||||||
|
*/
|
||||||
|
if ( incx != 1 || incy != 1 )
|
||||||
|
{
|
||||||
|
if ( 0 <= incx )
|
||||||
|
{
|
||||||
|
ix = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ix = ( - n + 1 ) * incx;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 0 <= incy )
|
||||||
|
{
|
||||||
|
iy = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
iy = ( - n + 1 ) * incy;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = 0; i < n; i++ )
|
||||||
|
{
|
||||||
|
dtemp = dtemp + dx[ix] * dy[iy];
|
||||||
|
ix = ix + incx;
|
||||||
|
iy = iy + incy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Code for both increments equal to 1.
|
||||||
|
*/
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m = n % 5;
|
||||||
|
|
||||||
|
for ( i = 0; i < m; i++ )
|
||||||
|
{
|
||||||
|
dtemp = dtemp + dx[i] * dy[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = m; i < n; i = i + 5 )
|
||||||
|
{
|
||||||
|
dtemp = dtemp + dx[i ] * dy[i ]
|
||||||
|
+ dx[i+1] * dy[i+1]
|
||||||
|
+ dx[i+2] * dy[i+2]
|
||||||
|
+ dx[i+3] * dy[i+3]
|
||||||
|
+ dx[i+4] * dy[i+4];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dtemp;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
double dnrm2 ( int n, double x[], int incx )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DNRM2 returns the euclidean norm of a vector.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
DNRM2 ( X ) = sqrt ( X' * X )
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
30 March 2007
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
|
||||||
|
LINPACK User's Guide,
|
||||||
|
SIAM, 1979.
|
||||||
|
|
||||||
|
Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
|
||||||
|
Basic Linear Algebra Subprograms for Fortran Usage,
|
||||||
|
Algorithm 539,
|
||||||
|
ACM Transactions on Mathematical Software,
|
||||||
|
Volume 5, Number 3, September 1979, pages 308-323.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, int N, the number of entries in the vector.
|
||||||
|
|
||||||
|
Input, double X[*], the vector whose norm is to be computed.
|
||||||
|
|
||||||
|
Input, int INCX, the increment between successive entries of X.
|
||||||
|
|
||||||
|
Output, double DNRM2, the Euclidean norm of X.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
double absxi;
|
||||||
|
int i;
|
||||||
|
int ix;
|
||||||
|
double norm;
|
||||||
|
double scale;
|
||||||
|
double ssq;
|
||||||
|
double value;
|
||||||
|
|
||||||
|
if ( n < 1 || incx < 1 )
|
||||||
|
{
|
||||||
|
norm = 0.0;
|
||||||
|
}
|
||||||
|
else if ( n == 1 )
|
||||||
|
{
|
||||||
|
norm = r8_abs ( x[0] );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
scale = 0.0;
|
||||||
|
ssq = 1.0;
|
||||||
|
ix = 0;
|
||||||
|
|
||||||
|
for ( i = 0; i < n; i++ )
|
||||||
|
{
|
||||||
|
if ( x[ix] != 0.0 )
|
||||||
|
{
|
||||||
|
absxi = r8_abs ( x[ix] );
|
||||||
|
if ( scale < absxi )
|
||||||
|
{
|
||||||
|
ssq = 1.0 + ssq * ( scale / absxi ) * ( scale / absxi );
|
||||||
|
scale = absxi;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ssq = ssq + ( absxi / scale ) * ( absxi / scale );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ix = ix + incx;
|
||||||
|
}
|
||||||
|
|
||||||
|
norm = scale * sqrt ( ssq );
|
||||||
|
}
|
||||||
|
|
||||||
|
return norm;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
void dqrank ( double a[], int lda, int m, int n, double tol, int *kr,
|
||||||
|
int jpvt[], double qraux[] )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DQRANK computes the QR factorization of a rectangular matrix.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
This routine is used in conjunction with DQRLSS to solve
|
||||||
|
overdetermined, underdetermined and singular linear systems
|
||||||
|
in a least squares sense.
|
||||||
|
|
||||||
|
DQRANK uses the LINPACK subroutine DQRDC to compute the QR
|
||||||
|
factorization, with column pivoting, of an M by N matrix A.
|
||||||
|
The numerical rank is determined using the tolerance TOL.
|
||||||
|
|
||||||
|
Note that on output, ABS ( A(1,1) ) / ABS ( A(KR,KR) ) is an estimate
|
||||||
|
of the condition number of the matrix of independent columns,
|
||||||
|
and of R. This estimate will be <= 1/TOL.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
21 April 2012
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt.
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
|
||||||
|
LINPACK User's Guide,
|
||||||
|
SIAM, 1979,
|
||||||
|
ISBN13: 978-0-898711-72-1,
|
||||||
|
LC: QA214.L56.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input/output, double A[LDA*N]. On input, the matrix whose
|
||||||
|
decomposition is to be computed. On output, the information from DQRDC.
|
||||||
|
The triangular matrix R of the QR factorization is contained in the
|
||||||
|
upper triangle and information needed to recover the orthogonal
|
||||||
|
matrix Q is stored below the diagonal in A and in the vector QRAUX.
|
||||||
|
|
||||||
|
Input, int LDA, the leading dimension of A, which must
|
||||||
|
be at least M.
|
||||||
|
|
||||||
|
Input, int M, the number of rows of A.
|
||||||
|
|
||||||
|
Input, int N, the number of columns of A.
|
||||||
|
|
||||||
|
Input, double TOL, a relative tolerance used to determine the
|
||||||
|
numerical rank. The problem should be scaled so that all the elements
|
||||||
|
of A have roughly the same absolute accuracy, EPS. Then a reasonable
|
||||||
|
value for TOL is roughly EPS divided by the magnitude of the largest
|
||||||
|
element.
|
||||||
|
|
||||||
|
Output, int *KR, the numerical rank.
|
||||||
|
|
||||||
|
Output, int JPVT[N], the pivot information from DQRDC.
|
||||||
|
Columns JPVT(1), ..., JPVT(KR) of the original matrix are linearly
|
||||||
|
independent to within the tolerance TOL and the remaining columns
|
||||||
|
are linearly dependent.
|
||||||
|
|
||||||
|
Output, double QRAUX[N], will contain extra information defining
|
||||||
|
the QR factorization.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int job;
|
||||||
|
int k;
|
||||||
|
double *work;
|
||||||
|
|
||||||
|
for ( i = 0; i < n; i++ )
|
||||||
|
{
|
||||||
|
jpvt[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
work = ( double * ) malloc ( n * sizeof ( double ) );
|
||||||
|
job = 1;
|
||||||
|
|
||||||
|
dqrdc ( a, lda, m, n, qraux, jpvt, work, job );
|
||||||
|
|
||||||
|
*kr = 0;
|
||||||
|
k = i4_min ( m, n );
|
||||||
|
|
||||||
|
for ( j = 0; j < k; j++ )
|
||||||
|
{
|
||||||
|
if ( r8_abs ( a[j+j*lda] ) <= tol * r8_abs ( a[0+0*lda] ) )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
*kr = j + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
free ( work );
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
void dqrdc ( double a[], int lda, int n, int p, double qraux[], int jpvt[],
|
||||||
|
double work[], int job )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DQRDC computes the QR factorization of a real rectangular matrix.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
DQRDC uses Householder transformations.
|
||||||
|
|
||||||
|
Column pivoting based on the 2-norms of the reduced columns may be
|
||||||
|
performed at the user's option.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
07 June 2005
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt.
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
Jack Dongarra, Cleve Moler, Jim Bunch and Pete Stewart,
|
||||||
|
LINPACK User's Guide,
|
||||||
|
SIAM, (Society for Industrial and Applied Mathematics),
|
||||||
|
3600 University City Science Center,
|
||||||
|
Philadelphia, PA, 19104-2688.
|
||||||
|
ISBN 0-89871-172-X
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input/output, double A(LDA,P). On input, the N by P matrix
|
||||||
|
whose decomposition is to be computed. On output, A contains in
|
||||||
|
its upper triangle the upper triangular matrix R of the QR
|
||||||
|
factorization. Below its diagonal A contains information from
|
||||||
|
which the orthogonal part of the decomposition can be recovered.
|
||||||
|
Note that if pivoting has been requested, the decomposition is not that
|
||||||
|
of the original matrix A but that of A with its columns permuted
|
||||||
|
as described by JPVT.
|
||||||
|
|
||||||
|
Input, int LDA, the leading dimension of the array A. LDA must
|
||||||
|
be at least N.
|
||||||
|
|
||||||
|
Input, int N, the number of rows of the matrix A.
|
||||||
|
|
||||||
|
Input, int P, the number of columns of the matrix A.
|
||||||
|
|
||||||
|
Output, double QRAUX[P], contains further information required
|
||||||
|
to recover the orthogonal part of the decomposition.
|
||||||
|
|
||||||
|
Input/output, integer JPVT[P]. On input, JPVT contains integers that
|
||||||
|
control the selection of the pivot columns. The K-th column A(*,K) of A
|
||||||
|
is placed in one of three classes according to the value of JPVT(K).
|
||||||
|
> 0, then A(K) is an initial column.
|
||||||
|
= 0, then A(K) is a free column.
|
||||||
|
< 0, then A(K) is a final column.
|
||||||
|
Before the decomposition is computed, initial columns are moved to
|
||||||
|
the beginning of the array A and final columns to the end. Both
|
||||||
|
initial and final columns are frozen in place during the computation
|
||||||
|
and only free columns are moved. At the K-th stage of the
|
||||||
|
reduction, if A(*,K) is occupied by a free column it is interchanged
|
||||||
|
with the free column of largest reduced norm. JPVT is not referenced
|
||||||
|
if JOB == 0. On output, JPVT(K) contains the index of the column of the
|
||||||
|
original matrix that has been interchanged into the K-th column, if
|
||||||
|
pivoting was requested.
|
||||||
|
|
||||||
|
Workspace, double WORK[P]. WORK is not referenced if JOB == 0.
|
||||||
|
|
||||||
|
Input, int JOB, initiates column pivoting.
|
||||||
|
0, no pivoting is done.
|
||||||
|
nonzero, pivoting is done.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int j;
|
||||||
|
int jp;
|
||||||
|
int l;
|
||||||
|
int lup;
|
||||||
|
int maxj;
|
||||||
|
double maxnrm;
|
||||||
|
double nrmxl;
|
||||||
|
int pl;
|
||||||
|
int pu;
|
||||||
|
int swapj;
|
||||||
|
double t;
|
||||||
|
double tt;
|
||||||
|
|
||||||
|
pl = 1;
|
||||||
|
pu = 0;
|
||||||
|
/*
|
||||||
|
If pivoting is requested, rearrange the columns.
|
||||||
|
*/
|
||||||
|
if ( job != 0 )
|
||||||
|
{
|
||||||
|
for ( j = 1; j <= p; j++ )
|
||||||
|
{
|
||||||
|
swapj = ( 0 < jpvt[j-1] );
|
||||||
|
|
||||||
|
if ( jpvt[j-1] < 0 )
|
||||||
|
{
|
||||||
|
jpvt[j-1] = -j;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
jpvt[j-1] = j;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( swapj )
|
||||||
|
{
|
||||||
|
if ( j != pl )
|
||||||
|
{
|
||||||
|
dswap ( n, a+0+(pl-1)*lda, 1, a+0+(j-1), 1 );
|
||||||
|
}
|
||||||
|
jpvt[j-1] = jpvt[pl-1];
|
||||||
|
jpvt[pl-1] = j;
|
||||||
|
pl = pl + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pu = p;
|
||||||
|
|
||||||
|
for ( j = p; 1 <= j; j-- )
|
||||||
|
{
|
||||||
|
if ( jpvt[j-1] < 0 )
|
||||||
|
{
|
||||||
|
jpvt[j-1] = -jpvt[j-1];
|
||||||
|
|
||||||
|
if ( j != pu )
|
||||||
|
{
|
||||||
|
dswap ( n, a+0+(pu-1)*lda, 1, a+0+(j-1)*lda, 1 );
|
||||||
|
jp = jpvt[pu-1];
|
||||||
|
jpvt[pu-1] = jpvt[j-1];
|
||||||
|
jpvt[j-1] = jp;
|
||||||
|
}
|
||||||
|
pu = pu - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Compute the norms of the free columns.
|
||||||
|
*/
|
||||||
|
for ( j = pl; j <= pu; j++ )
|
||||||
|
{
|
||||||
|
qraux[j-1] = dnrm2 ( n, a+0+(j-1)*lda, 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( j = pl; j <= pu; j++ )
|
||||||
|
{
|
||||||
|
work[j-1] = qraux[j-1];
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Perform the Householder reduction of A.
|
||||||
|
*/
|
||||||
|
lup = i4_min ( n, p );
|
||||||
|
|
||||||
|
for ( l = 1; l <= lup; l++ )
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
Bring the column of largest norm into the pivot position.
|
||||||
|
*/
|
||||||
|
if ( pl <= l && l < pu )
|
||||||
|
{
|
||||||
|
maxnrm = 0.0;
|
||||||
|
maxj = l;
|
||||||
|
for ( j = l; j <= pu; j++ )
|
||||||
|
{
|
||||||
|
if ( maxnrm < qraux[j-1] )
|
||||||
|
{
|
||||||
|
maxnrm = qraux[j-1];
|
||||||
|
maxj = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( maxj != l )
|
||||||
|
{
|
||||||
|
dswap ( n, a+0+(l-1)*lda, 1, a+0+(maxj-1)*lda, 1 );
|
||||||
|
qraux[maxj-1] = qraux[l-1];
|
||||||
|
work[maxj-1] = work[l-1];
|
||||||
|
jp = jpvt[maxj-1];
|
||||||
|
jpvt[maxj-1] = jpvt[l-1];
|
||||||
|
jpvt[l-1] = jp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Compute the Householder transformation for column L.
|
||||||
|
*/
|
||||||
|
qraux[l-1] = 0.0;
|
||||||
|
|
||||||
|
if ( l != n )
|
||||||
|
{
|
||||||
|
nrmxl = dnrm2 ( n-l+1, a+l-1+(l-1)*lda, 1 );
|
||||||
|
|
||||||
|
if ( nrmxl != 0.0 )
|
||||||
|
{
|
||||||
|
if ( a[l-1+(l-1)*lda] != 0.0 )
|
||||||
|
{
|
||||||
|
nrmxl = nrmxl * r8_sign ( a[l-1+(l-1)*lda] );
|
||||||
|
}
|
||||||
|
|
||||||
|
dscal ( n-l+1, 1.0 / nrmxl, a+l-1+(l-1)*lda, 1 );
|
||||||
|
a[l-1+(l-1)*lda] = 1.0 + a[l-1+(l-1)*lda];
|
||||||
|
/*
|
||||||
|
Apply the transformation to the remaining columns, updating the norms.
|
||||||
|
*/
|
||||||
|
for ( j = l + 1; j <= p; j++ )
|
||||||
|
{
|
||||||
|
t = -ddot ( n-l+1, a+l-1+(l-1)*lda, 1, a+l-1+(j-1)*lda, 1 )
|
||||||
|
/ a[l-1+(l-1)*lda];
|
||||||
|
daxpy ( n-l+1, t, a+l-1+(l-1)*lda, 1, a+l-1+(j-1)*lda, 1 );
|
||||||
|
|
||||||
|
if ( pl <= j && j <= pu )
|
||||||
|
{
|
||||||
|
if ( qraux[j-1] != 0.0 )
|
||||||
|
{
|
||||||
|
tt = 1.0 - pow ( r8_abs ( a[l-1+(j-1)*lda] ) / qraux[j-1], 2 );
|
||||||
|
tt = r8_max ( tt, 0.0 );
|
||||||
|
t = tt;
|
||||||
|
tt = 1.0 + 0.05 * tt * pow ( qraux[j-1] / work[j-1], 2 );
|
||||||
|
|
||||||
|
if ( tt != 1.0 )
|
||||||
|
{
|
||||||
|
qraux[j-1] = qraux[j-1] * sqrt ( t );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qraux[j-1] = dnrm2 ( n-l, a+l+(j-1)*lda, 1 );
|
||||||
|
work[j-1] = qraux[j-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Save the transformation.
|
||||||
|
*/
|
||||||
|
qraux[l-1] = a[l-1+(l-1)*lda];
|
||||||
|
a[l-1+(l-1)*lda] = -nrmxl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
int dqrls ( double a[], int lda, int m, int n, double tol, int *kr, double b[],
|
||||||
|
double x[], double rsd[], int jpvt[], double qraux[], int itask )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DQRLS factors and solves a linear system in the least squares sense.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
The linear system may be overdetermined, underdetermined or singular.
|
||||||
|
The solution is obtained using a QR factorization of the
|
||||||
|
coefficient matrix.
|
||||||
|
|
||||||
|
DQRLS can be efficiently used to solve several least squares
|
||||||
|
problems with the same matrix A. The first system is solved
|
||||||
|
with ITASK = 1. The subsequent systems are solved with
|
||||||
|
ITASK = 2, to avoid the recomputation of the matrix factors.
|
||||||
|
The parameters KR, JPVT, and QRAUX must not be modified
|
||||||
|
between calls to DQRLS.
|
||||||
|
|
||||||
|
DQRLS is used to solve in a least squares sense
|
||||||
|
overdetermined, underdetermined and singular linear systems.
|
||||||
|
The system is A*X approximates B where A is M by N.
|
||||||
|
B is a given M-vector, and X is the N-vector to be computed.
|
||||||
|
A solution X is found which minimimzes the sum of squares (2-norm)
|
||||||
|
of the residual, A*X - B.
|
||||||
|
|
||||||
|
The numerical rank of A is determined using the tolerance TOL.
|
||||||
|
|
||||||
|
DQRLS uses the LINPACK subroutine DQRDC to compute the QR
|
||||||
|
factorization, with column pivoting, of an M by N matrix A.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
10 September 2012
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt.
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
David Kahaner, Cleve Moler, Steven Nash,
|
||||||
|
Numerical Methods and Software,
|
||||||
|
Prentice Hall, 1989,
|
||||||
|
ISBN: 0-13-627258-4,
|
||||||
|
LC: TA345.K34.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input/output, double A[LDA*N], an M by N matrix.
|
||||||
|
On input, the matrix whose decomposition is to be computed.
|
||||||
|
In a least squares data fitting problem, A(I,J) is the
|
||||||
|
value of the J-th basis (model) function at the I-th data point.
|
||||||
|
On output, A contains the output from DQRDC. The triangular matrix R
|
||||||
|
of the QR factorization is contained in the upper triangle and
|
||||||
|
information needed to recover the orthogonal matrix Q is stored
|
||||||
|
below the diagonal in A and in the vector QRAUX.
|
||||||
|
|
||||||
|
Input, int LDA, the leading dimension of A.
|
||||||
|
|
||||||
|
Input, int M, the number of rows of A.
|
||||||
|
|
||||||
|
Input, int N, the number of columns of A.
|
||||||
|
|
||||||
|
Input, double TOL, a relative tolerance used to determine the
|
||||||
|
numerical rank. The problem should be scaled so that all the elements
|
||||||
|
of A have roughly the same absolute accuracy EPS. Then a reasonable
|
||||||
|
value for TOL is roughly EPS divided by the magnitude of the largest
|
||||||
|
element.
|
||||||
|
|
||||||
|
Output, int *KR, the numerical rank.
|
||||||
|
|
||||||
|
Input, double B[M], the right hand side of the linear system.
|
||||||
|
|
||||||
|
Output, double X[N], a least squares solution to the linear
|
||||||
|
system.
|
||||||
|
|
||||||
|
Output, double RSD[M], the residual, B - A*X. RSD may
|
||||||
|
overwrite B.
|
||||||
|
|
||||||
|
Workspace, int JPVT[N], required if ITASK = 1.
|
||||||
|
Columns JPVT(1), ..., JPVT(KR) of the original matrix are linearly
|
||||||
|
independent to within the tolerance TOL and the remaining columns
|
||||||
|
are linearly dependent. ABS ( A(1,1) ) / ABS ( A(KR,KR) ) is an estimate
|
||||||
|
of the condition number of the matrix of independent columns,
|
||||||
|
and of R. This estimate will be <= 1/TOL.
|
||||||
|
|
||||||
|
Workspace, double QRAUX[N], required if ITASK = 1.
|
||||||
|
|
||||||
|
Input, int ITASK.
|
||||||
|
1, DQRLS factors the matrix A and solves the least squares problem.
|
||||||
|
2, DQRLS assumes that the matrix A was factored with an earlier
|
||||||
|
call to DQRLS, and only solves the least squares problem.
|
||||||
|
|
||||||
|
Output, int DQRLS, error code.
|
||||||
|
0: no error
|
||||||
|
-1: LDA < M (fatal error)
|
||||||
|
-2: N < 1 (fatal error)
|
||||||
|
-3: ITASK < 1 (fatal error)
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int ind;
|
||||||
|
|
||||||
|
if ( lda < m )
|
||||||
|
{
|
||||||
|
/*fprintf ( stderr, "\n" );
|
||||||
|
fprintf ( stderr, "DQRLS - Fatal error!\n" );
|
||||||
|
fprintf ( stderr, " LDA < M.\n" );*/
|
||||||
|
ind = -1;
|
||||||
|
return ind;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( n <= 0 )
|
||||||
|
{
|
||||||
|
/*fprintf ( stderr, "\n" );
|
||||||
|
fprintf ( stderr, "DQRLS - Fatal error!\n" );
|
||||||
|
fprintf ( stderr, " N <= 0.\n" );*/
|
||||||
|
ind = -2;
|
||||||
|
return ind;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( itask < 1 )
|
||||||
|
{
|
||||||
|
/*fprintf ( stderr, "\n" );
|
||||||
|
fprintf ( stderr, "DQRLS - Fatal error!\n" );
|
||||||
|
fprintf ( stderr, " ITASK < 1.\n" );*/
|
||||||
|
ind = -3;
|
||||||
|
return ind;
|
||||||
|
}
|
||||||
|
|
||||||
|
ind = 0;
|
||||||
|
/*
|
||||||
|
Factor the matrix.
|
||||||
|
*/
|
||||||
|
if ( itask == 1 )
|
||||||
|
{
|
||||||
|
dqrank ( a, lda, m, n, tol, kr, jpvt, qraux );
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Solve the least-squares problem.
|
||||||
|
*/
|
||||||
|
dqrlss ( a, lda, m, n, *kr, b, x, rsd, jpvt, qraux );
|
||||||
|
|
||||||
|
return ind;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
void dqrlss ( double a[], int lda, int m, int n, int kr, double b[], double x[],
|
||||||
|
double rsd[], int jpvt[], double qraux[] )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DQRLSS solves a linear system in a least squares sense.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
DQRLSS must be preceded by a call to DQRANK.
|
||||||
|
|
||||||
|
The system is to be solved is
|
||||||
|
A * X = B
|
||||||
|
where
|
||||||
|
A is an M by N matrix with rank KR, as determined by DQRANK,
|
||||||
|
B is a given M-vector,
|
||||||
|
X is the N-vector to be computed.
|
||||||
|
|
||||||
|
A solution X, with at most KR nonzero components, is found which
|
||||||
|
minimizes the 2-norm of the residual (A*X-B).
|
||||||
|
|
||||||
|
Once the matrix A has been formed, DQRANK should be
|
||||||
|
called once to decompose it. Then, for each right hand
|
||||||
|
side B, DQRLSS should be called once to obtain the
|
||||||
|
solution and residual.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
10 September 2012
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, double A[LDA*N], the QR factorization information
|
||||||
|
from DQRANK. The triangular matrix R of the QR factorization is
|
||||||
|
contained in the upper triangle and information needed to recover
|
||||||
|
the orthogonal matrix Q is stored below the diagonal in A and in
|
||||||
|
the vector QRAUX.
|
||||||
|
|
||||||
|
Input, int LDA, the leading dimension of A, which must
|
||||||
|
be at least M.
|
||||||
|
|
||||||
|
Input, int M, the number of rows of A.
|
||||||
|
|
||||||
|
Input, int N, the number of columns of A.
|
||||||
|
|
||||||
|
Input, int KR, the rank of the matrix, as estimated by DQRANK.
|
||||||
|
|
||||||
|
Input, double B[M], the right hand side of the linear system.
|
||||||
|
|
||||||
|
Output, double X[N], a least squares solution to the
|
||||||
|
linear system.
|
||||||
|
|
||||||
|
Output, double RSD[M], the residual, B - A*X. RSD may
|
||||||
|
overwrite B.
|
||||||
|
|
||||||
|
Input, int JPVT[N], the pivot information from DQRANK.
|
||||||
|
Columns JPVT[0], ..., JPVT[KR-1] of the original matrix are linearly
|
||||||
|
independent to within the tolerance TOL and the remaining columns
|
||||||
|
are linearly dependent.
|
||||||
|
|
||||||
|
Input, double QRAUX[N], auxiliary information from DQRANK
|
||||||
|
defining the QR factorization.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int info;
|
||||||
|
int j;
|
||||||
|
int job;
|
||||||
|
int k;
|
||||||
|
double t;
|
||||||
|
|
||||||
|
if ( kr != 0 )
|
||||||
|
{
|
||||||
|
job = 110;
|
||||||
|
info = dqrsl ( a, lda, m, kr, qraux, b, rsd, rsd, x, rsd, rsd, job );
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = 0; i < n; i++ )
|
||||||
|
{
|
||||||
|
jpvt[i] = - jpvt[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = kr; i < n; i++ )
|
||||||
|
{
|
||||||
|
x[i] = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( j = 1; j <= n; j++ )
|
||||||
|
{
|
||||||
|
if ( jpvt[j-1] <= 0 )
|
||||||
|
{
|
||||||
|
k = - jpvt[j-1];
|
||||||
|
jpvt[j-1] = k;
|
||||||
|
|
||||||
|
while ( k != j )
|
||||||
|
{
|
||||||
|
t = x[j-1];
|
||||||
|
x[j-1] = x[k-1];
|
||||||
|
x[k-1] = t;
|
||||||
|
jpvt[k-1] = -jpvt[k-1];
|
||||||
|
k = jpvt[k-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
int dqrsl ( double a[], int lda, int n, int k, double qraux[], double y[],
|
||||||
|
double qy[], double qty[], double b[], double rsd[], double ab[], int job )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DQRSL computes transformations, projections, and least squares solutions.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
DQRSL requires the output of DQRDC.
|
||||||
|
|
||||||
|
For K <= min(N,P), let AK be the matrix
|
||||||
|
|
||||||
|
AK = ( A(JPVT[0]), A(JPVT(2)), ..., A(JPVT(K)) )
|
||||||
|
|
||||||
|
formed from columns JPVT[0], ..., JPVT(K) of the original
|
||||||
|
N by P matrix A that was input to DQRDC. If no pivoting was
|
||||||
|
done, AK consists of the first K columns of A in their
|
||||||
|
original order. DQRDC produces a factored orthogonal matrix Q
|
||||||
|
and an upper triangular matrix R such that
|
||||||
|
|
||||||
|
AK = Q * (R)
|
||||||
|
(0)
|
||||||
|
|
||||||
|
This information is contained in coded form in the arrays
|
||||||
|
A and QRAUX.
|
||||||
|
|
||||||
|
The parameters QY, QTY, B, RSD, and AB are not referenced
|
||||||
|
if their computation is not requested and in this case
|
||||||
|
can be replaced by dummy variables in the calling program.
|
||||||
|
To save storage, the user may in some cases use the same
|
||||||
|
array for different parameters in the calling sequence. A
|
||||||
|
frequently occurring example is when one wishes to compute
|
||||||
|
any of B, RSD, or AB and does not need Y or QTY. In this
|
||||||
|
case one may identify Y, QTY, and one of B, RSD, or AB, while
|
||||||
|
providing separate arrays for anything else that is to be
|
||||||
|
computed.
|
||||||
|
|
||||||
|
Thus the calling sequence
|
||||||
|
|
||||||
|
dqrsl ( a, lda, n, k, qraux, y, dum, y, b, y, dum, 110, info )
|
||||||
|
|
||||||
|
will result in the computation of B and RSD, with RSD
|
||||||
|
overwriting Y. More generally, each item in the following
|
||||||
|
list contains groups of permissible identifications for
|
||||||
|
a single calling sequence.
|
||||||
|
|
||||||
|
1. (Y,QTY,B) (RSD) (AB) (QY)
|
||||||
|
|
||||||
|
2. (Y,QTY,RSD) (B) (AB) (QY)
|
||||||
|
|
||||||
|
3. (Y,QTY,AB) (B) (RSD) (QY)
|
||||||
|
|
||||||
|
4. (Y,QY) (QTY,B) (RSD) (AB)
|
||||||
|
|
||||||
|
5. (Y,QY) (QTY,RSD) (B) (AB)
|
||||||
|
|
||||||
|
6. (Y,QY) (QTY,AB) (B) (RSD)
|
||||||
|
|
||||||
|
In any group the value returned in the array allocated to
|
||||||
|
the group corresponds to the last member of the group.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
07 June 2005
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt.
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
Jack Dongarra, Cleve Moler, Jim Bunch and Pete Stewart,
|
||||||
|
LINPACK User's Guide,
|
||||||
|
SIAM, (Society for Industrial and Applied Mathematics),
|
||||||
|
3600 University City Science Center,
|
||||||
|
Philadelphia, PA, 19104-2688.
|
||||||
|
ISBN 0-89871-172-X
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, double A[LDA*P], contains the output of DQRDC.
|
||||||
|
|
||||||
|
Input, int LDA, the leading dimension of the array A.
|
||||||
|
|
||||||
|
Input, int N, the number of rows of the matrix AK. It must
|
||||||
|
have the same value as N in DQRDC.
|
||||||
|
|
||||||
|
Input, int K, the number of columns of the matrix AK. K
|
||||||
|
must not be greater than min(N,P), where P is the same as in the
|
||||||
|
calling sequence to DQRDC.
|
||||||
|
|
||||||
|
Input, double QRAUX[P], the auxiliary output from DQRDC.
|
||||||
|
|
||||||
|
Input, double Y[N], a vector to be manipulated by DQRSL.
|
||||||
|
|
||||||
|
Output, double QY[N], contains Q * Y, if requested.
|
||||||
|
|
||||||
|
Output, double QTY[N], contains Q' * Y, if requested.
|
||||||
|
|
||||||
|
Output, double B[K], the solution of the least squares problem
|
||||||
|
minimize norm2 ( Y - AK * B),
|
||||||
|
if its computation has been requested. Note that if pivoting was
|
||||||
|
requested in DQRDC, the J-th component of B will be associated with
|
||||||
|
column JPVT(J) of the original matrix A that was input into DQRDC.
|
||||||
|
|
||||||
|
Output, double RSD[N], the least squares residual Y - AK * B,
|
||||||
|
if its computation has been requested. RSD is also the orthogonal
|
||||||
|
projection of Y onto the orthogonal complement of the column space
|
||||||
|
of AK.
|
||||||
|
|
||||||
|
Output, double AB[N], the least squares approximation Ak * B,
|
||||||
|
if its computation has been requested. AB is also the orthogonal
|
||||||
|
projection of Y onto the column space of A.
|
||||||
|
|
||||||
|
Input, integer JOB, specifies what is to be computed. JOB has
|
||||||
|
the decimal expansion ABCDE, with the following meaning:
|
||||||
|
|
||||||
|
if A != 0, compute QY.
|
||||||
|
if B != 0, compute QTY.
|
||||||
|
if C != 0, compute QTY and B.
|
||||||
|
if D != 0, compute QTY and RSD.
|
||||||
|
if E != 0, compute QTY and AB.
|
||||||
|
|
||||||
|
Note that a request to compute B, RSD, or AB automatically triggers
|
||||||
|
the computation of QTY, for which an array must be provided in the
|
||||||
|
calling sequence.
|
||||||
|
|
||||||
|
Output, int DQRSL, is zero unless the computation of B has
|
||||||
|
been requested and R is exactly singular. In this case, INFO is the
|
||||||
|
index of the first zero diagonal element of R, and B is left unaltered.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int cab;
|
||||||
|
int cb;
|
||||||
|
int cqty;
|
||||||
|
int cqy;
|
||||||
|
int cr;
|
||||||
|
int i;
|
||||||
|
int info;
|
||||||
|
int j;
|
||||||
|
int jj;
|
||||||
|
int ju;
|
||||||
|
double t;
|
||||||
|
double temp;
|
||||||
|
/*
|
||||||
|
Set INFO flag.
|
||||||
|
*/
|
||||||
|
info = 0;
|
||||||
|
/*
|
||||||
|
Determine what is to be computed.
|
||||||
|
*/
|
||||||
|
cqy = ( job / 10000 != 0 );
|
||||||
|
cqty = ( ( job % 10000 ) != 0 );
|
||||||
|
cb = ( ( job % 1000 ) / 100 != 0 );
|
||||||
|
cr = ( ( job % 100 ) / 10 != 0 );
|
||||||
|
cab = ( ( job % 10 ) != 0 );
|
||||||
|
|
||||||
|
ju = i4_min ( k, n-1 );
|
||||||
|
/*
|
||||||
|
Special action when N = 1.
|
||||||
|
*/
|
||||||
|
if ( ju == 0 )
|
||||||
|
{
|
||||||
|
if ( cqy )
|
||||||
|
{
|
||||||
|
qy[0] = y[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cqty )
|
||||||
|
{
|
||||||
|
qty[0] = y[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cab )
|
||||||
|
{
|
||||||
|
ab[0] = y[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cb )
|
||||||
|
{
|
||||||
|
if ( a[0+0*lda] == 0.0 )
|
||||||
|
{
|
||||||
|
info = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
b[0] = y[0] / a[0+0*lda];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cr )
|
||||||
|
{
|
||||||
|
rsd[0] = 0.0;
|
||||||
|
}
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Set up to compute QY or QTY.
|
||||||
|
*/
|
||||||
|
if ( cqy )
|
||||||
|
{
|
||||||
|
for ( i = 1; i <= n; i++ )
|
||||||
|
{
|
||||||
|
qy[i-1] = y[i-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cqty )
|
||||||
|
{
|
||||||
|
for ( i = 1; i <= n; i++ )
|
||||||
|
{
|
||||||
|
qty[i-1] = y[i-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Compute QY.
|
||||||
|
*/
|
||||||
|
if ( cqy )
|
||||||
|
{
|
||||||
|
for ( jj = 1; jj <= ju; jj++ )
|
||||||
|
{
|
||||||
|
j = ju - jj + 1;
|
||||||
|
|
||||||
|
if ( qraux[j-1] != 0.0 )
|
||||||
|
{
|
||||||
|
temp = a[j-1+(j-1)*lda];
|
||||||
|
a[j-1+(j-1)*lda] = qraux[j-1];
|
||||||
|
t = -ddot ( n-j+1, a+j-1+(j-1)*lda, 1, qy+j-1, 1 ) / a[j-1+(j-1)*lda];
|
||||||
|
daxpy ( n-j+1, t, a+j-1+(j-1)*lda, 1, qy+j-1, 1 );
|
||||||
|
a[j-1+(j-1)*lda] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Compute Q'*Y.
|
||||||
|
*/
|
||||||
|
if ( cqty )
|
||||||
|
{
|
||||||
|
for ( j = 1; j <= ju; j++ )
|
||||||
|
{
|
||||||
|
if ( qraux[j-1] != 0.0 )
|
||||||
|
{
|
||||||
|
temp = a[j-1+(j-1)*lda];
|
||||||
|
a[j-1+(j-1)*lda] = qraux[j-1];
|
||||||
|
t = -ddot ( n-j+1, a+j-1+(j-1)*lda, 1, qty+j-1, 1 ) / a[j-1+(j-1)*lda];
|
||||||
|
daxpy ( n-j+1, t, a+j-1+(j-1)*lda, 1, qty+j-1, 1 );
|
||||||
|
a[j-1+(j-1)*lda] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Set up to compute B, RSD, or AB.
|
||||||
|
*/
|
||||||
|
if ( cb )
|
||||||
|
{
|
||||||
|
for ( i = 1; i <= k; i++ )
|
||||||
|
{
|
||||||
|
b[i-1] = qty[i-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cab )
|
||||||
|
{
|
||||||
|
for ( i = 1; i <= k; i++ )
|
||||||
|
{
|
||||||
|
ab[i-1] = qty[i-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cr && k < n )
|
||||||
|
{
|
||||||
|
for ( i = k+1; i <= n; i++ )
|
||||||
|
{
|
||||||
|
rsd[i-1] = qty[i-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cab && k+1 <= n )
|
||||||
|
{
|
||||||
|
for ( i = k+1; i <= n; i++ )
|
||||||
|
{
|
||||||
|
ab[i-1] = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cr )
|
||||||
|
{
|
||||||
|
for ( i = 1; i <= k; i++ )
|
||||||
|
{
|
||||||
|
rsd[i-1] = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Compute B.
|
||||||
|
*/
|
||||||
|
if ( cb )
|
||||||
|
{
|
||||||
|
for ( jj = 1; jj <= k; jj++ )
|
||||||
|
{
|
||||||
|
j = k - jj + 1;
|
||||||
|
|
||||||
|
if ( a[j-1+(j-1)*lda] == 0.0 )
|
||||||
|
{
|
||||||
|
info = j;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
b[j-1] = b[j-1] / a[j-1+(j-1)*lda];
|
||||||
|
|
||||||
|
if ( j != 1 )
|
||||||
|
{
|
||||||
|
t = -b[j-1];
|
||||||
|
daxpy ( j-1, t, a+0+(j-1)*lda, 1, b, 1 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Compute RSD or AB as required.
|
||||||
|
*/
|
||||||
|
if ( cr || cab )
|
||||||
|
{
|
||||||
|
for ( jj = 1; jj <= ju; jj++ )
|
||||||
|
{
|
||||||
|
j = ju - jj + 1;
|
||||||
|
|
||||||
|
if ( qraux[j-1] != 0.0 )
|
||||||
|
{
|
||||||
|
temp = a[j-1+(j-1)*lda];
|
||||||
|
a[j-1+(j-1)*lda] = qraux[j-1];
|
||||||
|
|
||||||
|
if ( cr )
|
||||||
|
{
|
||||||
|
t = -ddot ( n-j+1, a+j-1+(j-1)*lda, 1, rsd+j-1, 1 )
|
||||||
|
/ a[j-1+(j-1)*lda];
|
||||||
|
daxpy ( n-j+1, t, a+j-1+(j-1)*lda, 1, rsd+j-1, 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( cab )
|
||||||
|
{
|
||||||
|
t = -ddot ( n-j+1, a+j-1+(j-1)*lda, 1, ab+j-1, 1 )
|
||||||
|
/ a[j-1+(j-1)*lda];
|
||||||
|
daxpy ( n-j+1, t, a+j-1+(j-1)*lda, 1, ab+j-1, 1 );
|
||||||
|
}
|
||||||
|
a[j-1+(j-1)*lda] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
void dscal ( int n, double sa, double x[], int incx )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DSCAL scales a vector by a constant.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
30 March 2007
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
|
||||||
|
LINPACK User's Guide,
|
||||||
|
SIAM, 1979.
|
||||||
|
|
||||||
|
Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
|
||||||
|
Basic Linear Algebra Subprograms for Fortran Usage,
|
||||||
|
Algorithm 539,
|
||||||
|
ACM Transactions on Mathematical Software,
|
||||||
|
Volume 5, Number 3, September 1979, pages 308-323.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, int N, the number of entries in the vector.
|
||||||
|
|
||||||
|
Input, double SA, the multiplier.
|
||||||
|
|
||||||
|
Input/output, double X[*], the vector to be scaled.
|
||||||
|
|
||||||
|
Input, int INCX, the increment between successive entries of X.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int ix;
|
||||||
|
int m;
|
||||||
|
|
||||||
|
if ( n <= 0 )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else if ( incx == 1 )
|
||||||
|
{
|
||||||
|
m = n % 5;
|
||||||
|
|
||||||
|
for ( i = 0; i < m; i++ )
|
||||||
|
{
|
||||||
|
x[i] = sa * x[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = m; i < n; i = i + 5 )
|
||||||
|
{
|
||||||
|
x[i] = sa * x[i];
|
||||||
|
x[i+1] = sa * x[i+1];
|
||||||
|
x[i+2] = sa * x[i+2];
|
||||||
|
x[i+3] = sa * x[i+3];
|
||||||
|
x[i+4] = sa * x[i+4];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ( 0 <= incx )
|
||||||
|
{
|
||||||
|
ix = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ix = ( - n + 1 ) * incx;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = 0; i < n; i++ )
|
||||||
|
{
|
||||||
|
x[ix] = sa * x[ix];
|
||||||
|
ix = ix + incx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
void dswap ( int n, double x[], int incx, double y[], int incy )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
DSWAP interchanges two vectors.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
30 March 2007
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
C version by John Burkardt
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
|
||||||
|
LINPACK User's Guide,
|
||||||
|
SIAM, 1979.
|
||||||
|
|
||||||
|
Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
|
||||||
|
Basic Linear Algebra Subprograms for Fortran Usage,
|
||||||
|
Algorithm 539,
|
||||||
|
ACM Transactions on Mathematical Software,
|
||||||
|
Volume 5, Number 3, September 1979, pages 308-323.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, int N, the number of entries in the vectors.
|
||||||
|
|
||||||
|
Input/output, double X[*], one of the vectors to swap.
|
||||||
|
|
||||||
|
Input, int INCX, the increment between successive entries of X.
|
||||||
|
|
||||||
|
Input/output, double Y[*], one of the vectors to swap.
|
||||||
|
|
||||||
|
Input, int INCY, the increment between successive elements of Y.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int ix;
|
||||||
|
int iy;
|
||||||
|
int m;
|
||||||
|
double temp;
|
||||||
|
|
||||||
|
if ( n <= 0 )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else if ( incx == 1 && incy == 1 )
|
||||||
|
{
|
||||||
|
m = n % 3;
|
||||||
|
|
||||||
|
for ( i = 0; i < m; i++ )
|
||||||
|
{
|
||||||
|
temp = x[i];
|
||||||
|
x[i] = y[i];
|
||||||
|
y[i] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = m; i < n; i = i + 3 )
|
||||||
|
{
|
||||||
|
temp = x[i];
|
||||||
|
x[i] = y[i];
|
||||||
|
y[i] = temp;
|
||||||
|
|
||||||
|
temp = x[i+1];
|
||||||
|
x[i+1] = y[i+1];
|
||||||
|
y[i+1] = temp;
|
||||||
|
|
||||||
|
temp = x[i+2];
|
||||||
|
x[i+2] = y[i+2];
|
||||||
|
y[i+2] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ( 0 <= incx )
|
||||||
|
{
|
||||||
|
ix = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ix = ( - n + 1 ) * incx;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 0 <= incy )
|
||||||
|
{
|
||||||
|
iy = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
iy = ( - n + 1 ) * incy;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( i = 0; i < n; i++ )
|
||||||
|
{
|
||||||
|
temp = x[ix];
|
||||||
|
x[ix] = y[iy];
|
||||||
|
y[iy] = temp;
|
||||||
|
ix = ix + incx;
|
||||||
|
iy = iy + incy;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
double *qr_solve ( int m, int n, double a[], double b[] )
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/*
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
QR_SOLVE solves a linear system in the least squares sense.
|
||||||
|
|
||||||
|
Discussion:
|
||||||
|
|
||||||
|
If the matrix A has full column rank, then the solution X should be the
|
||||||
|
unique vector that minimizes the Euclidean norm of the residual.
|
||||||
|
|
||||||
|
If the matrix A does not have full column rank, then the solution is
|
||||||
|
not unique; the vector X will minimize the residual norm, but so will
|
||||||
|
various other vectors.
|
||||||
|
|
||||||
|
Licensing:
|
||||||
|
|
||||||
|
This code is distributed under the GNU LGPL license.
|
||||||
|
|
||||||
|
Modified:
|
||||||
|
|
||||||
|
11 September 2012
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
John Burkardt
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
|
||||||
|
David Kahaner, Cleve Moler, Steven Nash,
|
||||||
|
Numerical Methods and Software,
|
||||||
|
Prentice Hall, 1989,
|
||||||
|
ISBN: 0-13-627258-4,
|
||||||
|
LC: TA345.K34.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
Input, int M, the number of rows of A.
|
||||||
|
|
||||||
|
Input, int N, the number of columns of A.
|
||||||
|
|
||||||
|
Input, double A[M*N], the matrix.
|
||||||
|
|
||||||
|
Input, double B[M], the right hand side.
|
||||||
|
|
||||||
|
Output, double QR_SOLVE[N], the least squares solution.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
double *a_qr;
|
||||||
|
int ind;
|
||||||
|
int itask;
|
||||||
|
int *jpvt;
|
||||||
|
int kr;
|
||||||
|
int lda;
|
||||||
|
double *qraux;
|
||||||
|
double *r;
|
||||||
|
double tol;
|
||||||
|
double *x;
|
||||||
|
|
||||||
|
a_qr = r8mat_copy_new ( m, n, a );
|
||||||
|
lda = m;
|
||||||
|
tol = r8_epsilon ( ) / r8mat_amax ( m, n, a_qr );
|
||||||
|
x = ( double * ) malloc ( n * sizeof ( double ) );
|
||||||
|
jpvt = ( int * ) malloc ( n * sizeof ( int ) );
|
||||||
|
qraux = ( double * ) malloc ( n * sizeof ( double ) );
|
||||||
|
r = ( double * ) malloc ( m * sizeof ( double ) );
|
||||||
|
itask = 1;
|
||||||
|
|
||||||
|
ind = dqrls ( a_qr, lda, m, n, tol, &kr, b, x, r, jpvt, qraux, itask );
|
||||||
|
|
||||||
|
free ( a_qr );
|
||||||
|
free ( jpvt );
|
||||||
|
free ( qraux );
|
||||||
|
free ( r );
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,22 @@
|
|||||||
|
#include "Configuration.h"
|
||||||
|
|
||||||
|
#ifdef AUTO_BED_LEVELING_GRID
|
||||||
|
|
||||||
|
void daxpy ( int n, double da, double dx[], int incx, double dy[], int incy );
|
||||||
|
double ddot ( int n, double dx[], int incx, double dy[], int incy );
|
||||||
|
double dnrm2 ( int n, double x[], int incx );
|
||||||
|
void dqrank ( double a[], int lda, int m, int n, double tol, int *kr,
|
||||||
|
int jpvt[], double qraux[] );
|
||||||
|
void dqrdc ( double a[], int lda, int n, int p, double qraux[], int jpvt[],
|
||||||
|
double work[], int job );
|
||||||
|
int dqrls ( double a[], int lda, int m, int n, double tol, int *kr, double b[],
|
||||||
|
double x[], double rsd[], int jpvt[], double qraux[], int itask );
|
||||||
|
void dqrlss ( double a[], int lda, int m, int n, int kr, double b[], double x[],
|
||||||
|
double rsd[], int jpvt[], double qraux[] );
|
||||||
|
int dqrsl ( double a[], int lda, int n, int k, double qraux[], double y[],
|
||||||
|
double qy[], double qty[], double b[], double rsd[], double ab[], int job );
|
||||||
|
void dscal ( int n, double sa, double x[], int incx );
|
||||||
|
void dswap ( int n, double x[], int incx, double y[], int incy );
|
||||||
|
double *qr_solve ( int m, int n, double a[], double b[] );
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
vector_3.cpp - Vector library for bed leveling
|
||||||
|
Copyright (c) 2012 Lars Brubaker. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
#include <math.h>
|
||||||
|
#include "Marlin.h"
|
||||||
|
|
||||||
|
#ifdef ENABLE_AUTO_BED_LEVELING
|
||||||
|
#include "vector_3.h"
|
||||||
|
|
||||||
|
vector_3::vector_3() : x(0), y(0), z(0) { }
|
||||||
|
|
||||||
|
vector_3::vector_3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
|
||||||
|
|
||||||
|
vector_3 vector_3::cross(vector_3 left, vector_3 right)
|
||||||
|
{
|
||||||
|
return vector_3(left.y * right.z - left.z * right.y,
|
||||||
|
left.z * right.x - left.x * right.z,
|
||||||
|
left.x * right.y - left.y * right.x);
|
||||||
|
}
|
||||||
|
|
||||||
|
vector_3 vector_3::operator+(vector_3 v)
|
||||||
|
{
|
||||||
|
return vector_3((x + v.x), (y + v.y), (z + v.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
vector_3 vector_3::operator-(vector_3 v)
|
||||||
|
{
|
||||||
|
return vector_3((x - v.x), (y - v.y), (z - v.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
vector_3 vector_3::get_normal()
|
||||||
|
{
|
||||||
|
vector_3 normalized = vector_3(x, y, z);
|
||||||
|
normalized.normalize();
|
||||||
|
return normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
float vector_3::get_length()
|
||||||
|
{
|
||||||
|
float length = sqrt((x * x) + (y * y) + (z * z));
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
void vector_3::normalize()
|
||||||
|
{
|
||||||
|
float length = get_length();
|
||||||
|
x /= length;
|
||||||
|
y /= length;
|
||||||
|
z /= length;
|
||||||
|
}
|
||||||
|
|
||||||
|
void vector_3::apply_rotation(matrix_3x3 matrix)
|
||||||
|
{
|
||||||
|
float resultX = x * matrix.matrix[3*0+0] + y * matrix.matrix[3*1+0] + z * matrix.matrix[3*2+0];
|
||||||
|
float resultY = x * matrix.matrix[3*0+1] + y * matrix.matrix[3*1+1] + z * matrix.matrix[3*2+1];
|
||||||
|
float resultZ = x * matrix.matrix[3*0+2] + y * matrix.matrix[3*1+2] + z * matrix.matrix[3*2+2];
|
||||||
|
|
||||||
|
x = resultX;
|
||||||
|
y = resultY;
|
||||||
|
z = resultZ;
|
||||||
|
}
|
||||||
|
|
||||||
|
void vector_3::debug(char* title)
|
||||||
|
{
|
||||||
|
SERIAL_PROTOCOL(title);
|
||||||
|
SERIAL_PROTOCOLPGM(" x: ");
|
||||||
|
SERIAL_PROTOCOL(x);
|
||||||
|
SERIAL_PROTOCOLPGM(" y: ");
|
||||||
|
SERIAL_PROTOCOL(y);
|
||||||
|
SERIAL_PROTOCOLPGM(" z: ");
|
||||||
|
SERIAL_PROTOCOL(z);
|
||||||
|
SERIAL_PROTOCOLPGM("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void apply_rotation_xyz(matrix_3x3 matrix, float &x, float& y, float& z)
|
||||||
|
{
|
||||||
|
vector_3 vector = vector_3(x, y, z);
|
||||||
|
vector.apply_rotation(matrix);
|
||||||
|
x = vector.x;
|
||||||
|
y = vector.y;
|
||||||
|
z = vector.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix_3x3 matrix_3x3::create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2)
|
||||||
|
{
|
||||||
|
//row_0.debug("row_0");
|
||||||
|
//row_1.debug("row_1");
|
||||||
|
//row_2.debug("row_2");
|
||||||
|
matrix_3x3 new_matrix;
|
||||||
|
new_matrix.matrix[0] = row_0.x; new_matrix.matrix[1] = row_0.y; new_matrix.matrix[2] = row_0.z;
|
||||||
|
new_matrix.matrix[3] = row_1.x; new_matrix.matrix[4] = row_1.y; new_matrix.matrix[5] = row_1.z;
|
||||||
|
new_matrix.matrix[6] = row_2.x; new_matrix.matrix[7] = row_2.y; new_matrix.matrix[8] = row_2.z;
|
||||||
|
//new_matrix.debug("new_matrix");
|
||||||
|
|
||||||
|
return new_matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
void matrix_3x3::set_to_identity()
|
||||||
|
{
|
||||||
|
matrix[0] = 1; matrix[1] = 0; matrix[2] = 0;
|
||||||
|
matrix[3] = 0; matrix[4] = 1; matrix[5] = 0;
|
||||||
|
matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix_3x3 matrix_3x3::create_look_at(vector_3 target)
|
||||||
|
{
|
||||||
|
vector_3 z_row = target.get_normal();
|
||||||
|
vector_3 x_row = vector_3(1, 0, -target.x/target.z).get_normal();
|
||||||
|
vector_3 y_row = vector_3(0, 1, -target.y/target.z).get_normal();
|
||||||
|
|
||||||
|
// x_row.debug("x_row");
|
||||||
|
// y_row.debug("y_row");
|
||||||
|
// z_row.debug("z_row");
|
||||||
|
|
||||||
|
|
||||||
|
// create the matrix already correctly transposed
|
||||||
|
matrix_3x3 rot = matrix_3x3::create_from_rows(x_row, y_row, z_row);
|
||||||
|
|
||||||
|
// rot.debug("rot");
|
||||||
|
return rot;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
matrix_3x3 matrix_3x3::transpose(matrix_3x3 original)
|
||||||
|
{
|
||||||
|
matrix_3x3 new_matrix;
|
||||||
|
new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
|
||||||
|
new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];
|
||||||
|
new_matrix.matrix[6] = original.matrix[2]; new_matrix.matrix[7] = original.matrix[5]; new_matrix.matrix[8] = original.matrix[8];
|
||||||
|
return new_matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
void matrix_3x3::debug(char* title)
|
||||||
|
{
|
||||||
|
SERIAL_PROTOCOL(title);
|
||||||
|
SERIAL_PROTOCOL("\n");
|
||||||
|
int count = 0;
|
||||||
|
for(int i=0; i<3; i++)
|
||||||
|
{
|
||||||
|
for(int j=0; j<3; j++)
|
||||||
|
{
|
||||||
|
SERIAL_PROTOCOL(matrix[count]);
|
||||||
|
SERIAL_PROTOCOLPGM(" ");
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
SERIAL_PROTOCOLPGM("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // #ifdef ENABLE_AUTO_BED_LEVELING
|
||||||
|
|
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
vector_3.cpp - Vector library for bed leveling
|
||||||
|
Copyright (c) 2012 Lars Brubaker. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
#ifndef VECTOR_3_H
|
||||||
|
#define VECTOR_3_H
|
||||||
|
|
||||||
|
#ifdef ENABLE_AUTO_BED_LEVELING
|
||||||
|
class matrix_3x3;
|
||||||
|
|
||||||
|
struct vector_3
|
||||||
|
{
|
||||||
|
float x, y, z;
|
||||||
|
|
||||||
|
vector_3();
|
||||||
|
vector_3(float x, float y, float z);
|
||||||
|
|
||||||
|
static vector_3 cross(vector_3 a, vector_3 b);
|
||||||
|
|
||||||
|
vector_3 operator+(vector_3 v);
|
||||||
|
vector_3 operator-(vector_3 v);
|
||||||
|
void normalize();
|
||||||
|
float get_length();
|
||||||
|
vector_3 get_normal();
|
||||||
|
|
||||||
|
void debug(char* title);
|
||||||
|
|
||||||
|
void apply_rotation(matrix_3x3 matrix);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct matrix_3x3
|
||||||
|
{
|
||||||
|
float matrix[9];
|
||||||
|
|
||||||
|
static matrix_3x3 create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2);
|
||||||
|
static matrix_3x3 create_look_at(vector_3 target);
|
||||||
|
static matrix_3x3 transpose(matrix_3x3 original);
|
||||||
|
|
||||||
|
void set_to_identity();
|
||||||
|
|
||||||
|
void debug(char* title);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void apply_rotation_xyz(matrix_3x3 rotationMatrix, float &x, float& y, float& z);
|
||||||
|
#endif // ENABLE_AUTO_BED_LEVELING
|
||||||
|
|
||||||
|
#endif // VECTOR_3_H
|
Loading…
Reference in new issue