Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_CANManager/AP_CANSensor.h
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14/*15CANSensor class, for easy creation of CAN sensors using custom CAN protocols16*/1718#pragma once1920#include "AP_CAN.h"21#include "AP_CANDriver.h"22#ifndef HAL_BUILD_AP_PERIPH23#include "AP_CANManager.h"24#endif2526#if HAL_MAX_CAN_PROTOCOL_DRIVERS2728class CANSensor : public AP_CANDriver {29public:30CANSensor(const char *driver_name, uint16_t stack_size=2048);3132/* Do not allow copies */33CLASS_NO_COPY(CANSensor);3435void init(uint8_t driver_index, bool enable_filters) override;36bool add_interface(AP_HAL::CANIface* can_iface) override;3738// Return true if this sensor has been successfully registered to a driver and initialized.39bool initialized() const { return _initialized; }4041// handler for incoming frames42virtual void handle_frame(AP_HAL::CANFrame &frame) = 0;4344// handler for outgoing frames45bool write_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us);4647#ifdef HAL_BUILD_AP_PERIPH48static void set_periph(const uint8_t i, const AP_CAN::Protocol protocol, AP_HAL::CANIface* iface) {49if (i < ARRAY_SIZE(_periph)) {50_periph[i].protocol = protocol;51_periph[i].iface = iface;52}53}5455// return driver type index i56static AP_CAN::Protocol get_driver_type(const uint8_t i)57{58if (i < ARRAY_SIZE(_periph)) {59return _periph[i].protocol;60}61return AP_CAN::Protocol::None;62}63#else64static AP_CAN::Protocol get_driver_type(const uint8_t i) { return AP::can().get_driver_type(i); }65#endif6667protected:68void register_driver(AP_CAN::Protocol dtype);6970private:71void loop();7273const char *const _driver_name;74const uint16_t _stack_size;75bool _initialized;76uint8_t _driver_index;7778// this is true when we are setup as an auxillary driver using CAN_Dn_PROTOCOL279bool is_aux_11bit_driver;8081AP_CANDriver *_can_driver;82HAL_BinarySemaphore sem_handle;83AP_HAL::CANIface* _can_iface;8485#ifdef HAL_BUILD_AP_PERIPH86void register_driver_periph(const AP_CAN::Protocol dtype);8788struct CANSensor_Periph {89AP_HAL::CANIface* iface;90AP_CAN::Protocol protocol;91} static _periph[HAL_NUM_CAN_IFACES];92#endif93};9495// a class to allow for multiple CAN backends with one96// CANSensor driver. This can be shared among different libraries like rangefinder and proximity97class MultiCAN : public CANSensor {98public:99// callback functor def for forwarding frames100FUNCTOR_TYPEDEF(ForwardCanFrame, bool, AP_HAL::CANFrame &);101102MultiCAN(ForwardCanFrame cf, AP_CAN::Protocol can_type, const char *driver_name);103104// handle a received frame from the CAN bus105void handle_frame(AP_HAL::CANFrame &frame) override;106107private:108// class to allow for multiple callbacks implemented as a linked list109class MultiCANLinkedList {110public:111struct CANSensor_Multi {112ForwardCanFrame _callback;113CANSensor_Multi* next = nullptr;114};115116// register a callback for a CAN frame by adding it to the linked list117void register_callback(ForwardCanFrame callback);118119// distribute the CAN frame to the registered callbacks120void handle_frame(AP_HAL::CANFrame &frame);121HAL_Semaphore sem;122123private:124CANSensor_Multi* head = nullptr;125};126127// Pointer to static instance of the linked list for persistence across instances128static MultiCANLinkedList* callbacks;129};130131#endif // HAL_MAX_CAN_PROTOCOL_DRIVERS132133134135