working example of register modeled with CPP for easy readability andfailproof

master
polymurph 4 years ago
parent 12ab5b6cbe
commit 120de39832

@ -0,0 +1,47 @@
{
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}

@ -0,0 +1,24 @@
cpp_src = $(wildcard *.cpp)
cpp_src += $(wildcard ./utils/*.cpp)
cpp_src += $(wildcard ./driver/*.cpp)
cpp_obj = $(cpp_src:.cpp=.o)
c_obj = $(c_src:.c=.o)
CC = g++
CFLAGS = -Wall -pedantic -li2c
LDFLAGS =
EXEC = runtest
all : $(EXEC)
$(EXEC): $(cpp_obj) $(c_obj)
$(CC) -o $@ $^ $(LDFLAGS)
clean:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
clear
cleanall:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
clear

@ -1,14 +1,23 @@
#include "device.hpp"
#include <iostream>
Device::Device()
{
Reg_Control.POWER_DEV = Reg_Control::POWER_DEV::TURN_OFF;
Reg_Control.SPEED = Reg_Control::SPEED::STAND_STILL;
reg_control.bits.POWER_DEV = Reg_Control::POWER_DEV::TURN_OFF;
reg_control.bits.SPEED = Reg_Control::SPEED::STAND_STILL;
std::cout << Reg_Control.raw;
std::cout << "Device::Device()" << std::endl;
std::cout << +reg_control.raw << std::endl;
}
void Device::doSomething()
{
std::cout << "Device::doSomething()" << std::endl;
reg_control.bits.POWER_DEV = Reg_Control::POWER_DEV::TURN_ON;
reg_control.bits.SPEED = Reg_Control::SPEED::FAST;
std::cout << +reg_control.raw << std::endl;
}

@ -1,7 +1,7 @@
#ifndef _DEVICE_HPP_
#define _DEVICE_HPP_
#include"../BitField.hpp"
#include "../utils/BitField.hpp"
#include <iostream>
#include <stdint.h>
@ -46,12 +46,12 @@ class Device
{
// bit 7
struct POWER_DEV{
typedef Bits<uint8_t, 7, 1> bits;
typedef BitField<uint8_t, 7, 1> bits;
enum{TURN_OFF = 0, TURN_ON = 1};
};
// bits 2-3
struct SPEED{
typedef Bits<uint8_t, 2, 2> bits;
typedef BitField<uint8_t, 2, 2> bits;
enum{STAND_STILL = 0,
SLOW = 1,
NORMAL = 2,
@ -62,8 +62,14 @@ class Device
Reg_Control::SPEED::bits SPEED;
} bits;
uint8_t raw;
// union Ctor
Reg_Control(uint8_t v = 0x00) : raw(0x00) {}
};
Reg_Control reg_control;
};
#endif // _DEVICE_HPP_

@ -1,9 +1,67 @@
#include <iostream>
#include <stdint.h>
#include "mcp4725/mcp4725.hpp"
#include "./driver/device.hpp"
#include "./utils/BitField.hpp"
#ifdef SIMPLE_TEST
int main(void)
{
std::cout << "test" << std::endl;
BitField<uint8_t, 1, 2> bitfield_0;
BitField<uint8_t, 5, 2> bitfield_1;
std::cout << "testing bitfield 0" << std::endl;
for(uint8_t i = 0; i < 10; i++) {
bitfield_0 = i;
std::cout << static_cast<uint32_t>(bitfield_0()) << std::endl;
}
std::cout << "testing bitfield 1" << std::endl;
for(uint8_t i = 0; i < 10; i++) {
bitfield_1 = i;
std::cout << static_cast<uint32_t>(bitfield_1()) << std::endl;
}
Device dev();
return 0;
}
#endif
class da
{
public:
da();
private:
};
da::da()
{
std::cout << "da::da()" << std::endl;
}
int main(void)
{
std::cout << "test" << std::endl;
da dada;
Device dev;
dev.doSomething();
//dev.doSomething();
return 0;
}
}

@ -1,124 +0,0 @@
#include "mcp4725.hpp"
#if 0
MCP4725::MCP4725(pwrd_md power_down_mode,
i2c_addr address,
I2C* i2c) :
power_down_mode(power_down_mode),
address(address),
i2c(i2c),
dac_value(0),
eeprom_value(0)
{
uint8_t temp[6];
temp[0] = cmd_write_dac_and_eeprom | (power_down_mode << 1);
temp[1] = static_cast<uint8_t>(dac_value >> 4);
temp[2] = static_cast<uint8_t>(dac_value << 4);
temp[3] = temp[0];
temp[4] = temp[1];
temp[5] = temp[2];
//i2c_write(address, temp,6);
}
MCP4725::MCP4725(i2c_addr address,
I2C* i2c) :
power_down_mode(pwrd_md::normal),
address(address),
i2c(i2c),
dac_value(0),
eeprom_value(0)
{
uint8_t temp[6];
temp[0] = cmd_write_dac_and_eeprom | (power_down_mode << 1);
temp[1] = static_cast<uint8_t>(dac_value >> 4);
temp[2] = static_cast<uint8_t>(dac_value << 4);
temp[3] = temp[0];
temp[4] = temp[1];
temp[5] = temp[2];
//i2c_write(address, temp, 6);
}
#endif
MCP4725::MCP4725()
{
}
MCP4725::MCP4725(I2C* i2c) :
power_down_mode(pwrd_md::normal),
address(addr_0x60),
i2c(i2c),
dac_value(0),
eeprom_value(0)
{
uint8_t temp[6];
temp[0] = cmd_write_dac_and_eeprom | (power_down_mode << 1);
temp[1] = static_cast<uint8_t>(dac_value >> 4);
temp[2] = static_cast<uint8_t>(dac_value << 4);
temp[3] = temp[0];
temp[4] = temp[1];
temp[5] = temp[2];
i2c->writeBuffer(address, temp, 6);
}
//MCP4725::~MCP4725()
//{
//}
void MCP4725::operator=(uint16_t dac_value)
{
uint8_t temp[3];
this->dac_value = dac_value;
temp[0] = cmd_write_dac | (power_down_mode << 1);
temp[1] = static_cast<uint8_t>(dac_value >> 4);
temp[2] = static_cast<uint8_t>(dac_value << 4);
i2c->writeBuffer(address, temp, 3);
//i2c_write(address, temp, 3);
}
void MCP4725::operator==(uint16_t dac_and_eeprom_value)
{
uint8_t temp[6];
dac_value = dac_and_eeprom_value;
eeprom_value = dac_value;
temp[0] = cmd_write_dac_and_eeprom | (power_down_mode << 1);
temp[1] = static_cast<uint8_t>(dac_and_eeprom_value >> 4);
temp[2] = static_cast<uint8_t>(dac_and_eeprom_value << 4);
temp[3] = temp[0];
temp[4] = temp[1];
temp[5] = temp[2];
i2c->writeBuffer(address, temp, 6);
}
#if 0
void MCP4725::write_dac(uint16_t dac_value)
{
&this = dac_value;
}
void MCP4725::write_dac_and_eeprom(uint16_t dac_and_eeprom_value)
{
&this == dac_and_eeprom_value;
}
#endif

@ -1,100 +0,0 @@
#ifndef _MCP4725_HPP_
#define _MCP4725_HPP_
#include <stdint.h>
#include <unistd.h>
#include <functional>
#include "../../communication/i2c/i2c.hpp"
class MCP4725
{
public:
//typedef std::function<void(uint8_t,uint8_t*,uint8_t)> i2c_write_n_t;
//typedef std::function<void(const uint8_t&,const uint8_t*,const uint8_t&)> i2c_write_n_t;
//typedef void(*i2c_write_n_t)(const uint8_t&, const uint8_t*, const uint8_t&);
//typedef std::function<void(int)>i2c_write_n_t;
// address list
enum i2c_addr
{
addr_0x60 = 0b01100000,
addr_0x61 = addr_0x60 + 1,
addr_0x62 = addr_0x60 + 2,
addr_0x63 = addr_0x60 + 3,
addr_0x64 = addr_0x60 + 4,
addr_0x65 = addr_0x60 + 5,
addr_0x66 = addr_0x60 + 6,
addr_0x67 = addr_0x60 + 7
};
// power down impedance modes
enum pwrd_md
{
normal = 0x04,
ohm_1k = 0x01,
ohm_100k = 0x02,
ohm_500k = 0x03
};
//using std::functional<uint8_t(uint8_t, uint8_t)> i2c_read_n_t;
//using i2c_write_n_t = std::functional<void(uint8_t, uint8_t*,uint8_t)>;
/**
* @brief Constructor of mcp4725 dac
*
* @param power_down_mode pwrd_md power down mode
* @param address i2c_addr i2c address of the dac
* @param i2c_write i2c_write_n_t callback for i2c writ
*/
#if 0
MCP4725(pwrd_md power_down_mode,
i2c_addr address,
I2C* i2c);
MCP4725(i2c_addr address,
I2C* i2c);
#endif
MCP4725();
MCP4725(I2C* i2c);
// ~MCP4725();
void operator=(uint16_t dac_value);
void operator==(uint16_t dac_and_eeprom_value);
#if 0
void write_dac_and_eeprom(uint16_t value);
void set_powerdown_impedance(pwrd_md mode);
i2c_addr get_i2c_addr();
#endif
//void set_power
private:
enum commands_t
{
cmd_fast_mode = 0x00,
cmd_write_dac = 0x40,
cmd_write_dac_and_eeprom = 0x60
};
I2C* i2c;
i2c_addr address;
pwrd_md power_down_mode;
//i2c_write_n_t i2c_write;
uint16_t dac_value;
uint16_t eeprom_value;
};
#endif // _MCP4725_HPP_

@ -14,6 +14,9 @@
// https://stackoverflow.com/questions/31726191/is-there-a-portable-alternative-to-c-bitfields
// https://stackoverflow.com/questions/1392059/algorithm-to-generate-bit-mask
#ifndef _BITFIELDS_HPP_
#define _BITFIELDS_HPP_
#include <stdint.h>
/**
@ -72,3 +75,5 @@ struct BitField
private:
T _raw;
};
#endif // _BITFIELDS_HPP_
Loading…
Cancel
Save