forked from microbit-more/pxt-mbit-more-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MbitMoreSerial.h
109 lines (95 loc) · 2.18 KB
/
MbitMoreSerial.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "MbitMoreCommon.h"
#if MBIT_MORE_USE_SERIAL
#ifndef MBIT_MORE_SERIAL_H
#define MBIT_MORE_SERIAL_H
#include "MbitMoreDevice.h"
#define MM_SFD 0xff
#define MM_RX_BUFFER_SIZE 254
#define MM_TX_BUFFER_SIZE 254
// // Forward declaration
class MbitMoreDevice;
/**
* Class definition for main logics of Microbit More Service except bluetooth connectivity.
*
*/
class MbitMoreSerial {
private:
/**
* @brief Communication route between Scratch and micro:bit
*
*/
enum MbitMoreCommunicationRoute
{
BLE = 0,
SERIAL = 1,
};
/**
* @brief Request type from Scratch
*
*/
enum ChRequest
{
REQ_READ = 0x01,
REQ_WRITE = 0x10,
REQ_WRITE_RESPONSE = 0x11,
REQ_NOTIFY_STOP = 0x20,
REQ_NOTIFY_START = 0x21,
};
/**
* @brief Response type to Scratch
*
*/
enum ChResponse
{
RES_READ = 0x01,
RES_WRITE = 0x11,
RES_NOTIFY = 0x21,
};
public:
/**
* @brief Microbit More object.
*
*/
MbitMoreDevice &mbitMore;
/**
* @brief Construct a new Microbit More Serial service
*
* @param _mbitMore An instance of Microbit More device controller
*/
MbitMoreSerial(MbitMoreDevice &_mbitMore);
/**
* @brief Send a response for read request.
*
* @param ch Characteristeic of the request
* @param dataBuffer Buffer to send
* @param len Length of the buffer to send
*/
void readResponseOnSerial(uint16_t ch, uint8_t *dataBuffer, size_t len);
/**
* @brief Send a response for write request.
*
* @param ch Characteristic of the request
* @param response Response for the request
*/
void writeResponseOnSerial(uint16_t ch, bool response);
/**
* @brief Notify data of the characteristic
*
* @param ch Characteristic to notify
* @param dataBuffer Buffer to notify
* @param len Length of the buffer to notify
*/
void notifyOnSerial(uint16_t ch, uint8_t *dataBuffer, size_t len);
/**
* @brief Start continuous receiving process from serial port.
*
*/
void startSerialReceiving();
/**
* @brief Start continuous updating process to serial port.
*
*/
void startSerialUpdating();
};
#endif // MBIT_MORE_SERIAL_H
#endif // MBIT_MORE_USE_SERIAL