forked from Zidit/partyhat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.h
64 lines (47 loc) · 1.43 KB
/
serial.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef INCLUDE_SERIAL
#define INCLUDE_SERIAL
#include "circular_buffer.h"
#include <avr/pgmspace.h>
#undef PSTR
#define PSTR(s) (__extension__({static const char __c[] __attribute__((section (".progmem.data"))) = (s); &__c[0];}))
class uart {
public:
uart(USART_t* usart, const uint32_t baud);
void sendChar(const char c);
void sendString(const char *str);
void sendStringPgm(const char *str);
uint8_t sendStringNonBlocking(const char *str);
uint8_t sendStringPgmNonBlocking(const char *str);
void sendInt (const int8_t i);
void sendInt (const int16_t i);
void sendInt (const int32_t i);
void sendInt (const uint8_t i);
void sendInt (const uint16_t i);
void sendInt (const uint32_t i);
void sendHex (const uint8_t i);
void sendHex (const uint16_t i);
void sendHex (const uint32_t i);
void sendHex (const int8_t i);
void sendHex (const int16_t i);
void sendHex (const int32_t i);
char getChar();
uint8_t dataAvailable();
void flush();
uint8_t setBaudRate(const uint32_t baud);
void rxInterrupt()
{
rxBuffer.write(reg->DATA);
}
void txInterrupt()
{
if (txBuffer.isEmpty())
reg->CTRLA = USART_RXCINTLVL_HI_gc | USART_DREINTLVL_OFF_gc;
else
reg->DATA = txBuffer.read();
}
private:
circularBuffer<char, 128> rxBuffer;
circularBuffer<char, 64> txBuffer;
USART_t * const reg;
};
#endif