WPILibC++  2019.1.1-beta-2-18-ga769d56
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
CAN.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <stdint.h>
11 
12 #include <hal/CANAPITypes.h>
13 #include <wpi/ArrayRef.h>
14 
15 #include "frc/ErrorBase.h"
16 
17 namespace frc {
18 struct CANData {
19  uint8_t data[8];
20  int32_t length;
21  uint64_t timestamp;
22 };
23 
34 class CAN : public ErrorBase {
35  public:
43  explicit CAN(int deviceId);
44 
54  CAN(int deviceId, int deviceManufacturer, int deviceType);
55 
59  ~CAN() override;
60 
61  CAN(CAN&& rhs);
62  CAN& operator=(CAN&& rhs);
63 
71  void WritePacket(const uint8_t* data, int length, int apiId);
72 
83  void WritePacketRepeating(const uint8_t* data, int length, int apiId,
84  int repeatMs);
85 
91  void StopPacketRepeating(int apiId);
92 
102  bool ReadPacketNew(int apiId, CANData* data);
103 
112  bool ReadPacketLatest(int apiId, CANData* data);
113 
123  bool ReadPacketTimeout(int apiId, int timeoutMs, CANData* data);
124 
139  bool ReadPeriodicPacket(int apiId, int timeoutMs, int periodMs,
140  CANData* data);
141 
142  static constexpr HAL_CANManufacturer kTeamManufacturer = HAL_CAN_Man_kTeamUse;
143  static constexpr HAL_CANDeviceType kTeamDeviceType =
144  HAL_CAN_Dev_kMiscellaneous;
145 
146  private:
147  HAL_CANHandle m_handle = HAL_kInvalidHandle;
148 };
149 } // namespace frc
CAN(int deviceId)
Create a new CAN communication interface with the specific device ID.
~CAN() override
Closes the CAN communication.
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
void WritePacket(const uint8_t *data, int length, int apiId)
Write a packet to the CAN device with a specific ID.
bool ReadPacketTimeout(int apiId, int timeoutMs, CANData *data)
Read a CAN packet.
void WritePacketRepeating(const uint8_t *data, int length, int apiId, int repeatMs)
Write a repeating packet to the CAN device with a specific ID.
High level class for interfacing with CAN devices conforming to the standard CAN spec.
Definition: CAN.h:34
bool ReadPacketLatest(int apiId, CANData *data)
Read a CAN packet.
bool ReadPacketNew(int apiId, CANData *data)
Read a new CAN packet.
Base class for most objects.
Definition: ErrorBase.h:74
Definition: CAN.h:18
bool ReadPeriodicPacket(int apiId, int timeoutMs, int periodMs, CANData *data)
Read a CAN packet.
void StopPacketRepeating(int apiId)
Stop a repeating packet with a specific ID.