|
1 |
| -/* Brandon Matthews |
2 |
| - * Developed for Arduino UNO and MCP2515 |
3 |
| - * Modified for PKP-3500-SI-MT |
| 1 | +/** |
| 2 | + * @brief Example for PKP-3500-SI-MT on Arduino with MCP2515 |
| 3 | + * @author Brandon Matthews, Stefan Hirschenberger |
4 | 4 | */
|
5 | 5 |
|
6 |
| -// This library depends on and requires TimerOne library, SPI library, and autowp mcp2515 library v1.03 |
| 6 | +#include <Adafruit_MCP2515.h> |
7 | 7 | #include <BlinkMarinePkpCanOpen.h>
|
8 |
| -#include <mcp2515.h> |
9 | 8 |
|
10 |
| -#define CS_PIN 10 |
11 |
| -#define INTERRUPT_PIN 3 |
12 |
| -#define KEYPAD_BASE_ID 0x15 |
13 |
| -#define ENABLE_PASSCODE false |
| 9 | +//Prototype for hardware specific callback function |
| 10 | +uint8_t transmittMessageCallBack(const struct can_frame& txMsg); |
14 | 11 |
|
15 |
| -MCP2515 mcp2515(CS_PIN); |
16 |
| -PkpKeypad keypad(mcp2515, INTERRUPT_PIN, KEYPAD_BASE_ID, ENABLE_PASSCODE); |
| 12 | +#define KEYPAD_BASE_ID 0x15 |
| 13 | +#define CAN_BUS_BAUDRATE 125000 |
| 14 | +#define MCP_CS_PIN 10 |
| 15 | +#define MCP_INT_PIN 3 |
| 16 | +#define MCP_CLOCK_SPEED 8e6 //16e6 for 16MHz |
17 | 17 |
|
18 |
| -unsigned long currentMillis; |
19 |
| -unsigned long key9OnTime = 0; |
20 |
| -bool lastKey9State; |
| 18 | +Adafruit_MCP2515 can(MCP_CS_PIN); |
| 19 | +Pkp keypad(KEYPAD_BASE_ID, transmittMessageCallBack); |
21 | 20 |
|
22 | 21 | void setup() {
|
| 22 | + |
| 23 | + pinMode(LED_BUILTIN, OUTPUT); |
23 | 24 | Serial.begin(115200);
|
24 |
| - keypad.setSerial(&Serial); // Required for the keypad library to print things out to serial |
| 25 | + while (!Serial) { |
| 26 | + digitalWrite(LED_BUILTIN, (millis() % 500 > 250)); |
| 27 | + delay(10); |
| 28 | + if (millis() > 5000) { |
| 29 | + break; |
| 30 | + } |
| 31 | + } |
| 32 | + digitalWrite(LED_BUILTIN, LOW); |
| 33 | + Serial.println(F("PKP-3500-SI-MT Example Sketch started.")); |
| 34 | + |
| 35 | + // start the CAN bus at 250 kbps |
| 36 | + if (!can.begin(CAN_BUS_BAUDRATE)) { |
| 37 | + Serial.println("Starting CAN failed!"); |
| 38 | + while (1) { |
| 39 | + delay(10); |
| 40 | + } |
| 41 | + } |
| 42 | + Serial.println("Starting CAN!"); |
25 | 43 |
|
26 |
| - uint8_t keypadPasscode[4] = {1, 2, 3, 4}; |
27 |
| - keypad.setKeypadPassword(keypadPasscode); |
28 |
| - keypad.setKeyBrightness(70); |
29 |
| - keypad.setBacklight(BACKLIGHT_AMBER, 10); |
| 44 | + // register the receive callback |
| 45 | + can.onReceive(MCP_INT_PIN, onReceive); |
30 | 46 |
|
31 | 47 | // Set Key color and blink states
|
32 |
| - uint8_t colors1[4] = {PKP_COLOR_BLANK, PKP_COLOR_YELLOW, PKP_COLOR_BLANK, |
33 |
| - PKP_COLOR_YELLOW}; // array for the 4 possible key states' respective colors |
34 |
| - uint8_t blinks1[4] = {PKP_COLOR_BLANK, PKP_COLOR_BLANK, PKP_COLOR_CYAN, PKP_COLOR_BLANK}; |
35 |
| - |
36 |
| - keypad.setKeyColor(PKP_KEY_2, colors1, blinks1); |
37 |
| - keypad.setKeyColor(PKP_KEY_3, colors1, blinks1); |
38 |
| - keypad.setKeyColor(PKP_KEY_4, colors1, blinks1); |
39 |
| - keypad.setKeyColor(PKP_KEY_5, colors1, blinks1); |
40 |
| - keypad.setKeyColor(PKP_KEY_6, colors1, blinks1); |
41 |
| - colors1[1] = PKP_COLOR_GREEN; |
42 |
| - keypad.setKeyColor(PKP_KEY_7, colors1, blinks1); |
43 |
| - keypad.setKeyColor(PKP_KEY_8, colors1, blinks1); |
44 |
| - keypad.setKeyColor(PKP_KEY_9, colors1, blinks1); |
45 |
| - keypad.setKeyColor(PKP_KEY_10, colors1, blinks1); |
46 |
| - colors1[1] = PKP_COLOR_RED; |
47 |
| - colors1[2] = PKP_COLOR_GREEN; |
48 |
| - colors1[3] = PKP_COLOR_BLUE; |
49 |
| - keypad.setKeyColor(PKP_KEY_12, colors1, blinks1); |
50 |
| - keypad.setKeyColor(PKP_KEY_13, colors1, blinks1); |
51 |
| - keypad.setKeyColor(PKP_KEY_14, colors1, blinks1); |
52 |
| - keypad.setKeyColor(PKP_KEY_15, colors1, blinks1); |
53 |
| - |
54 |
| - keypad.setKeyMode(PKP_KEY_1, PkpKeypad::KEY_MODE_MOMENTARY); |
55 |
| - keypad.setKeyMode(PKP_KEY_2, PkpKeypad::KEY_MODE_MOMENTARY); |
56 |
| - keypad.setKeyMode(PKP_KEY_3, PkpKeypad::KEY_MODE_MOMENTARY); |
57 |
| - keypad.setKeyMode(PKP_KEY_4, PkpKeypad::KEY_MODE_MOMENTARY); |
58 |
| - keypad.setKeyMode(PKP_KEY_5, PkpKeypad::KEY_MODE_MOMENTARY); |
59 |
| - keypad.setKeyMode(PKP_KEY_6, PkpKeypad::KEY_MODE_MOMENTARY); |
60 |
| - keypad.setKeyMode(PKP_KEY_7, PkpKeypad::KEY_MODE_TOGGLE); |
61 |
| - keypad.setKeyMode(PKP_KEY_8, PkpKeypad::KEY_MODE_TOGGLE); |
62 |
| - keypad.setKeyMode(PKP_KEY_9, PkpKeypad::KEY_MODE_TOGGLE); |
63 |
| - keypad.setKeyMode(PKP_KEY_10, PkpKeypad::KEY_MODE_TOGGLE); |
64 |
| - keypad.setKeyMode(PKP_KEY_11, PkpKeypad::KEY_MODE_MOMENTARY); |
65 |
| - keypad.setKeyMode(PKP_KEY_12, PkpKeypad::KEY_MODE_CYCLE4); |
66 |
| - keypad.setKeyMode(PKP_KEY_13, PkpKeypad::KEY_MODE_TOGGLE); |
67 |
| - keypad.setKeyMode(PKP_KEY_14, PkpKeypad::KEY_MODE_TOGGLE); |
68 |
| - keypad.setKeyMode(PKP_KEY_15, PkpKeypad::KEY_MODE_CYCLE4); |
69 |
| - |
70 |
| - |
71 |
| - uint8_t defaultStates[PKP_MAX_KEY_AMOUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 48 | + uint8_t colors1[4] = {Pkp::KEY_COLOR_BLANK, Pkp::KEY_COLOR_GREEN, Pkp::KEY_COLOR_BLANK, Pkp::KEY_COLOR_RED}; |
| 49 | + uint8_t blinks1[4] = {Pkp::KEY_COLOR_BLANK, Pkp::KEY_COLOR_BLANK, Pkp::KEY_COLOR_GREEN, Pkp::KEY_COLOR_BLANK}; |
| 50 | + |
| 51 | + keypad.setKeyColor(Pkp::KEY_2, colors1, blinks1); |
| 52 | + keypad.setKeyColor(Pkp::KEY_3, colors1, blinks1); |
| 53 | + keypad.setKeyColor(Pkp::KEY_4, colors1, blinks1); |
| 54 | + keypad.setKeyColor(Pkp::KEY_5, colors1, blinks1); |
| 55 | + keypad.setKeyColor(Pkp::KEY_6, colors1, blinks1); |
| 56 | + colors1[1] = Pkp::KEY_COLOR_GREEN; |
| 57 | + keypad.setKeyColor(Pkp::KEY_7, colors1, blinks1); |
| 58 | + keypad.setKeyColor(Pkp::KEY_8, colors1, blinks1); |
| 59 | + keypad.setKeyColor(Pkp::KEY_9, colors1, blinks1); |
| 60 | + keypad.setKeyColor(Pkp::KEY_10, colors1, blinks1); |
| 61 | + colors1[1] = Pkp::KEY_COLOR_RED; |
| 62 | + colors1[2] = Pkp::KEY_COLOR_GREEN; |
| 63 | + colors1[3] = Pkp::KEY_COLOR_BLUE; |
| 64 | + keypad.setKeyColor(Pkp::KEY_12, colors1, blinks1); |
| 65 | + keypad.setKeyColor(Pkp::KEY_13, colors1, blinks1); |
| 66 | + keypad.setKeyColor(Pkp::KEY_14, colors1, blinks1); |
| 67 | + keypad.setKeyColor(Pkp::KEY_15, colors1, blinks1); |
| 68 | + |
| 69 | + keypad.setKeyMode(Pkp::KEY_1, Pkp::KEY_MODE_TOGGLE); |
| 70 | + keypad.setKeyMode(Pkp::KEY_2, Pkp::KEY_MODE_MOMENTARY); |
| 71 | + keypad.setKeyMode(Pkp::KEY_3, Pkp::KEY_MODE_MOMENTARY); |
| 72 | + keypad.setKeyMode(Pkp::KEY_4, Pkp::KEY_MODE_MOMENTARY); |
| 73 | + keypad.setKeyMode(Pkp::KEY_5, Pkp::KEY_MODE_MOMENTARY); |
| 74 | + keypad.setKeyMode(Pkp::KEY_6, Pkp::KEY_MODE_MOMENTARY); |
| 75 | + keypad.setKeyMode(Pkp::KEY_7, Pkp::KEY_MODE_TOGGLE); |
| 76 | + keypad.setKeyMode(Pkp::KEY_8, Pkp::KEY_MODE_TOGGLE); |
| 77 | + keypad.setKeyMode(Pkp::KEY_9, Pkp::KEY_MODE_TOGGLE); |
| 78 | + keypad.setKeyMode(Pkp::KEY_10, Pkp::KEY_MODE_TOGGLE); |
| 79 | + keypad.setKeyMode(Pkp::KEY_11, Pkp::KEY_MODE_MOMENTARY); |
| 80 | + keypad.setKeyMode(Pkp::KEY_12, Pkp::KEY_MODE_CYCLE4); |
| 81 | + keypad.setKeyMode(Pkp::KEY_13, Pkp::KEY_MODE_TOGGLE); |
| 82 | + keypad.setKeyMode(Pkp::KEY_14, Pkp::KEY_MODE_TOGGLE); |
| 83 | + keypad.setKeyMode(Pkp::KEY_15, Pkp::KEY_MODE_CYCLE4); |
| 84 | + |
| 85 | + |
| 86 | + int8_t defaultStates[PKP_MAX_KEY_AMOUNT] = {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}; |
72 | 87 | keypad.presetDefaultKeyStates(defaultStates);
|
73 |
| - |
| 88 | + keypad.applyDefaultKeyStates(); |
| 89 | + keypad.setKeyBrightness(70); |
| 90 | + keypad.setBacklight(Pkp::BACKLIGHT_BLUE, 50); |
74 | 91 | keypad.initializeEncoder(0, 16, 5);
|
75 | 92 | keypad.initializeEncoder(1, 16, 10);
|
76 | 93 |
|
77 |
| - keypad.begin(CAN_1000KBPS, MCP_8MHZ); // These are MCP settings to be passed |
| 94 | + keypad.begin(); |
78 | 95 | }
|
79 | 96 |
|
80 |
| -//---------------------------------------------------------------------------- |
81 |
| - |
82 |
| -uint8_t brightness = 0; |
83 |
| -uint32_t lastIncrement = 0; |
84 |
| -int16_t encoder = 0; |
85 |
| - |
86 | 97 | void loop() {
|
87 |
| - currentMillis = millis(); |
88 | 98 |
|
89 |
| - keypad.process(); // must have this in main loop. |
| 99 | + static uint32_t key9OnTime = 0; |
| 100 | + static bool lastKey9State = 0; |
| 101 | + static uint32_t lastIncrement = 0; |
| 102 | + uint32_t currentMillis = millis(); |
90 | 103 |
|
91 |
| - if (keypad._keyState[PKP_KEY_1] == 1) { |
| 104 | + if (keypad.getKeyState(Pkp::KEY_1) == 1) { |
92 | 105 | // do stuff
|
93 | 106 | }
|
94 | 107 |
|
95 |
| - // if key 9 is pressed, turn off after 2 seconds |
96 |
| - if (keypad._keyState[PKP_KEY_9] == 1 && lastKey9State == 0) { |
| 108 | + // if key 9 is release, blink for two seconds and turn off afterwards |
| 109 | + uint8_t key9State = keypad.getKeyState(Pkp::KEY_9); |
| 110 | + if (key9State == 0 && lastKey9State == 1) { |
97 | 111 | key9OnTime = currentMillis;
|
| 112 | + keypad.setKeyStateOverride(Pkp::KEY_9, 2); |
98 | 113 | }
|
99 |
| - lastKey9State = keypad._keyState[PKP_KEY_9]; |
100 |
| - if (lastKey9State == 1 && (currentMillis - key9OnTime) > 2000) { |
101 |
| - keypad._keyState[PKP_KEY_9] = 0; |
102 |
| - keypad.update(); |
| 114 | + if (key9OnTime + 2000 < currentMillis) { |
| 115 | + keypad.setKeyStateOverride(Pkp::KEY_9, -1); |
103 | 116 | }
|
| 117 | + lastKey9State = key9State; |
104 | 118 |
|
105 |
| - int encoderOld = encoder; |
106 |
| - bool newData = false; |
107 |
| - uint16_t leds = 0; |
108 |
| - encoder += keypad.getRelativeEncoderTicks(0); |
109 |
| - encoder = constrain(encoder, 0, 16); |
110 | 119 |
|
111 |
| - if (encoder != encoderOld) { |
112 |
| - for (int i = 0; i < encoder; i++) { |
113 |
| - leds |= 1 << i; |
| 120 | + bool newData = false; |
| 121 | + int32_t leds[2] = {-1, -1}; |
| 122 | + for (int i = 0; i < 2; i++) { |
| 123 | + int16_t encoder = keypad.getEncoderPosition(i); |
| 124 | + for (int j = 0; j < encoder; j++) { |
| 125 | + leds[i] |= 1 << j; |
114 | 126 | }
|
115 |
| - newData = 1; |
116 | 127 | }
|
| 128 | + keypad.setEncoderLeds(leds); |
| 129 | + |
| 130 | + if (keypad.getStatus() != Pkp::KPS_RX_WITHIN_LAST_SECOND) { |
| 131 | + digitalWrite(LED_BUILTIN, HIGH); |
| 132 | + delay(100); |
| 133 | + digitalWrite(LED_BUILTIN, LOW); |
| 134 | + delay(500); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +uint8_t transmittMessageCallBack(const struct can_frame& txMsg) { |
| 139 | + can.beginPacket(txMsg.can_id, txMsg.can_dlc); |
| 140 | + for (int i = 0; i < txMsg.can_dlc; i++) { |
| 141 | + can.write(txMsg.data[i]); |
| 142 | + } |
| 143 | + can.endPacket(); |
| 144 | + return 0; |
| 145 | +} |
117 | 146 |
|
118 |
| - if (newData && lastIncrement + 50 < currentMillis) { |
119 |
| - keypad.setEncoderLed(0, leds); |
120 |
| - lastIncrement = currentMillis; |
| 147 | +void onReceive(int packetSize) { |
| 148 | + struct can_frame rxMsg; |
| 149 | + rxMsg.can_id = can.packetId(); |
| 150 | + rxMsg.can_dlc = can.packetDlc(); |
| 151 | + packetSize = min(packetSize, sizeof(rxMsg.data) / sizeof(rxMsg.data[0])); |
| 152 | + for (int i = 0; i < packetSize; i++) { |
| 153 | + if (can.peek() == -1) { |
| 154 | + break; |
| 155 | + } |
| 156 | + rxMsg.data[i] = (char)can.read(); |
121 | 157 | }
|
| 158 | + keypad.process(rxMsg); |
122 | 159 | }
|
0 commit comments