API Documentation v0.0.1
Loading...
Searching...
No Matches
2A3A1-AMPCD.ino
Go to the documentation of this file.
1/**************************************************************************************
2 * ____ _ _ _
3 * / __ \ | | | | | |
4 * | | | |_ __ ___ _ __ | |__| | ___ _ __ _ __ ___| |_
5 * | | | | '_ \ / _ \ '_ \| __ |/ _ \| '__| '_ \ / _ \ __|
6 * | |__| | |_) | __/ | | | | | | (_) | | | | | | __/ |_
7 * \____/| .__/ \___|_| |_|_| |_|\___/|_| |_| |_|\___|\__|
8 * | |
9 * |_|
10 * ----------------------------------------------------------------------------------
11 * Copyright 2016-2024 OpenHornet
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 * ----------------------------------------------------------------------------------
25 * Note: All other portions of OpenHornet not within the 'OpenHornet-Software'
26 * GitHub repository is released under the Creative Commons Attribution -
27 * Non-Commercial - Share Alike License. (CC BY-NC-SA 4.0)
28 * ----------------------------------------------------------------------------------
29 * This Project uses Doxygen as a documentation generator.
30 * Please use Doxygen capable comments.
31 **************************************************************************************/
32
76#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega2560__)
77#define DCSBIOS_IRQ_SERIAL
78#else
79#define DCSBIOS_DEFAULT_SERIAL
80#endif
81
82#ifdef __AVR__
83#include <avr/power.h>
84#endif
85
86
91#define TXENABLE_PIN 5
92#define UART1_SELECT
93
94#include "DcsBios.h"
95#include "Wire.h"
96#include "TCA9534.h"
97
98// Define pins per the OH Interconnect.
99#define AMPCD_ROT_A A0
100#define AMPCD_ROT_OFF A1
101#define HDG_P 8
102#define HDG_M 10
103#define CRS_P 4
104#define CRS_M 7
105#define AMPCD_BACK_LIGHT 9
106#define AMPCD_IRQ 6
107
113TCA9534 ampcdButtons[4] = {
114 TCA9534(0x23), //Left Row
115 TCA9534(0x20), //Top Row
116 TCA9534(0x22), // Right Row
117 TCA9534(0x21)
118}; // Bottom Row
119
120// Setup global variables for reading DDI button presses.
121bool lastBtnState[28];
122bool buttonState[28];
123uint8_t inputRegister[4];
124unsigned long lastDebounceTime[28];
125unsigned long debounceDelay = 10;
126
127int index;
128
130char *AMPCD_Btns[] = { "AMPCD_GAIN_SW",
131 "AMPCD_NIGHT_DAY",
132 "AMPCD_SYM_SW",
133 "AMPCD_CONT_SW" };
134char btnName[20];
135
136//Connect switches to DCS-BIOS
137DcsBios::Potentiometer ampcdBrtCtl("AMPCD_BRT_CTL", AMPCD_ROT_A);
138DcsBios::Switch3Pos leftDdiCrsSw("LEFT_DDI_CRS_SW", CRS_P, CRS_M);
139DcsBios::Switch3Pos leftDdiHdgSw("LEFT_DDI_HDG_SW", HDG_P, HDG_M);
140
145void onInstrIntLtChange(unsigned int newValue) {
146 analogWrite(AMPCD_BACK_LIGHT, map(newValue, 0, 65535, 0, 255));
147}
148DcsBios::IntegerBuffer instrIntLtBuffer(FA_18C_hornet_INSTR_INT_LT, onInstrIntLtChange);
149
156void setup() {
157
158 // Run DCS Bios setup function
159 DcsBios::setup();
160
161 pinMode(AMPCD_IRQ, OUTPUT);
162
163 pinMode(AMPCD_BACK_LIGHT, OUTPUT); // set backlight pinmode to output
164 analogWrite(AMPCD_BACK_LIGHT, 0); // turn-off backlighting
169 for (int i = 0; i < sizeof(lastBtnState) / sizeof(lastBtnState[0]); i++) {
170 lastBtnState[i] = 0;
171 }
172
177 for (int i = 0; i < sizeof(ampcdButtons) / sizeof(ampcdButtons[0]); i++) {
178 ampcdButtons[i].Begin();
179 for (int j = 0; j < 7; j++) {
180 ampcdButtons[i].PinMode(j, INPUT);
181 }
182 }
183}
184
193void loop() {
194
195 //Run DCS Bios loop function
196 DcsBios::loop();
197
203 for (int i = 0; i < sizeof(ampcdButtons) / sizeof(ampcdButtons[0]); i++) {
204 inputRegister[i] = ampcdButtons[i].ReadAll();
205
210 for (int j = 0; j < 7; j++) {
211
212 index = j + (7 * i);
213
214 bool btnState = (inputRegister[i] >> (6 - j)) & 1;
215
216 if (btnState != lastBtnState[index]) {
217 lastDebounceTime[index] = millis();
218 }
219
220 if ((millis() - lastDebounceTime[index]) > debounceDelay) {
221 if (btnState != buttonState[index]) {
222 buttonState[index] = btnState;
223 if (j > 4) { // one of the AMPCD NIGHT, GAIN, SYMBOLOGY, CONTRAST rockers.
224 if (btnState != 1) { // If one of the AMPCD rocker buttons determine if value to send to DCS 0 or 2.
225 if (i == 0 or i == 1) {
226 if (j == 5) { DcsBios::sendDcsBiosMessage(AMPCD_Btns[i], "2"); }
227 if (j == 6) { DcsBios::sendDcsBiosMessage(AMPCD_Btns[i], "0"); }
228 } else {
229 if (j == 5) { DcsBios::sendDcsBiosMessage(AMPCD_Btns[i], "0"); }
230 if (j == 6) { DcsBios::sendDcsBiosMessage(AMPCD_Btns[i], "2"); }
231 }
232 } else { // every AMPCD button has a "1" state.
233 DcsBios::sendDcsBiosMessage(AMPCD_Btns[i], "1");
234 }
235 } else { // One of the 20 push buttons.
236 int btnNum = 4 - j + 5 * i;
237 sprintf(btnName, "AMPCD_PB_%02d", btnNum + 1);
238 DcsBios::sendDcsBiosMessage(btnName, btnState == 1 ? "0" : "1");
239 }
240 }
241 }
242 lastBtnState[index] = btnState;
243 }
244 }
245}
int index
Index to track the button press states.
#define AMPCD_IRQ
AMPCD IRQ Pin.
#define CRS_P
CRS +.
bool lastBtnState[28]
Array to hold the last state of the 20 AMPCD buttons, and 4 rocker switches.
void setup()
unsigned long debounceDelay
The debounce delay duration in ms, increase if the output flickers.
#define HDG_M
HDG -.
bool buttonState[28]
Array to hold the current state of the 20 AMPCD buttons, and 4 rocker switches.
#define CRS_M
CRS -.
#define AMPCD_BACK_LIGHT
DDI Backlighting PWM, must be defined as digital pin #.
void onInstrIntLtChange(unsigned int newValue)
Setup DCS-BIOS control for DDI backlighting.
TCA9534 ampcdButtons[4]
uint8_t inputRegister[4]
Input register for button read logic.
unsigned long lastDebounceTime[28]
Array to hold last time of AMPCD button update for debounce.
#define HDG_P
HDG +.
#define AMPCD_ROT_A
AMPCD Rotary Brightness.
char * AMPCD_Btns[]
DCS Bios Messages for the 4 AMPCD rocker switches.
void loop()