Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add HardwareCAN - a abstract base class for implementing CAN interfaces across Arduino cores.#185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
facchinm merged 1 commit intomasterfromcan-api
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletionsapi/CanMsg.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/

#ifndef ARDUINOCORE_API_CAN_MSG_H_
#define ARDUINOCORE_API_CAN_MSG_H_

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <cstdlib>
#include <cstdint>
#include <cstring>

#include <Arduino.h>

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespace arduino
{

/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/

class CanMsg : public Printable
{
public:
static size_t constexpr MAX_DATA_LENGTH = 8;

CanMsg(uint32_t const can_id, uint8_t const can_data_len, uint8_t const * can_data_ptr)
: id{can_id}
, data_length{can_data_len}
, data{0}
{
memcpy(data, can_data_ptr, min(can_data_len, MAX_DATA_LENGTH));
}

CanMsg() : CanMsg(0, 0, nullptr) { }

CanMsg(CanMsg const & other)
{
this->id = other.id;
this->data_length = other.data_length;
memcpy(this->data, other.data, this->data_length);
}

virtual ~CanMsg() { }

void operator = (CanMsg const & other)
{
if (this == &other)
return;

this->id = other.id;
this->data_length = other.data_length;
memcpy(this->data, other.data, this->data_length);
}

virtual size_t printTo(Print & p) const override
{
char buf[20] = {0};
size_t len = 0;

/* Print the header. */
len = snprintf(buf, sizeof(buf), "[%08X] (%d) : ", id, data_length);
size_t n = p.write(buf, len);

/* Print the data. */
for (size_t d = 0; d < data_length; d++)
{
len = snprintf(buf, sizeof(buf), "%02X", data[d]);
n += p.write(buf, len);
}

/* Wrap up. */
return n;
}

uint32_t id;
uint8_t data_length;
uint8_t data[MAX_DATA_LENGTH];
};

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

} /* arduino */

#endif /* ARDUINOCORE_API_CAN_MSG_H_ */
62 changes: 62 additions & 0 deletionsapi/CanMsgRingbuffer.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include "CanMsgRingbuffer.h"

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespace arduino
{

/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/

CanMsgRingbuffer::CanMsgRingbuffer()
: _head{0}
, _tail{0}
, _num_elems{0}
{
}

/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/

void CanMsgRingbuffer::enqueue(CanMsg const & msg)
{
if (isFull())
return;

_buf[_head] = msg;
_head = next(_head);
_num_elems++;
}

CanMsg CanMsgRingbuffer::dequeue()
{
if (isEmpty())
return CanMsg();

CanMsg const msg = _buf[_tail];
_tail = next(_tail);
_num_elems--;

return msg;
}

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

} /* arduino */
60 changes: 60 additions & 0 deletionsapi/CanMsgRingbuffer.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/

#ifndef ARDUINOCORE_API_CAN_MSG_RING_BUFFER_H_
#defineARDUINOCORE_API_CAN_MSG_RING_BUFFER_H_

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include<cstdint>

#include"CanMsg.h"

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespacearduino
{

/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/

classCanMsgRingbuffer
{
public:
staticsize_tconstexpr RING_BUFFER_SIZE =32U;

CanMsgRingbuffer();

inlineboolisFull()const {return (_num_elems == RING_BUFFER_SIZE); }
voidenqueue(CanMsgconst & msg);

inlineboolisEmpty()const {return (_num_elems ==0); }
CanMsgdequeue();

inlinesize_tavailable()const {return _num_elems; }

private:
CanMsg _buf[RING_BUFFER_SIZE];
volatilesize_t _head;
volatilesize_t _tail;
volatilesize_t _num_elems;

inlinesize_tnext(size_tconst idx)const {return ((idx +1) % RING_BUFFER_SIZE); }
};

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

}/* arduino*/

#endif/* ARDUINOCORE_API_CAN_MSG_RING_BUFFER_H_*/
62 changes: 62 additions & 0 deletionsapi/HardwareCAN.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/

#ifndef ARDUINOCORE_API_HARDWARECAN_H
#define ARDUINOCORE_API_HARDWARECAN_H

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include "CanMsg.h"
#include "CanMsgRingbuffer.h"

/**************************************************************************************
* TYPEDEF
**************************************************************************************/

enum class CanBitRate : int
{
BR_125k = 125000,
BR_250k = 250000,
BR_500k = 500000,
BR_1000k = 1000000,
};

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespace arduino
{

/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/

class HardwareCAN
{
public:
virtual ~HardwareCAN() {}


virtual bool begin(CanBitRate const can_bitrate) = 0;
virtual void end() = 0;


virtual int write(CanMsg const &msg) = 0;
virtual size_t available() = 0;
virtual CanMsg read() = 0;
};

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

} /* arduino */

#endif /* ARDUINOCORE_API_HARDWARECAN_H */

[8]ページ先頭

©2009-2025 Movatter.jp