Added Serial line encoding functions + events

https://github.com/arduino/Arduino/pull/2469
Weak implementation sounds usefull but takes 6 more bytes of flash/
function (12). Even more, if we would also pass the line states but the
user can read this with Serial.dtr() for example.

https://github.com/arduino/Arduino/pull/1179
Should we inline the Serial.dtr() functuions etc? But then we need to
move the line info to the header as well, also the definition.
These optional functions only take flash if they are used. Virtual isnt
used, because this would take more flash for no reason.
Syntax like this: https://www.pjrc.com/teensy/td_serial.html
This commit is contained in:
Nico 2014-12-09 17:47:25 +01:00
parent cdc7471ddb
commit a1449d1576
4 changed files with 87 additions and 0 deletions

41
CDC.cpp
View file

@ -80,11 +80,13 @@ bool WEAK CDC_Setup(Setup& setup)
if (CDC_SET_LINE_CODING == r)
{
USB_RecvControl((void*)&_usbLineInfo, 7);
CDC_LineEncodingEvent();
}
if (CDC_SET_CONTROL_LINE_STATE == r)
{
_usbLineInfo.lineState = setup.wValueL;
CDC_LineStateEvent();
}
if (CDC_SET_LINE_CODING == r || CDC_SET_CONTROL_LINE_STATE == r)
@ -146,6 +148,15 @@ bool WEAK CDC_Setup(Setup& setup)
return false;
}
void WEAK CDC_LineEncodingEvent(void)
{
// has to be implemented by the user
}
void WEAK CDC_LineStateEvent(void)
{
// has to be implemented by the user
}
void Serial_::begin(unsigned long /* baud_count */)
{
@ -221,6 +232,36 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
return 0;
}
uint32_t Serial_::baud(void)
{
return _usbLineInfo.dwDTERate;
}
uint8_t Serial_::stopbits(void)
{
return _usbLineInfo.bCharFormat;
}
uint8_t Serial_::paritytype(void)
{
return _usbLineInfo.bParityType;
}
uint8_t Serial_::numbits(void)
{
return _usbLineInfo.bDataBits;
}
bool Serial_::dtr(void)
{
return (_usbLineInfo.lineState & CDC_CONTROL_LINE_OUT_DTR) ? true : false;
}
bool Serial_::rts(void)
{
return (_usbLineInfo.lineState & CDC_CONTROL_LINE_OUT_RTS) ? true : false;
}
// 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

35
HID_Tests/HID_Tests.ino Normal file
View file

@ -0,0 +1,35 @@
// simply, ugly code to test the new functions
void setup() {
Serial.begin(1);
}
uint32_t eventBaud = 0;
void loop() {
if (Serial.available()) {
while (Serial.read() != -1);
Serial.println("Serial Port working");
Keyboard.println("Nico is cool");
delay(3000);
}
delay(3000);
Serial.println(Serial.dtr());
Serial.println(Serial.rts());
Serial.println(Serial.baud());
Serial.println(Serial.stopbits());
Serial.println(Serial.paritytype());
Serial.println(Serial.numbits());
if (eventBaud) {
Serial.println("Event");
Serial.println(eventBaud);
eventBaud = 0;
}
}
void CDC_LineEncodingEvent(void)
{
eventBaud = Serial.baud();
}

View file

@ -82,6 +82,12 @@ public:
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t*, size_t);
using Print::write; // pull in write(str) and write(buf, size) from Print
uint32_t baud(void);
uint8_t stopbits(void);
uint8_t paritytype(void);
uint8_t numbits(void);
bool dtr(void);
bool rts(void);
operator bool();
volatile uint8_t _rx_buffer_head;
@ -222,6 +228,8 @@ bool MSC_Data(uint8_t rx, uint8_t tx);
int CDC_GetInterface(uint8_t* interfaceNum);
int CDC_GetDescriptor(int i);
bool CDC_Setup(Setup& setup);
void CDC_LineEncodingEvent(void);
void CDC_LineStateEvent(void);
//================================================================================
//================================================================================

View file

@ -56,6 +56,9 @@
#define CDC_GET_LINE_CODING 0x21
#define CDC_SET_CONTROL_LINE_STATE 0x22
#define CDC_CONTROL_LINE_OUT_DTR (1 << 0)
#define CDC_CONTROL_LINE_OUT_RTS (1 << 1)
#define MSC_RESET 0xFF
#define MSC_GET_MAX_LUN 0xFE