Created digital IO classes
This commit is contained in:
40
firmware/src/board/din.cpp
Normal file
40
firmware/src/board/din.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/**** Includes ****/
|
||||
#include "../utils/utils.h"
|
||||
#include "mcu/mcu_hal.h"
|
||||
#include "din.h"
|
||||
|
||||
using namespace board;
|
||||
|
||||
/**** Private definitions ****/
|
||||
/**** Private constants ****/
|
||||
/**** Private variables ****/
|
||||
/**** Private function declarations ****/
|
||||
|
||||
/**** Public function definitions ****/
|
||||
board::DigitalIn::DigitalIn(uint8_t gpio_ch, uint8_t inverted, uint8_t init_value)
|
||||
{
|
||||
this->gpio_ch = gpio_ch;
|
||||
this->invert = inverted;
|
||||
|
||||
if(init_value) this->last_read = DIN_HIGH;
|
||||
else this->last_read = DIN_LOW;
|
||||
}
|
||||
|
||||
board::DigitalIn::~DigitalIn(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t board::DigitalIn::read(void)
|
||||
{
|
||||
uint8_t lvl = mcu::gpio_read(this->gpio_ch);
|
||||
|
||||
if(this->invert) lvl = util::invert(lvl);
|
||||
|
||||
if(lvl>0) this->last_read = DIN_HIGH;
|
||||
else this->last_read = DIN_LOW;
|
||||
|
||||
return this->last_read;
|
||||
}
|
||||
|
||||
/**** Private function definitions ****/
|
||||
35
firmware/src/board/din.h
Normal file
35
firmware/src/board/din.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef DIGITAL_INPUT_H_
|
||||
#define DIGITAL_INPUT_H_
|
||||
|
||||
/**** Includes ****/
|
||||
#include <stdint.h>
|
||||
|
||||
namespace board {
|
||||
|
||||
/**** Public definitions ****/
|
||||
const uint8_t DIN_LOW = 0;
|
||||
const uint8_t DIN_HIGH = 1;
|
||||
|
||||
class DigitalIn
|
||||
{
|
||||
protected:
|
||||
uint8_t gpio_ch;
|
||||
uint8_t invert;
|
||||
|
||||
public:
|
||||
DigitalIn(uint8_t gpio_ch, uint8_t inverted, uint8_t init_value);
|
||||
~DigitalIn(void);
|
||||
|
||||
uint8_t last_read;
|
||||
|
||||
uint8_t read(void);
|
||||
};
|
||||
|
||||
/**** Public function declarations ****/
|
||||
|
||||
#ifdef TESTING
|
||||
#endif
|
||||
|
||||
} //namespace
|
||||
|
||||
#endif /* DIGITAL_INPUT_H_ */
|
||||
@@ -11,52 +11,19 @@ using namespace board;
|
||||
/**** Private function declarations ****/
|
||||
|
||||
/**** Public function definitions ****/
|
||||
board::DigitalIO::DigitalIO(uint8_t gpio_ch, uint8_t init_read)
|
||||
board::DigitalIO::DigitalIO(uint8_t gpio_ch, uint8_t init_value) : DigitalIn(gpio_ch, 0, init_value), DigitalOut(gpio_ch, 0)
|
||||
{
|
||||
this->gpio_ch = gpio_ch;
|
||||
|
||||
if(init_read) this->last_read = (uint8_t)DIO_HIGH;
|
||||
else this->last_read = (uint8_t)DIO_LOW;
|
||||
|
||||
this->write(DIO_HIZ);
|
||||
return;
|
||||
}
|
||||
|
||||
board::DigitalIO::~DigitalIO(void)
|
||||
{
|
||||
this->write(DIO_HIZ);
|
||||
}
|
||||
|
||||
uint8_t board::DigitalIO::read(void)
|
||||
{
|
||||
uint8_t lvl = mcu::gpio_read(this->gpio_ch);
|
||||
if(lvl>0) this->last_read = (uint8_t)DIO_HIGH;
|
||||
else this->last_read = (uint8_t)DIO_LOW;
|
||||
|
||||
return this->last_read;
|
||||
}
|
||||
|
||||
void board::DigitalIO::write(int8_t level)
|
||||
{
|
||||
if(level > 0)
|
||||
{
|
||||
this->last_set = DIO_HIGH;
|
||||
mcu::gpio_write(this->gpio_ch, mcu::LEVEL_HIGH);
|
||||
}
|
||||
else if(level < 0)
|
||||
{
|
||||
this->last_set = DIO_HIZ;
|
||||
mcu::gpio_write(this->gpio_ch, mcu::LEVEL_HIZ);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->last_set = DIO_LOW;
|
||||
mcu::gpio_write(this->gpio_ch, mcu::LEVEL_LOW);
|
||||
}
|
||||
this->write(DOUT_HIZ);
|
||||
}
|
||||
|
||||
uint8_t board::DigitalIO::is_io_match(void)
|
||||
{
|
||||
if(this->last_set == DIO_HIZ) return 1;
|
||||
if(this->last_set == DOUT_HIZ) return 1;
|
||||
|
||||
uint8_t read_lvl = this->read();
|
||||
|
||||
@@ -64,9 +31,4 @@ uint8_t board::DigitalIO::is_io_match(void)
|
||||
else return 0;
|
||||
}
|
||||
|
||||
int8_t board::DigitalIO::get_set_level(void)
|
||||
{
|
||||
return this->last_set;
|
||||
}
|
||||
|
||||
/**** Private function definitions ****/
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#ifndef DIGITAL_OUT_H_
|
||||
#define DIGITAL_OUT_H_
|
||||
#ifndef DIGITAL_IO_H_
|
||||
#define DIGITAL_IO_H_
|
||||
|
||||
/**** Includes ****/
|
||||
#include <stdint.h>
|
||||
#include "din.h"
|
||||
#include "dout.h"
|
||||
|
||||
namespace board {
|
||||
|
||||
@@ -11,22 +13,13 @@ const int8_t DIO_LOW = 0;
|
||||
const int8_t DIO_HIGH = 1;
|
||||
const int8_t DIO_HIZ = -1;
|
||||
|
||||
class DigitalIO
|
||||
class DigitalIO : public DigitalIn, public DigitalOut
|
||||
{
|
||||
protected:
|
||||
uint8_t gpio_ch;
|
||||
int8_t last_set;
|
||||
|
||||
public:
|
||||
DigitalIO(uint8_t gpio_ch, uint8_t init_read);
|
||||
DigitalIO(uint8_t gpio_ch, uint8_t init_value);
|
||||
~DigitalIO(void);
|
||||
|
||||
uint8_t last_read;
|
||||
|
||||
uint8_t read(void);
|
||||
void write(int8_t level);
|
||||
uint8_t is_io_match(void);
|
||||
int8_t get_set_level(void);
|
||||
};
|
||||
|
||||
/**** Public function declarations ****/
|
||||
@@ -36,4 +29,4 @@ class DigitalIO
|
||||
|
||||
} //namespace
|
||||
|
||||
#endif /* DIGITAL_OUT_H_ */
|
||||
#endif /* DIGITAL_IO_H_ */
|
||||
52
firmware/src/board/dout.cpp
Normal file
52
firmware/src/board/dout.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
/**** Includes ****/
|
||||
#include "../utils/utils.h"
|
||||
#include "mcu/mcu_hal.h"
|
||||
#include "dout.h"
|
||||
|
||||
using namespace board;
|
||||
|
||||
/**** Private definitions ****/
|
||||
/**** Private constants ****/
|
||||
/**** Private variables ****/
|
||||
/**** Private function declarations ****/
|
||||
|
||||
/**** Public function definitions ****/
|
||||
board::DigitalOut::DigitalOut(uint8_t gpio_ch, uint8_t inverted)
|
||||
{
|
||||
this->gpio_ch = gpio_ch;
|
||||
this->invert = inverted;
|
||||
this->write(DOUT_HIZ);
|
||||
}
|
||||
|
||||
board::DigitalOut::~DigitalOut(void)
|
||||
{
|
||||
this->write(DOUT_HIZ);
|
||||
}
|
||||
|
||||
void board::DigitalOut::write(int8_t level)
|
||||
{
|
||||
if(level > 0)
|
||||
{
|
||||
this->last_set = DOUT_HIGH;
|
||||
if(this->invert) mcu::gpio_write(this->gpio_ch, mcu::LEVEL_LOW);
|
||||
else mcu::gpio_write(this->gpio_ch, mcu::LEVEL_HIGH);
|
||||
}
|
||||
else if(level == 0)
|
||||
{
|
||||
this->last_set = DOUT_LOW;
|
||||
if(this->invert) mcu::gpio_write(this->gpio_ch, mcu::LEVEL_HIGH);
|
||||
else mcu::gpio_write(this->gpio_ch, mcu::LEVEL_LOW);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->last_set = DOUT_HIZ;
|
||||
mcu::gpio_write(this->gpio_ch, mcu::LEVEL_HIZ);
|
||||
}
|
||||
}
|
||||
|
||||
int8_t board::DigitalOut::get_set_level(void)
|
||||
{
|
||||
return this->last_set;
|
||||
}
|
||||
|
||||
/**** Private function definitions ****/
|
||||
36
firmware/src/board/dout.h
Normal file
36
firmware/src/board/dout.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef DIGITAL_OUTPUT_H_
|
||||
#define DIGITAL_OUTPUT_H_
|
||||
|
||||
/**** Includes ****/
|
||||
#include <stdint.h>
|
||||
|
||||
namespace board {
|
||||
|
||||
/**** Public definitions ****/
|
||||
const int8_t DOUT_LOW = 0;
|
||||
const int8_t DOUT_HIGH = 1;
|
||||
const int8_t DOUT_HIZ = -1;
|
||||
|
||||
class DigitalOut
|
||||
{
|
||||
protected:
|
||||
uint8_t gpio_ch;
|
||||
uint8_t invert;
|
||||
int8_t last_set;
|
||||
|
||||
public:
|
||||
DigitalOut(uint8_t gpio_ch, uint8_t inverted);
|
||||
~DigitalOut(void);
|
||||
|
||||
void write(int8_t level);
|
||||
int8_t get_set_level(void);
|
||||
};
|
||||
|
||||
/**** Public function declarations ****/
|
||||
|
||||
#ifdef TESTING
|
||||
#endif
|
||||
|
||||
} //namespace
|
||||
|
||||
#endif /* DIGITAL_OUTPUT_H_ */
|
||||
Reference in New Issue
Block a user