-
Notifications
You must be signed in to change notification settings - Fork 3
/
NewCanbus.cpp
executable file
·98 lines (82 loc) · 2.12 KB
/
NewCanbus.cpp
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
#include <Arduino.h>
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <pins_arduino.h>
#include <inttypes.h>
#include <global.h>
#include <mcp2515.h>
#include <defaults.h>
#include <NewCanbus.h>
// Default filter -- all open
const prog_uint8_t all_open_filters[] PROGMEM =
{
// Group 0
MCP2515_FILTER(0), // Filter 0
MCP2515_FILTER(0), // Filter 1
// Group 1
MCP2515_FILTER_EXTENDED(0), // Filter 2
MCP2515_FILTER_EXTENDED(0), // Filter 3
MCP2515_FILTER_EXTENDED(0), // Filter 4
MCP2515_FILTER_EXTENDED(0), // Filter 5
MCP2515_FILTER(0), // Mask 0 (for group 0)
MCP2515_FILTER_EXTENDED(0), // Mask 1 (for group 1)
};
/* C++ wrapper */
NewCanbusClass::NewCanbusClass() {
}
void NewCanbusClass::set_filters(const prog_uint8_t *filter) {
mcp2515_static_filter(filter);
}
//
// Get the full message, including id and data
//
char NewCanbusClass::message_rx(tCAN *message) {
if (mcp2515_read_status(SPI_RX_STATUS)) {
uint8_t result = mcp2515_get_message(message);
if (result) {
return 1;
}
}
return 0;
}
char NewCanbusClass::message_rx(unsigned char *buffer) {
tCAN message;
if (mcp2515_check_message()) {
if (mcp2515_get_message(&message)) {
buffer[0] = message.data[0];
buffer[1] = message.data[1];
buffer[2] = message.data[2];
buffer[3] = message.data[3];
buffer[4] = message.data[4];
buffer[5] = message.data[5];
buffer[6] = message.data[6];
buffer[7] = message.data[7];
return 1;
}
}
return 0;
}
char NewCanbusClass::message_tx(uint16_t id, uint8_t data[]) {
tCAN message;
// May be a few defaults here that should be parameterized?
message.id = id;
message.header.rtr = 0;
message.header.length = 8;
message.data[0] = data[0];
message.data[1] = data[1];
message.data[2] = data[2];
message.data[3] = data[3];
message.data[4] = data[4];
message.data[5] = data[5];
message.data[6] = data[6];
message.data[7] = data[7];
if (mcp2515_send_message(&message)) {
return 1;
}
return 0;
}
char NewCanbusClass::init(unsigned char speed) {
return mcp2515_init(speed);
}
NewCanbusClass NewCanbus;