WPILibC++  2018.4.1-20180920010257-1187-gd2a5aaa
 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/CANAPI.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 
68  void WritePacket(const uint8_t* data, int length, int apiId);
69 
80  void WritePacketRepeating(const uint8_t* data, int length, int apiId,
81  int repeatMs);
82 
88  void StopPacketRepeating(int apiId);
89 
99  bool ReadPacketNew(int apiId, CANData* data);
100 
109  bool ReadPacketLatest(int apiId, CANData* data);
110 
120  bool ReadPacketTimeout(int apiId, int timeoutMs, CANData* data);
121 
136  bool ReadPeriodicPacket(int apiId, int timeoutMs, int periodMs,
137  CANData* data);
138 
139  static constexpr HAL_CANManufacturer kTeamManufacturer = HAL_CAN_Man_kTeamUse;
140  static constexpr HAL_CANDeviceType kTeamDeviceType =
141  HAL_CAN_Dev_kMiscellaneous;
142 
143  private:
144  HAL_CANHandle m_handle{HAL_kInvalidHandle};
145 };
146 } // 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.