Original implementation of HardwareSerial lib.

This commit is contained in:
Knutwurst
2020-06-04 10:47:08 +02:00
parent 72fe4d3943
commit 256dda9d13
3 changed files with 36 additions and 46 deletions

View File

@@ -1,9 +1,4 @@
/* /*
AnycubicSerial.cpp --- Support for Anycubic i3 Mega TFT serial connection
Created by Christian Hopp on 2017-12-09
Modified by Oliver Köster on 2020-06-02
Original file:
HardwareSerial.cpp - Hardware serial library for Wiring HardwareSerial.cpp - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
@@ -40,7 +35,7 @@
// this is so I can support Attiny series and any other chip without a uart // this is so I can support Attiny series and any other chip without a uart
#if defined(UBRR3H) #if defined(UBRR3H)
#include "anycubic_serial.h" #include "HardwareSerial.h"
// Define constants and variables for buffering incoming serial data. We're // 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 // using a ring buffer (I think), in which head is the index of the location
@@ -116,7 +111,7 @@ ISR(USART3_UDRE_vect)
#endif #endif
AnycubicSerialClass::AnycubicSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer, HardwareSerialClass::HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
volatile uint8_t *ucsra, volatile uint8_t *ucsrb, volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
volatile uint8_t *ucsrc, volatile uint8_t *udr, volatile uint8_t *ucsrc, volatile uint8_t *udr,
@@ -139,7 +134,7 @@ AnycubicSerialClass::AnycubicSerialClass(ring_buffer *rx_buffer, ring_buffer *tx
// Public Methods ////////////////////////////////////////////////////////////// // Public Methods //////////////////////////////////////////////////////////////
void AnycubicSerialClass::begin(unsigned long baud) void HardwareSerialClass::begin(unsigned long baud)
{ {
uint16_t baud_setting; uint16_t baud_setting;
bool use_u2x = true; bool use_u2x = true;
@@ -182,7 +177,7 @@ try_again:
cbi(*_ucsrb, _udrie); cbi(*_ucsrb, _udrie);
} }
void AnycubicSerialClass::begin(unsigned long baud, byte config) void HardwareSerialClass::begin(unsigned long baud, byte config)
{ {
uint16_t baud_setting; uint16_t baud_setting;
uint8_t current_config; uint8_t current_config;
@@ -230,7 +225,7 @@ try_again:
cbi(*_ucsrb, _udrie); cbi(*_ucsrb, _udrie);
} }
void AnycubicSerialClass::end() void HardwareSerialClass::end()
{ {
// wait for transmission of outgoing data // wait for transmission of outgoing data
while (_tx_buffer->head != _tx_buffer->tail) while (_tx_buffer->head != _tx_buffer->tail)
@@ -245,12 +240,12 @@ void AnycubicSerialClass::end()
_rx_buffer->head = _rx_buffer->tail; _rx_buffer->head = _rx_buffer->tail;
} }
int AnycubicSerialClass::available(void) int HardwareSerialClass::available(void)
{ {
return (int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE; return (int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
} }
int AnycubicSerialClass::peek(void) int HardwareSerialClass::peek(void)
{ {
if (_rx_buffer->head == _rx_buffer->tail) if (_rx_buffer->head == _rx_buffer->tail)
{ {
@@ -262,7 +257,7 @@ int AnycubicSerialClass::peek(void)
} }
} }
int AnycubicSerialClass::read(void) int HardwareSerialClass::read(void)
{ {
// if the head isn't ahead of the tail, we don't have any characters // if the head isn't ahead of the tail, we don't have any characters
if (_rx_buffer->head == _rx_buffer->tail) if (_rx_buffer->head == _rx_buffer->tail)
@@ -277,7 +272,7 @@ int AnycubicSerialClass::read(void)
} }
} }
void AnycubicSerialClass::flush() void HardwareSerialClass::flush()
{ {
// UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
while (transmitting && !(*_ucsra & _BV(TXC0))) while (transmitting && !(*_ucsra & _BV(TXC0)))
@@ -285,7 +280,7 @@ void AnycubicSerialClass::flush()
transmitting = false; transmitting = false;
} }
size_t AnycubicSerialClass::write(uint8_t c) size_t HardwareSerialClass::write(uint8_t c)
{ {
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE; int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
@@ -306,13 +301,13 @@ size_t AnycubicSerialClass::write(uint8_t c)
return 1; return 1;
} }
AnycubicSerialClass::operator bool() HardwareSerialClass::operator bool()
{ {
return true; return true;
} }
#if defined(UBRR3H) #if defined(UBRR3H)
AnycubicSerialClass AnycubicSerial(&rx_buffer_ajg, &tx_buffer_ajg, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3); HardwareSerialClass HardwareSerial(&rx_buffer_ajg, &tx_buffer_ajg, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
#endif #endif
#endif #endif

View File

@@ -1,10 +1,4 @@
/* /*
AnycubicSerial.h --- Support for Anycubic i3 Mega TFT serial connection
Created by Christian Hopp on 2017-12-09
Modified by Oliver Köster on 2020-06-02
Original file:
HardwareSerial.h - Hardware serial library for Wiring HardwareSerial.h - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
@@ -26,8 +20,8 @@
Modified 14 August 2012 by Alarus Modified 14 August 2012 by Alarus
*/ */
#ifndef anycubic_serial_h #ifndef hardwareserial_h
#define anycubic_serial_h #define hardwareserial_h
#include <inttypes.h> #include <inttypes.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
@@ -38,7 +32,7 @@
struct ring_buffer; struct ring_buffer;
class AnycubicSerialClass : public Stream class HardwareSerialClass : public Stream
{ {
private: private:
ring_buffer *_rx_buffer; ring_buffer *_rx_buffer;
@@ -57,7 +51,7 @@ private:
bool transmitting; bool transmitting;
public: public:
AnycubicSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer, HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
volatile uint8_t *ucsra, volatile uint8_t *ucsrb, volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
volatile uint8_t *ucsrc, volatile uint8_t *udr, volatile uint8_t *ucsrc, volatile uint8_t *udr,
@@ -105,44 +99,44 @@ public:
#define SERIAL_8O2 0x3E #define SERIAL_8O2 0x3E
#if defined(UBRR3H) #if defined(UBRR3H)
extern AnycubicSerialClass AnycubicSerial; extern HardwareSerialClass HardwareSerial;
#endif #endif
extern void serialEventRun(void) __attribute__((weak)); extern void serialEventRun(void) __attribute__((weak));
#define ANYCUBIC_SERIAL_PROTOCOL(x) (AnycubicSerial.print(x)) #define ANYCUBIC_SERIAL_PROTOCOL(x) (HardwareSerial.print(x))
#define ANYCUBIC_SERIAL_PROTOCOL_F(x, y) (AnycubicSerial.print(x, y)) #define ANYCUBIC_SERIAL_PROTOCOL_F(x, y) (HardwareSerial.print(x, y))
#define ANYCUBIC_SERIAL_PROTOCOLPGM(x) (AnycubicSerialprintPGM(PSTR(x))) #define ANYCUBIC_SERIAL_PROTOCOLPGM(x) (HardwareSerialprintPGM(PSTR(x)))
#define ANYCUBIC_SERIAL_(x) (AnycubicSerial.print(x), AnycubicSerial.write('\n')) #define ANYCUBIC_SERIAL_(x) (HardwareSerial.print(x), HardwareSerial.write('\n'))
#define ANYCUBIC_SERIAL_PROTOCOLLN(x) (AnycubicSerial.print(x), AnycubicSerial.write('\r'), AnycubicSerial.write('\n')) #define ANYCUBIC_SERIAL_PROTOCOLLN(x) (HardwareSerial.print(x), HardwareSerial.write('\r'), HardwareSerial.write('\n'))
#define ANYCUBIC_SERIAL_PROTOCOLLNPGM(x) (AnycubicSerialprintPGM(PSTR(x)), AnycubicSerial.write('\r'), AnycubicSerial.write('\n')) #define ANYCUBIC_SERIAL_PROTOCOLLNPGM(x) (HardwareSerialprintPGM(PSTR(x)), HardwareSerial.write('\r'), HardwareSerial.write('\n'))
#define ANYCUBIC_SERIAL_START() (AnycubicSerial.write('\r'), AnycubicSerial.write('\n')) #define ANYCUBIC_SERIAL_START() (HardwareSerial.write('\r'), HardwareSerial.write('\n'))
#define ANYCUBIC_SERIAL_CMD_SEND(x) (AnycubicSerialprintPGM(PSTR(x)), AnycubicSerial.write('\r'), AnycubicSerial.write('\n')) #define ANYCUBIC_SERIAL_CMD_SEND(x) (HardwareSerialprintPGM(PSTR(x)), HardwareSerial.write('\r'), HardwareSerial.write('\n'))
#define ANYCUBIC_SERIAL_ENTER() (AnycubicSerial.write('\r'), AnycubicSerial.write('\n')) #define ANYCUBIC_SERIAL_ENTER() (HardwareSerial.write('\r'), HardwareSerial.write('\n'))
#define ANYCUBIC_SERIAL_SPACE() (AnycubicSerial.write(' ')) #define ANYCUBIC_SERIAL_SPACE() (HardwareSerial.write(' '))
const char newErr[] PROGMEM = "ERR "; const char newErr[] PROGMEM = "ERR ";
const char newSucc[] PROGMEM = "OK"; const char newSucc[] PROGMEM = "OK";
#define ANYCUBIC_SERIAL_ERROR_START (AnycubicSerialprintPGM(newErr)) #define ANYCUBIC_SERIAL_ERROR_START (HardwareSerialprintPGM(newErr))
#define ANYCUBIC_SERIAL_ERROR(x) ANYCUBIC_SERIAL_PROTOCOL(x) #define ANYCUBIC_SERIAL_ERROR(x) ANYCUBIC_SERIAL_PROTOCOL(x)
#define ANYCUBIC_SERIAL_ERRORPGM(x) ANYCUBIC_SERIAL_PROTOCOLPGM(x) #define ANYCUBIC_SERIAL_ERRORPGM(x) ANYCUBIC_SERIAL_PROTOCOLPGM(x)
#define ANYCUBIC_SERIAL_ERRORLN(x) ANYCUBIC_SERIAL_PROTOCOLLN(x) #define ANYCUBIC_SERIAL_ERRORLN(x) ANYCUBIC_SERIAL_PROTOCOLLN(x)
#define ANYCUBIC_SERIAL_ERRORLNPGM(x) ANYCUBIC_SERIAL_PROTOCOLLNPGM(x) #define ANYCUBIC_SERIAL_ERRORLNPGM(x) ANYCUBIC_SERIAL_PROTOCOLLNPGM(x)
#define ANYCUBIC_SERIAL_ECHOLN(x) ANYCUBIC_SERIAL_PROTOCOLLN(x) #define ANYCUBIC_SERIAL_ECHOLN(x) ANYCUBIC_SERIAL_PROTOCOLLN(x)
#define ANYCUBIC_SERIAL_SUCC_START (AnycubicSerialprintPGM(newSucc)) #define ANYCUBIC_SERIAL_SUCC_START (HardwareSerialprintPGM(newSucc))
#define ANYCUBIC_SERIAL_ECHOPAIR(name, value) (serial_echopair_P(PSTR(name), (value))) #define ANYCUBIC_SERIAL_ECHOPAIR(name, value) (serial_echopair_P(PSTR(name), (value)))
#define ANYCUBIC_SERIAL_ECHOPGM(x) ANYCUBIC_SERIAL_PROTOCOLPGM(x) #define ANYCUBIC_SERIAL_ECHOPGM(x) ANYCUBIC_SERIAL_PROTOCOLPGM(x)
#define ANYCUBIC_SERIAL_ECHO(x) ANYCUBIC_SERIAL_PROTOCOL(x) #define ANYCUBIC_SERIAL_ECHO(x) ANYCUBIC_SERIAL_PROTOCOL(x)
FORCE_INLINE void AnycubicSerialprintPGM(const char *str) FORCE_INLINE void HardwareSerialprintPGM(const char *str)
{ {
char ch = pgm_read_byte(str); char ch = pgm_read_byte(str);
while (ch) while (ch)
{ {
AnycubicSerial.write(ch); HardwareSerial.write(ch);
ch = pgm_read_byte(++str); ch = pgm_read_byte(++str);
} }
} }

View File

@@ -41,7 +41,7 @@
#ifdef ANYCUBIC_TOUCHSCREEN #ifdef ANYCUBIC_TOUCHSCREEN
#include "anycubic_touchscreen.h" #include "anycubic_touchscreen.h"
#include "anycubic_serial.h" #include "HardwareSerial.h"
char _conv[8]; char _conv[8];
@@ -92,7 +92,7 @@ AnycubicTouchscreenClass::AnycubicTouchscreenClass()
void AnycubicTouchscreenClass::Setup() void AnycubicTouchscreenClass::Setup()
{ {
AnycubicSerial.begin(115200); HardwareSerial.begin(115200);
//ANYCUBIC_SERIAL_START(); //ANYCUBIC_SERIAL_START();
ANYCUBIC_SERIAL_PROTOCOLPGM("J17"); // J17 Main board reset ANYCUBIC_SERIAL_PROTOCOLPGM("J17"); // J17 Main board reset
ANYCUBIC_SERIAL_ENTER(); ANYCUBIC_SERIAL_ENTER();
@@ -566,6 +566,7 @@ void AnycubicTouchscreenClass::Ls()
uint16_t max_files; uint16_t max_files;
uint16_t dir_files = card.countFilesInWorkDir(); uint16_t dir_files = card.countFilesInWorkDir();
// What is this shit? What if there are exactely 3 files+folders?
if ((dir_files - filenumber) < 4) if ((dir_files - filenumber) < 4)
{ {
max_files = dir_files; max_files = dir_files;
@@ -888,9 +889,9 @@ void AnycubicTouchscreenClass::FilamentRunout()
void AnycubicTouchscreenClass::GetCommandFromTFT() void AnycubicTouchscreenClass::GetCommandFromTFT()
{ {
char *starpos = NULL; char *starpos = NULL;
while (AnycubicSerial.available() > 0 && TFTbuflen < TFTBUFSIZE) while (HardwareSerial.available() > 0 && TFTbuflen < TFTBUFSIZE)
{ {
serial3_char = AnycubicSerial.read(); serial3_char = HardwareSerial.read();
if (serial3_char == '\n' || if (serial3_char == '\n' ||
serial3_char == '\r' || serial3_char == '\r' ||
serial3_char == ':' || serial3_char == ':' ||