Skip to content

Commit 868820b

Browse files
committed
Reduce RAM usage
1 parent 3fd930f commit 868820b

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

CanHacker.cpp

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ CanHacker::CanHacker(Stream *stream, Stream *debugStream, uint8_t cs) {
3838
_stream = stream;
3939
_debugStream = debugStream;
4040

41-
writeDebugStream("Initialization\n");
41+
writeDebugStream(F("Initialization\n"));
4242

4343
_cs = cs;
4444
mcp2515 = new MCP2515(_cs);
@@ -57,7 +57,7 @@ Stream *CanHacker::getInterfaceStream() {
5757
CanHacker::ERROR CanHacker::connectCan() {
5858
MCP2515::ERROR error = mcp2515->setBitrate(bitrate);
5959
if (error != MCP2515::ERROR_OK) {
60-
writeDebugStream("setBitrate error:\n");
60+
writeDebugStream(F("setBitrate error:\n"));
6161
writeDebugStream((int)error);
6262
writeDebugStream("\n");
6363
return ERROR_MCP2515_INIT_BITRATE;
@@ -145,60 +145,60 @@ uint16_t CanHacker::getTimestamp() {
145145

146146
CanHacker::ERROR CanHacker::receiveSetBitrateCommand(const char *buffer, const int length) {
147147
if (isConnected()) {
148-
writeDebugStream("Bitrate command cannot be called while connected\n");
148+
writeDebugStream(F("Bitrate command cannot be called while connected\n"));
149149
writeStream(BEL);
150150
return ERROR_CONNECTED;
151151
}
152152

153153
if (length < 2) {
154154
writeStream(BEL);
155-
writeDebugStream("Bitrate command must by 2 bytes long\n");
155+
writeDebugStream(F("Bitrate command must by 2 bytes long\n"));
156156
writeDebugStream((const uint8_t*)buffer, length);
157157
writeDebugStream('\n');
158158
return ERROR_INVALID_COMMAND;
159159
}
160160
switch(buffer[1]) {
161161
case '0':
162-
writeDebugStream("Set bitrate 10KBPS\n");
162+
writeDebugStream(F("Set bitrate 10KBPS\n"));
163163
bitrate = CAN_10KBPS;
164164
break;
165165
case '1':
166-
writeDebugStream("Set bitrate 20KBPS\n");
166+
writeDebugStream(F("Set bitrate 20KBPS\n"));
167167
bitrate = CAN_20KBPS;
168168
break;
169169
case '2':
170-
writeDebugStream("Set bitrate 50KBPS\n");
170+
writeDebugStream(F("Set bitrate 50KBPS\n"));
171171
bitrate = CAN_50KBPS;
172172
break;
173173
case '3':
174-
writeDebugStream("Set bitrate 100KBPS\n");
174+
writeDebugStream(F("Set bitrate 100KBPS\n"));
175175
bitrate = CAN_100KBPS;
176176
break;
177177
case '4':
178-
writeDebugStream("Set bitrate 125KBPS\n");
178+
writeDebugStream(F("Set bitrate 125KBPS\n"));
179179
bitrate = CAN_125KBPS;
180180
break;
181181
case '5':
182-
writeDebugStream("Set bitrate 250KBPS\n");
182+
writeDebugStream(F("Set bitrate 250KBPS\n"));
183183
bitrate = CAN_250KBPS;
184184
break;
185185
case '6':
186-
writeDebugStream("Set bitrate 500KBPS\n");
186+
writeDebugStream(F("Set bitrate 500KBPS\n"));
187187
bitrate = CAN_500KBPS;
188188
break;
189189
case '7':
190-
writeDebugStream("Bitrate 7 is not supported\n");
190+
writeDebugStream(F("Bitrate 7 is not supported\n"));
191191
writeDebugStream((const uint8_t*)buffer, length);
192192
writeDebugStream('\n');
193193
writeStream(BEL);
194194
return ERROR_INVALID_COMMAND;
195195
break;
196196
case '8':
197-
writeDebugStream("Set bitrate 1000KBPS\n");
197+
writeDebugStream(F("Set bitrate 1000KBPS\n"));
198198
bitrate = CAN_1000KBPS;
199199
break;
200200
default:
201-
writeDebugStream("Unexpected bitrate\n");
201+
writeDebugStream(F("Unexpected bitrate\n"));
202202
writeDebugStream((const uint8_t*)buffer, length);
203203
writeDebugStream('\n');
204204
writeStream(BEL);
@@ -211,7 +211,8 @@ CanHacker::ERROR CanHacker::receiveSetBitrateCommand(const char *buffer, const i
211211

212212
CanHacker::ERROR CanHacker::processInterrupt() {
213213
if (!isConnected()) {
214-
writeDebugStream("Process interrupt while not connected\n");
214+
return ERROR_OK;
215+
writeDebugStream(F("Process interrupt while not connected\n"));
215216
return ERROR_NOT_CONNECTED;
216217
}
217218

@@ -244,19 +245,19 @@ CanHacker::ERROR CanHacker::processInterrupt() {
244245

245246

246247
if (irq & MCP2515::CANINTF_WAKIF) {
247-
_debugStream->print("MCP_WAKIF\r\n");
248+
_debugStream->print(F("MCP_WAKIF\r\n"));
248249
mcp2515->clearInterrupts();
249250
}
250251

251252
if (irq & MCP2515::CANINTF_ERRIF) {
252-
_debugStream->print("ERRIF\r\n");
253+
_debugStream->print(F("ERRIF\r\n"));
253254

254255
//return ERROR_MCP2515_MERRF;
255256
mcp2515->clearMERR();
256257
}
257258

258259
if (irq & MCP2515::CANINTF_MERRF) {
259-
_debugStream->print("MERRF\r\n");
260+
_debugStream->print(F("MERRF\r\n"));
260261

261262
//return ERROR_MCP2515_MERRF;
262263
mcp2515->clearInterrupts();
@@ -267,7 +268,7 @@ CanHacker::ERROR CanHacker::processInterrupt() {
267268

268269
CanHacker::ERROR CanHacker::setFilter(const uint32_t filter) {
269270
if (isConnected()) {
270-
writeDebugStream("Filter cannot be set while connected\n");
271+
writeDebugStream(F("Filter cannot be set while connected\n"));
271272
return ERROR_CONNECTED;
272273
}
273274

@@ -284,7 +285,7 @@ CanHacker::ERROR CanHacker::setFilter(const uint32_t filter) {
284285

285286
CanHacker::ERROR CanHacker::setFilterMask(const uint32_t mask) {
286287
if (isConnected()) {
287-
writeDebugStream("Filter mask cannot be set while connected\n");
288+
writeDebugStream(F("Filter mask cannot be set while connected\n"));
288289
return ERROR_CONNECTED;
289290
}
290291

@@ -331,6 +332,13 @@ CanHacker::ERROR CanHacker::writeDebugStream(const char *buffer) {
331332
return ERROR_OK;
332333
}
333334

335+
CanHacker::ERROR CanHacker::writeDebugStream(const __FlashStringHelper *ifsh) {
336+
if (_debugStream != NULL) {
337+
_debugStream->print(ifsh);
338+
}
339+
return ERROR_OK;
340+
}
341+
334342
CanHacker::ERROR CanHacker::writeDebugStream(const uint8_t *buffer, size_t size) {
335343
if (_debugStream != NULL) {
336344
_debugStream->write(buffer, size);
@@ -383,10 +391,10 @@ CanHacker::ERROR CanHacker::receiveCommand(const char *buffer, const int length)
383391
case COMMAND_SET_BTR: {
384392
if (isConnected()) {
385393
writeStream(BEL);
386-
writeDebugStream("SET_BTR command cannot be called while connected\n");
394+
writeDebugStream(F("SET_BTR command cannot be called while connected\n"));
387395
return ERROR_CONNECTED;
388396
}
389-
writeDebugStream("SET_BTR not supported\n");
397+
writeDebugStream(F("SET_BTR not supported\n"));
390398
return writeStream(CR);
391399
}
392400

@@ -405,7 +413,7 @@ CanHacker::ERROR CanHacker::receiveCommand(const char *buffer, const int length)
405413
case COMMAND_READ_ECR:
406414
case COMMAND_READ_ALCR: {
407415
if (!isConnected()) {
408-
writeDebugStream("Read status, ecr, alcr while not connected\n");
416+
writeDebugStream(F("Read status, ecr, alcr while not connected\n"));
409417
writeStream(BEL);
410418
return ERROR_NOT_CONNECTED;
411419
}
@@ -414,7 +422,7 @@ CanHacker::ERROR CanHacker::receiveCommand(const char *buffer, const int length)
414422

415423
default: {
416424
writeStream(BEL);
417-
writeDebugStream("Unknown command received\n");
425+
writeDebugStream(F("Unknown command received\n"));
418426
writeDebugStream((const uint8_t*)buffer, length);
419427
writeDebugStream('\n');
420428
return ERROR_UNKNOWN_COMMAND;
@@ -433,7 +441,7 @@ CanHacker::ERROR CanHacker::receiveCanFrame(const struct can_frame *frame) {
433441

434442
CanHacker::ERROR CanHacker::parseTransmit(const char *buffer, int length, struct can_frame *frame) {
435443
if (length < MIN_MESSAGE_LENGTH) {
436-
writeDebugStream("Transmit message lenght < minimum\n");
444+
writeDebugStream(F("Transmit message lenght < minimum\n"));
437445
writeDebugStream((const uint8_t*)buffer, length);
438446
writeDebugStream('\n');
439447
return ERROR_INVALID_COMMAND;
@@ -456,7 +464,7 @@ CanHacker::ERROR CanHacker::parseTransmit(const char *buffer, int length, struct
456464
isRTR = 1;
457465
break;
458466
default:
459-
writeDebugStream("Unexpected type of transmit command\n");
467+
writeDebugStream(F("Unexpected type of transmit command\n"));
460468
writeDebugStream((const uint8_t*)buffer, length);
461469
writeDebugStream('\n');
462470
return ERROR_INVALID_COMMAND;
@@ -481,13 +489,13 @@ CanHacker::ERROR CanHacker::parseTransmit(const char *buffer, int length, struct
481489

482490
__u8 dlc = hexCharToByte(buffer[offset++]);
483491
if (dlc > 8) {
484-
writeDebugStream("DLC > 8\n");
492+
writeDebugStream(F("DLC > 8\n"));
485493
writeDebugStream((const uint8_t*)buffer, length);
486494
writeDebugStream('\n');
487495
return ERROR_INVALID_COMMAND;
488496
}
489497
if (dlc == 0) {
490-
writeDebugStream("DLC = 0\n");
498+
writeDebugStream(F("DLC = 0\n"));
491499
writeDebugStream((const uint8_t*)buffer, length);
492500
writeDebugStream('\n');
493501
return ERROR_INVALID_COMMAND;
@@ -557,7 +565,7 @@ CanHacker::ERROR CanHacker::sendFrame(const struct can_frame *frame) {
557565

558566
CanHacker::ERROR CanHacker::receiveTransmitCommand(const char *buffer, const int length) {
559567
if (!isConnected()) {
560-
writeDebugStream("Transmit command while not connected\n");
568+
writeDebugStream(F("Transmit command while not connected\n"));
561569
return ERROR_NOT_CONNECTED;
562570
}
563571

@@ -581,7 +589,7 @@ CanHacker::ERROR CanHacker::receiveTransmitCommand(const char *buffer, const int
581589
CanHacker::ERROR CanHacker::receiveTimestampCommand(const char *buffer, const int length) {
582590
if (length != 2) {
583591
writeStream(BEL);
584-
writeDebugStream("Timestamp command must be 2 bytes long\n");
592+
writeDebugStream(F("Timestamp command must be 2 bytes long\n"));
585593
writeDebugStream((const uint8_t*)buffer, length);
586594
writeDebugStream('\n');
587595
return ERROR_INVALID_COMMAND;
@@ -595,7 +603,7 @@ CanHacker::ERROR CanHacker::receiveTimestampCommand(const char *buffer, const in
595603
return writeStream(CR);
596604
default:
597605
writeStream(BEL);
598-
writeDebugStream("Timestamp cammand must have value 0 or 1\n");
606+
writeDebugStream(F("Timestamp cammand must have value 0 or 1\n"));
599607
writeDebugStream((const uint8_t*)buffer, length);
600608
writeDebugStream('\n');
601609
return ERROR_INVALID_COMMAND;
@@ -605,7 +613,7 @@ CanHacker::ERROR CanHacker::receiveTimestampCommand(const char *buffer, const in
605613
}
606614

607615
CanHacker::ERROR CanHacker::receiveCloseCommand(const char *buffer, const int length) {
608-
writeDebugStream("receiveCloseCommand\n");
616+
writeDebugStream(F("receiveCloseCommand\n"));
609617

610618
if (length != 1) {
611619
return ERROR_INVALID_COMMAND;
@@ -635,7 +643,7 @@ CanHacker::ERROR CanHacker::receiveOpenCommand(const char *buffer, const int len
635643
return ERROR_INVALID_COMMAND;
636644
}
637645

638-
writeDebugStream("receiveOpenCommand\n");
646+
writeDebugStream(F("receiveOpenCommand\n"));
639647
ERROR error = connectCan();
640648
if (error != ERROR_OK) {
641649
return error;
@@ -653,10 +661,10 @@ CanHacker::ERROR CanHacker::receiveListenOnlyCommand(const char *buffer, const i
653661
return ERROR_INVALID_COMMAND;
654662
}
655663

656-
writeDebugStream("receiveListenOnlyCommand\n");
664+
writeDebugStream(F("receiveListenOnlyCommand\n"));
657665
if (isConnected()) {
658666
writeStream(BEL);
659-
writeDebugStream("ListenOnly command cannot be called while connected\n");
667+
writeDebugStream(F("ListenOnly command cannot be called while connected\n"));
660668
return ERROR_CONNECTED;
661669
}
662670
_listenOnly = true;
@@ -666,7 +674,7 @@ CanHacker::ERROR CanHacker::receiveListenOnlyCommand(const char *buffer, const i
666674
CanHacker::ERROR CanHacker::receiveSetAcrCommand(const char *buffer, const int length) {
667675
if (length != 9) {
668676
writeStream(BEL);
669-
writeDebugStream("ACR command must by 9 bytes long\n");
677+
writeDebugStream(F("ACR command must by 9 bytes long\n"));
670678
writeDebugStream((const uint8_t*)buffer, length);
671679
writeDebugStream('\n');
672680
return ERROR_INVALID_COMMAND;
@@ -705,7 +713,7 @@ CanHacker::ERROR CanHacker::receiveSetAcrCommand(const char *buffer, const int l
705713
CanHacker::ERROR CanHacker::receiveSetAmrCommand(const char *buffer, const int length) {
706714
if (length != 9) {
707715
writeStream(BEL);
708-
writeDebugStream("AMR command must by 9 bytes long\n");
716+
writeDebugStream(F("AMR command must by 9 bytes long\n"));
709717
writeDebugStream((const uint8_t*)buffer, length);
710718
writeDebugStream('\n');
711719
return ERROR_INVALID_COMMAND;
@@ -743,7 +751,7 @@ CanHacker::ERROR CanHacker::receiveSetAmrCommand(const char *buffer, const int l
743751

744752
CanHacker::ERROR CanHacker::enableLoopback() {
745753
if (isConnected()) {
746-
writeDebugStream("Loopback cannot be changed while connected\n");
754+
writeDebugStream(F("Loopback cannot be changed while connected\n"));
747755
return ERROR_CONNECTED;
748756
}
749757

@@ -754,7 +762,7 @@ CanHacker::ERROR CanHacker::enableLoopback() {
754762

755763
CanHacker::ERROR CanHacker::disableLoopback() {
756764
if (isConnected()) {
757-
writeDebugStream("Loopback cannot be changed while connected\n");
765+
writeDebugStream(F("Loopback cannot be changed while connected\n"));
758766
return ERROR_CONNECTED;
759767
}
760768

CanHacker.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
class CanHacker {
2626
public:
27-
enum ERROR {
28-
ERROR_OK,
29-
ERROR_CONNECTED,
30-
ERROR_NOT_CONNECTED,
27+
enum ERROR {
28+
ERROR_OK,
29+
ERROR_CONNECTED,
30+
ERROR_NOT_CONNECTED,
3131
ERROR_UNKNOWN_COMMAND,
3232
ERROR_INVALID_COMMAND,
3333
ERROR_ERROR_FRAME_NOT_SUPPORTED,
@@ -73,7 +73,7 @@ class CanHacker {
7373
bool _isConnected = false;
7474
Stream *_stream;
7575
Stream *_debugStream;
76-
76+
7777
enum /*class*/ COMMAND : char {
7878
COMMAND_SET_BITRATE = 'S', // set CAN bit rate
7979
COMMAND_SET_BTR = 's', // set CAN bit rate via
@@ -96,10 +96,10 @@ class CanHacker {
9696
COMMAND_WRITE_REG = 'W', // write register content to SJA1000
9797
COMMAND_LISTEN_ONLY = 'L' // switch to listen only mode
9898
};
99-
99+
100100
ERROR parseTransmit(const char *buffer, int length, struct can_frame *frame);
101101
ERROR createTransmit(const struct can_frame *frame, char *buffer, const int length);
102-
102+
103103
uint16_t getTimestamp();
104104
ERROR setFilter(const uint32_t filter);
105105
ERROR setFilterMask(const uint32_t mask);
@@ -114,7 +114,8 @@ class CanHacker {
114114
ERROR writeDebugStream(const char *buffer);
115115
ERROR writeDebugStream(const int buffer);
116116
ERROR writeDebugStream(const uint8_t *buffer, size_t size);
117-
117+
ERROR writeDebugStream(const __FlashStringHelper *ifsh);
118+
118119
ERROR receiveSetBitrateCommand(const char *buffer, const int length);
119120
ERROR receiveTransmitCommand(const char *buffer, const int length);
120121
ERROR receiveTimestampCommand(const char *buffer, const int length);

0 commit comments

Comments
 (0)