-
Notifications
You must be signed in to change notification settings - Fork 8
/
carduino.h
128 lines (115 loc) · 3.28 KB
/
carduino.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef CARSYSTEM_H
#define CARSYSTEM_H
#include "binarydata.h"
#include "canpacket.h"
#include <SPI.h>
#include <mcp_can.h>
#include <everytime.h>
class CarSystem {
protected:
uint8_t _id;
BinaryData* _state;
public:
CarSystem(uint8_t id, uint8_t len){
_id = id;
_state = new BinaryData(len);
};
virtual ~CarSystem() {
delete _state;
}
void serialize();
};
////////////////////////////////////////////
class ClimateControl : public CarSystem {
public:
ClimateControl() : CarSystem(0x63, 3) {}
void setAc(bool state);
void setAuto(bool state);
void setAirductWindshield(bool state);
void setAirductFace(bool state);
void setAirductFeet(bool state);
void setWindshieldHeating(bool state);
void setRearWindowHeating(bool state);
void setRecirculation(bool state);
void setFanLevel(uint8_t fans);
void setDesiredTemperature(uint8_t temperature);
};
class GearBox : public CarSystem {
public:
GearBox() : CarSystem(0x67, 2) {}
void setGear(int8_t gear);
void setSynchroRev(bool state);
};
////////////////////////////////////////////////////////////////////////
class CarSystemCanConnector {
protected:
CarSystem* _system;
public:
CarSystemCanConnector(CarSystem* systemInstance) { _system = systemInstance; }
virtual void readCan(CanPacket* packet) = 0;
virtual void writeCan(MCP_CAN* mcp) {};
virtual ~CarSystemCanConnector() {};
};
////////////////////////////////////////////////////////////////////////
class CarConnector {
private:
uint8_t _canInterruptPin = 2;
boolean _isInitialized = false;
MCP_CAN* _can;
boolean _sniff = false;
public:
CarConnector(uint8_t canInterruptPin, uint8_t canCsPin) {
_can = new MCP_CAN(canCsPin);
_canInterruptPin = canInterruptPin;
}
virtual ~CarConnector() {
delete _can;
}
boolean setup() {
uint8_t canStatus = _can->begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ);
if (canStatus == CAN_OK) {
_can->setMode(MCP_NORMAL);
pinMode(_canInterruptPin, INPUT);
_isInitialized = true;
}
return _isInitialized;
}
void update() {
if (!_isInitialized) {
return;
}
if (!digitalRead(_canInterruptPin)) {
CanPacket* packet = CanPacket::fromMcp(_can);
updateFromCan(packet);
if (_sniff) {
BinaryData* data = packet->getData();
uint8_t packetSize = data->getSize();
unsigned long int packetId = htonl(packet->getId());
int packetIdLenght = sizeof(packetId);
Serial.write("{b");
Serial.write(0x6d);
Serial.write(packetSize + packetIdLenght);
Serial.write((byte*)&packetId, packetIdLenght);
for (int i = 0; i < packetSize; i++) {
Serial.write(data->readByte(i).data);
}
Serial.write("}");
}
delete packet;
}
if (!_sniff) {
broadcast(_can);
}
}
void startSniffer() {
_sniff = true;
}
void stopSniffer() {
_sniff = false;
}
protected:
virtual void updateFromSerial(BinaryBuffer* serialData) = 0;
virtual void updateFromCan(CanPacket* packet) = 0;
virtual void broadcast(MCP_CAN* can) = 0;
};
#endif