Skip to content

Commit

Permalink
Merge pull request #2 from caternuson/updates
Browse files Browse the repository at this point in the history
Update examples
  • Loading branch information
caternuson committed Apr 7, 2023
2 parents c628716 + e447e7b commit b594476
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 106 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Sandeep Mistry

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file.
9 changes: 5 additions & 4 deletions examples/feather_m4can_onreceive/feather_m4can_onreceive.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
CANSAME5x CAN;

void setup() {
Serial.begin(9600);
while (!Serial);
Serial.begin(115200);
while (!Serial) delay(10);

Serial.println("CAN Receiver Callback");

// start the CAN bus at 250 kbps
if (!CAN.begin(250E3)) {
if (!CAN.begin(250000)) {
Serial.println("Starting CAN failed!");
while (1);
while (1) delay(10);
}
Serial.println("Starting CAN!");

// register the receive callback
CAN.onReceive(onReceive);
Expand Down
Empty file.
7 changes: 4 additions & 3 deletions examples/feather_m4can_rx/feather_m4can_rx.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
CANSAME5x CAN;

void setup() {
Serial.begin(9600);
while (!Serial);
Serial.begin(115200);
while (!Serial) delay(10);

Serial.println("CAN Receiver");

Expand All @@ -20,8 +20,9 @@ void setup() {
// start the CAN bus at 250 kbps
if (!CAN.begin(250000)) {
Serial.println("Starting CAN failed!");
while (1);
while (1) delay(10);
}
Serial.println("Starting CAN!");
}

void loop() {
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
CANSAME5x CAN;

void setup() {
Serial.begin(9600);
while (!Serial);
Serial.begin(115200);
while (!Serial) delay(10);

Serial.println("CAN Sender");

pinMode(PIN_CAN_STANDBY, OUTPUT);
digitalWrite(PIN_CAN_STANDBY, false); // turn off STANDBY
pinMode(PIN_CAN_BOOSTEN, OUTPUT);
Expand All @@ -19,8 +20,9 @@ void setup() {
// start the CAN bus at 250 kbps
if (!CAN.begin(250000)) {
Serial.println("Starting CAN failed!");
while (1);
while (1) delay(10);
}
Serial.println("Starting CAN!");
}

void loop() {
Expand Down
130 changes: 36 additions & 94 deletions src/CANController.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
// Forked from:
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Licensed under the MIT license. See LICENSE file in the project root for full
// license information.

#include "CANController.h"

CANControllerClass::CANControllerClass() :
_onReceive(NULL),

_packetBegun(false),
_txId(-1),
_txExtended(-1),
_txRtr(false),
_txDlc(0),
_txLength(0),

_rxId(-1),
_rxExtended(false),
_rxRtr(false),
_rxDlc(0),
_rxLength(0),
_rxIndex(0)
{
CANControllerClass::CANControllerClass()
: _onReceive(NULL),

_packetBegun(false), _txId(-1), _txExtended(-1), _txRtr(false), _txDlc(0),
_txLength(0),

_rxId(-1), _rxExtended(false), _rxRtr(false), _rxDlc(0), _rxLength(0),
_rxIndex(0) {
// overide Stream timeout value
setTimeout(0);
}

CANControllerClass::~CANControllerClass()
{
}
CANControllerClass::~CANControllerClass() {}

int CANControllerClass::begin(long /*baudRate*/)
{
int CANControllerClass::begin(long /*baudRate*/) {
_packetBegun = false;
_txId = -1;
_txRtr =false;
_txRtr = false;
_txDlc = 0;
_txLength = 0;

Expand All @@ -45,12 +35,9 @@ int CANControllerClass::begin(long /*baudRate*/)
return 1;
}

void CANControllerClass::end()
{
}
void CANControllerClass::end() {}

int CANControllerClass::beginPacket(int id, int dlc, bool rtr)
{
int CANControllerClass::beginPacket(int id, int dlc, bool rtr) {
if (id < 0 || id > 0x7FF) {
return 0;
}
Expand All @@ -71,8 +58,7 @@ int CANControllerClass::beginPacket(int id, int dlc, bool rtr)
return 1;
}

int CANControllerClass::beginExtendedPacket(long id, int dlc, bool rtr)
{
int CANControllerClass::beginExtendedPacket(long id, int dlc, bool rtr) {
if (id < 0 || id > 0x1FFFFFFF) {
return 0;
}
Expand All @@ -93,8 +79,7 @@ int CANControllerClass::beginExtendedPacket(long id, int dlc, bool rtr)
return 1;
}

int CANControllerClass::endPacket()
{
int CANControllerClass::endPacket() {
if (!_packetBegun) {
return 0;
}
Expand All @@ -107,38 +92,21 @@ int CANControllerClass::endPacket()
return 1;
}

int CANControllerClass::parsePacket()
{
return 0;
}
int CANControllerClass::parsePacket() { return 0; }

long CANControllerClass::packetId()
{
return _rxId;
}
long CANControllerClass::packetId() { return _rxId; }

bool CANControllerClass::packetExtended()
{
return _rxExtended;
}
bool CANControllerClass::packetExtended() { return _rxExtended; }

bool CANControllerClass::packetRtr()
{
return _rxRtr;
}
bool CANControllerClass::packetRtr() { return _rxRtr; }

int CANControllerClass::packetDlc()
{
return _rxDlc;
}
int CANControllerClass::packetDlc() { return _rxDlc; }

size_t CANControllerClass::write(uint8_t byte)
{
size_t CANControllerClass::write(uint8_t byte) {
return write(&byte, sizeof(byte));
}

size_t CANControllerClass::write(const uint8_t *buffer, size_t size)
{
size_t CANControllerClass::write(const uint8_t *buffer, size_t size) {
if (!_packetBegun) {
return 0;
}
Expand All @@ -153,64 +121,38 @@ size_t CANControllerClass::write(const uint8_t *buffer, size_t size)
return size;
}

int CANControllerClass::available()
{
return (_rxLength - _rxIndex);
}
int CANControllerClass::available() { return (_rxLength - _rxIndex); }

int CANControllerClass::read()
{
int CANControllerClass::read() {
if (!available()) {
return -1;
}

return _rxData[_rxIndex++];
}

int CANControllerClass::peek()
{
int CANControllerClass::peek() {
if (!available()) {
return -1;
}

return _rxData[_rxIndex];
}

void CANControllerClass::flush()
{
}
void CANControllerClass::flush() {}

void CANControllerClass::onReceive(void(*callback)(int))
{
void CANControllerClass::onReceive(void (*callback)(int)) {
_onReceive = callback;
}

int CANControllerClass::filter(int /*id*/, int /*mask*/)
{
return 0;
}
int CANControllerClass::filter(int /*id*/, int /*mask*/) { return 0; }

int CANControllerClass::filterExtended(long /*id*/, long /*mask*/)
{
return 0;
}
int CANControllerClass::filterExtended(long /*id*/, long /*mask*/) { return 0; }

int CANControllerClass::observe()
{
return 0;
}
int CANControllerClass::observe() { return 0; }

int CANControllerClass::loopback()
{
return 0;
}
int CANControllerClass::loopback() { return 0; }

int CANControllerClass::sleep()
{
return 0;
}
int CANControllerClass::sleep() { return 0; }

int CANControllerClass::wakeup()
{
return 0;
}
int CANControllerClass::wakeup() { return 0; }
6 changes: 4 additions & 2 deletions src/CANController.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Forked from:
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Licensed under the MIT license. See LICENSE file in the project root for full
// license information.

#ifndef CAN_CONTROLLER_H
#define CAN_CONTROLLER_H
Expand Down Expand Up @@ -32,7 +34,7 @@ class CANControllerClass : public Stream {
virtual int peek();
virtual void flush();

virtual void onReceive(void(*callback)(int));
virtual void onReceive(void (*callback)(int));

virtual int filter(int id) { return filter(id, 0x7ff); }
virtual int filter(int id, int mask);
Expand Down

0 comments on commit b594476

Please sign in to comment.