Skip to content

Commit 63ea8f0

Browse files
author
Nico
committed
Added Serial line encoding functions + events
arduino/Arduino#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. arduino/Arduino#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
1 parent 6c13faa commit 63ea8f0

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

plugins/KeyboardioHID/CDC.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ bool WEAK CDC_Setup(Setup& setup)
8080
if (CDC_SET_LINE_CODING == r)
8181
{
8282
USB_RecvControl((void*)&_usbLineInfo, 7);
83+
CDC_LineEncodingEvent();
8384
}
8485

8586
if (CDC_SET_CONTROL_LINE_STATE == r)
8687
{
8788
_usbLineInfo.lineState = setup.wValueL;
89+
CDC_LineStateEvent();
8890
}
8991

9092
if (CDC_SET_LINE_CODING == r || CDC_SET_CONTROL_LINE_STATE == r)
@@ -146,6 +148,15 @@ bool WEAK CDC_Setup(Setup& setup)
146148
return false;
147149
}
148150

151+
void WEAK CDC_LineEncodingEvent(void)
152+
{
153+
// has to be implemented by the user
154+
}
155+
156+
void WEAK CDC_LineStateEvent(void)
157+
{
158+
// has to be implemented by the user
159+
}
149160

150161
void Serial_::begin(unsigned long /* baud_count */)
151162
{
@@ -221,6 +232,36 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
221232
return 0;
222233
}
223234

235+
uint32_t Serial_::baud(void)
236+
{
237+
return _usbLineInfo.dwDTERate;
238+
}
239+
240+
uint8_t Serial_::stopbits(void)
241+
{
242+
return _usbLineInfo.bCharFormat;
243+
}
244+
245+
uint8_t Serial_::paritytype(void)
246+
{
247+
return _usbLineInfo.bParityType;
248+
}
249+
250+
uint8_t Serial_::numbits(void)
251+
{
252+
return _usbLineInfo.bDataBits;
253+
}
254+
255+
bool Serial_::dtr(void)
256+
{
257+
return (_usbLineInfo.lineState & CDC_CONTROL_LINE_OUT_DTR) ? true : false;
258+
}
259+
260+
bool Serial_::rts(void)
261+
{
262+
return (_usbLineInfo.lineState & CDC_CONTROL_LINE_OUT_RTS) ? true : false;
263+
}
264+
224265
// This operator is a convenient way for a sketch to check whether the
225266
// port has actually been configured and opened by the host (as opposed
226267
// to just being connected to the host). It can be used, for example, in
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// simply, ugly code to test the new functions
2+
3+
void setup() {
4+
Serial.begin(1);
5+
}
6+
7+
uint32_t eventBaud = 0;
8+
9+
void loop() {
10+
if (Serial.available()) {
11+
while (Serial.read() != -1);
12+
Serial.println("Serial Port working");
13+
Keyboard.println("Nico is cool");
14+
delay(3000);
15+
}
16+
17+
delay(3000);
18+
Serial.println(Serial.dtr());
19+
Serial.println(Serial.rts());
20+
Serial.println(Serial.baud());
21+
Serial.println(Serial.stopbits());
22+
Serial.println(Serial.paritytype());
23+
Serial.println(Serial.numbits());
24+
25+
if (eventBaud) {
26+
Serial.println("Event");
27+
Serial.println(eventBaud);
28+
eventBaud = 0;
29+
}
30+
}
31+
32+
void CDC_LineEncodingEvent(void)
33+
{
34+
eventBaud = Serial.baud();
35+
}

plugins/KeyboardioHID/USBAPI.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ class Serial_ : public Stream
8282
virtual size_t write(uint8_t);
8383
virtual size_t write(const uint8_t*, size_t);
8484
using Print::write; // pull in write(str) and write(buf, size) from Print
85+
uint32_t baud(void);
86+
uint8_t stopbits(void);
87+
uint8_t paritytype(void);
88+
uint8_t numbits(void);
89+
bool dtr(void);
90+
bool rts(void);
8591
operator bool();
8692

8793
volatile uint8_t _rx_buffer_head;
@@ -222,6 +228,8 @@ bool MSC_Data(uint8_t rx, uint8_t tx);
222228
int CDC_GetInterface(uint8_t* interfaceNum);
223229
int CDC_GetDescriptor(int i);
224230
bool CDC_Setup(Setup& setup);
231+
void CDC_LineEncodingEvent(void);
232+
void CDC_LineStateEvent(void);
225233

226234
//================================================================================
227235
//================================================================================

plugins/KeyboardioHID/USBCore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
#define CDC_GET_LINE_CODING 0x21
5757
#define CDC_SET_CONTROL_LINE_STATE 0x22
5858

59+
#define CDC_CONTROL_LINE_OUT_DTR (1 << 0)
60+
#define CDC_CONTROL_LINE_OUT_RTS (1 << 1)
61+
5962
#define MSC_RESET 0xFF
6063
#define MSC_GET_MAX_LUN 0xFE
6164

0 commit comments

Comments
 (0)