CANTram Framework 1.0.1
Dieses Framework ermöglicht die Nutzung der CANTram Hardware, einer Mikrocontroller basierten mdoularen Steuerung.
Loading...
Searching...
No Matches
SPIChip.h
1#ifndef SPICHIP_H
2#define SPICHIP_H
3
4#include "Debug.h"
5
6using namespace std;
7
8/* Base class for SPI devices in CANTram Controller. This class makes it possible to use external chip select since the SPI device may be selected via a shift register.*/
9class SPIChip {
10 public:
11
12 void activateExternalChipSelect() {
13 useExternalChipSelect = true;
14 }
15 void setCSControlCallback(std::function<void(bool)> callback) {
16 csControl = callback;
17 useExternalChipSelect = true;
18 }
19 void setCSPin(int csPin) {
20 useExternalChipSelect = false;
21 _csPin = csPin;
22 pinMode(_csPin, OUTPUT);
23 digitalWrite(_csPin, HIGH); // Set CS high to deselect the device
24 }
25
26 protected:
27 int _csPin;
28 bool useExternalChipSelect = false;
29 SPIChip() = default;
30 SPIChip(uint8_t csPin) : _csPin(csPin) {
31 useExternalChipSelect = false;
32 pinMode(_csPin, OUTPUT);
33 digitalWrite(_csPin, HIGH); // Set CS high to deselect the device
34 }
35 std::function<void(bool)> csControl = nullptr;
36 void selectChip() {
37 CHIP_PRINTLN("[SPIChip] Select chip");
38 if(!SPIChip::useExternalChipSelect){
39 CHIP_PRINTLN("[SPIChip] Using internal chip select");
40 digitalWrite(_csPin, LOW); // Set CS low
41 }
42 else{
43 CHIP_PRINTLN("[SPIChip] Using external chip select");
44 SPIChip::csControl(LOW);
45 }
46 }
47 void deselectChip() {
48 CHIP_PRINTLN("[SPIChip] Deselect chip");
49 if(!SPIChip::useExternalChipSelect){
50 CHIP_PRINTLN("[SPIChip] Using internal chip select");
51 digitalWrite(_csPin, HIGH); // Set CS high
52 }
53 else{
54 CHIP_PRINTLN("[SPIChip] Using external chip select");
55 SPIChip::csControl(HIGH);
56 }
57 }
58
59
60};
61#endif
Logging and debug macro definitions for CANTramCore.
#define CHIP_PRINTLN(x)
Print a chip-level message with newline.
Definition Debug.h:233