ClearCore Library
DigitalIn.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Teknic, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
29 #ifndef __DIGITALIN_H__
30 #define __DIGITALIN_H__
31 
32 #include <stdint.h>
33 #include "Connector.h"
34 #include "InputManager.h"
35 #include "PeripheralRoute.h"
36 #include "ShiftRegister.h"
37 
38 namespace ClearCore {
39 
43 typedef void (*voidFuncPtr)(void);
44 
70 class DigitalIn : public Connector {
71  friend class SysManager;
72  friend class TestIO;
73 
74 public:
75 #ifndef HIDE_FROM_DOXYGEN
76 
82  DigitalIn() {};
83 #endif
84 
91  typedef enum {
96  } FilterUnits;
97 
122  void FilterLength(uint16_t length,
123  FilterUnits units = FILTER_UNIT_SAMPLES) {
124  // 1 ms = 1000 us = 5 * (200 us) = 5 sample times
125  uint16_t samples = (units == FILTER_UNIT_MS) ? 5 * length : length;
126  m_filterLength = samples;
127  m_filterTicksLeft = samples;
128  }
129 
146  uint16_t FilterLength() {
147  return m_filterLength;
148  }
149 
161  virtual ConnectorModes Mode() override {
162  return Connector::Mode();
163  }
164 
172  virtual bool Mode(ConnectorModes newMode) override {
173  if (newMode == ConnectorModes::INPUT_DIGITAL) {
174  m_mode = newMode;
175  return true;
176  }
177  else {
178  return false;
179  }
180  }
181 
195  }
196 
208  bool IsWritable() override {
209  return false;
210  }
211 
227  int16_t State() override;
228 
229 #ifndef HIDE_FROM_DOXYGEN
230 
236  int16_t StateRT();
237 
249  bool State(int16_t newState) override {
250  (void)newState;
251  return false;
252  }
253 #endif
254 
266  bool InputRisen();
267 
279  bool InputFallen();
280 
281 #ifndef HIDE_FROM_DOXYGEN
282 
288  bool IsInHwFault() override {
289  return false;
290  }
291 
298  int8_t ExternalInterrupt() override {
299  return m_extInt;
300  }
301 #endif
302 
339  bool InterruptHandlerSet(voidFuncPtr callback = nullptr,
341  InputManager::InterruptTrigger::RISING,
342  bool enable = true);
343 
359  void InterruptEnable(bool enable);
360 
361 protected:
362  // LED associated with input
363  ShiftRegister::Masks m_ledMask;
364 
365  // Register that contains the digital input
366  uint32_t m_inputPort;
367  uint32_t m_inputDataBit;
368  uint32_t m_inputDataMask;
369 
370  // External interrupts
371  uint8_t m_extInt; // External interrupt line index
372  bool m_interruptAvail; // An external interrupt is available on this input
373 
374  uint32_t *m_changeRegPtr;
375  uint32_t *m_inRegPtr;
376  uint32_t *m_inputRegRTPtr;
377 
378  // Boolean state holders
379  bool m_stateFiltered;
380 
381  // Stability filter
382  uint16_t m_filterLength;
383  // Set to filter length on input state change
384  uint16_t m_filterTicksLeft;
385 
389  DigitalIn(enum ShiftRegister::Masks ledMask,
390  const PeripheralRoute *inputInfo);
391 
402  void Refresh() override;
403 
407  void Initialize(ClearCorePins clearCorePin) override;
408 
412  void UpdateFilterState();
413 
414 }; // DigitalIn
415 
416 } // ClearCore namespace
417 
418 #endif // __DIGITALIN_H__
int16_t State() override
Get the connector&#39;s last majority-filtered sampled value.
ClearCore input state access.
virtual ConnectorModes Mode() override
Get the connector&#39;s operational mode.
Definition: DigitalIn.h:161
FilterUnits
Units for the digital filter length.
Definition: DigitalIn.h:91
Defines the Peripheral Route structure, used in HardwareMapping.
void InterruptEnable(bool enable)
Enable or disable the interrupt on this connector.
virtual bool IsInHwFault()=0
Get whether the connector is in a hardware fault state.
ClearCore digital input connector class.
Definition: DigitalIn.h:70
Definition: Connector.h:185
Connector::ConnectorTypes Type() override
Get connector type.
Definition: DigitalIn.h:193
virtual bool Mode(ConnectorModes newMode) override
Set the connector&#39;s operational mode.
Definition: DigitalIn.h:172
uint16_t FilterLength()
Get the connector&#39;s digital filter length in samples. The default is 3 samples.
Definition: DigitalIn.h:146
Base class for interacting with all ClearCore connector objects.
Definition: Connector.h:62
Definition: DigitalIn.h:93
Namespace to encompass the ClearCore board API.
Definition: AdcManager.h:36
bool IsWritable() override
Get R/W status of the connector.
Definition: DigitalIn.h:208
LED shift register access class.
Base class for all connector classes.
ConnectorModes
All possible operational modes for a connector.
Definition: Connector.h:74
bool InputFallen()
Clear on read accessor for this connector&#39;s falling input state.
InterruptTrigger
The possible input state conditions to trigger an interrupt on.
Definition: InputManager.h:58
void(* voidFuncPtr)(void)
Definition: DigitalIn.h:43
void FilterLength(uint16_t length, FilterUnits units=FILTER_UNIT_SAMPLES)
Set the connector&#39;s digital transition filter length. The default digital filter length for digital i...
Definition: DigitalIn.h:122
bool InterruptHandlerSet(voidFuncPtr callback=nullptr, InputManager::InterruptTrigger trigger=InputManager::InterruptTrigger::RISING, bool enable=true)
Register the interrupt service routine to be triggered when the given input state condition is met on...
ClearCore Board Supervisory System Manager.
Definition: SysManager.h:58
ClearCorePins
ClearCore PIN definitions.
Definition: SysConnectors.h:54
virtual ConnectorModes Mode()
Get the connector&#39;s operational mode.
Definition: Connector.h:309
ConnectorTypes
The different types of ClearCore connectors.
Definition: Connector.h:172
bool InputRisen()
Clear on read accessor for this connector&#39;s rising input state.