work on = problem

master
polymurph 4 years ago
parent 120de39832
commit 2bd36a17c2

@ -1,8 +1,11 @@
#include "device.hpp" #include "device.hpp"
#include <iostream> #include <iostream>
Device::Device() Device::Device() :
reg_control(0xFF)
{ {
std::cout << +reg_control.raw << std::endl;
reg_control.bits.POWER_DEV = Reg_Control::POWER_DEV::TURN_OFF; reg_control.bits.POWER_DEV = Reg_Control::POWER_DEV::TURN_OFF;
reg_control.bits.SPEED = Reg_Control::SPEED::STAND_STILL; reg_control.bits.SPEED = Reg_Control::SPEED::STAND_STILL;
@ -18,6 +21,9 @@ void Device::doSomething()
reg_control.bits.POWER_DEV = Reg_Control::POWER_DEV::TURN_ON; reg_control.bits.POWER_DEV = Reg_Control::POWER_DEV::TURN_ON;
reg_control.bits.SPEED = Reg_Control::SPEED::FAST; reg_control.bits.SPEED = Reg_Control::SPEED::FAST;
if(reg_control.bits.POWER_DEV == Reg_Control::POWER_DEV::TURN_ON){
std::cout << "device turned on!" << std::endl;
}
std::cout << +reg_control.raw << std::endl; std::cout << +reg_control.raw << std::endl;
} }

@ -44,26 +44,27 @@ class Device
#endif #endif
union Reg_Control union Reg_Control
{ {
// bit 7 // bit 4
struct POWER_DEV{ struct POWER_DEV{
typedef BitField<uint8_t, 7, 1> bits; typedef BitField<uint8_t, 4, 1> Bits;
enum{TURN_OFF = 0, TURN_ON = 1}; enum{TURN_OFF = 0, TURN_ON = 1};
}; };
// bits 2-3 // bits 2-3
struct SPEED{ struct SPEED{
typedef BitField<uint8_t, 2, 2> bits; typedef BitField<uint8_t, 2, 2> Bits;
enum{STAND_STILL = 0, enum{STAND_STILL = 0,
SLOW = 1, SLOW = 1,
NORMAL = 2, NORMAL = 2,
FAST = 3}; FAST = 3};
}; };
union Bits{ struct Bits{
Reg_Control::POWER_DEV::bits POWER_DEV; Reg_Control::POWER_DEV::Bits POWER_DEV;
Reg_Control::SPEED::bits SPEED; Reg_Control::SPEED::Bits SPEED;
} bits; } bits;
// raw value. all bitfields will be "unified" here ;)
uint8_t raw; uint8_t raw;
// union Ctor // union Ctor with default value set to 0x00
Reg_Control(uint8_t v = 0x00) : raw(0x00) {} Reg_Control(uint8_t v = 0x00) : raw(v) {}
}; };
Reg_Control reg_control; Reg_Control reg_control;

Loading…
Cancel
Save