Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] New read/write functions for 16 and 32 bit registers #3

Merged
merged 1 commit into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@ bool Battery::isCharging()

float Battery::powerLevel()
{
unsigned char high_byte, low_byte;

// get current voltage reading
// TODO: read both values at the same time, once we support that
Private::Wallaby::instance()->readRegister(REG_RW_BATT_H, high_byte);
Private::Wallaby::instance()->readRegister(REG_RW_BATT_L, low_byte);

// piece the 12-bit ADC result back together
unsigned short raw_batt_adc = (static_cast<unsigned short>(high_byte) << 8) | static_cast<unsigned short>(low_byte);
unsigned short raw_batt_adc = Private::Wallaby::instance()->readRegister16b(REG_RW_BATT_H);

// calculate voltage based on linear curve-fitting
float batt_voltage = -0.02070635f + 0.009071161f * static_cast<float>(raw_batt_adc);

// FIXME ADC->capacity or voltage->capacity

return batt_voltage;
}
75 changes: 12 additions & 63 deletions src/digital_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ bool Digital::value(unsigned char port) const
// TODO which port is 0?
// TODO: boundary checking

unsigned char high_byte, low_byte;

// TODO: read both values at the same time, once we support that
// TODO: or just the byte we need
Private::Wallaby::instance()->readRegister(REG_RW_DIG_IN_H, high_byte);
Private::Wallaby::instance()->readRegister(REG_RW_DIG_IN_L, low_byte);

unsigned short dig_ins_val = (static_cast<unsigned short>(high_byte) << 8) | static_cast<unsigned short>(low_byte);
unsigned short dig_ins_val = Private::Wallaby::instance()->readRegister16b(REG_RW_DIG_IN_H);

// TODO: what if this isn't a digital in
bool ret = dig_ins_val & (1 << port);
Expand All @@ -39,14 +32,8 @@ bool Digital::value(unsigned char port) const
bool Digital::setValue(unsigned char port, bool value)
{
// TODO boundary checks
unsigned char high_byte, low_byte;

// TODO: read both values at the same time, once we support that
// TODO: or just the byte we need
Private::Wallaby::instance()->readRegister(REG_RW_DIG_OUT_H, high_byte);
Private::Wallaby::instance()->readRegister(REG_RW_DIG_OUT_L, low_byte);

unsigned short out = (static_cast<unsigned short>(high_byte) << 8) | static_cast<unsigned short>(low_byte);
unsigned short out = Private::Wallaby::instance()->readRegister16b(REG_RW_DIG_OUT_H);

if (value)
{
Expand All @@ -57,13 +44,9 @@ bool Digital::setValue(unsigned char port, bool value)
out &= ~(1 << port);
}

high_byte = static_cast<unsigned char>((out & 0xFF00) >> 8);
low_byte = static_cast<unsigned char>(out & 0x00FF);

bool success = Private::Wallaby::instance()->writeRegister(REG_RW_DIG_OUT_H, high_byte)
& Private::Wallaby::instance()->writeRegister(REG_RW_DIG_OUT_L, low_byte);
Private::Wallaby::instance()->writeRegister16b(REG_RW_DIG_OUT_H, out);

return success;
return true; //TODO: return based on success
}


Expand All @@ -73,14 +56,7 @@ const Digital::Direction Digital::direction(unsigned char port) const
{
// TODO: bounds check, if not a good port return Digital::Unknown

unsigned char high_byte, low_byte;

// TODO: read both values at the same time, once we support that
// TODO: or just the byte we need
Private::Wallaby::instance()->readRegister(REG_RW_DIG_OE_H, high_byte);
Private::Wallaby::instance()->readRegister(REG_RW_DIG_OE_L, low_byte);

unsigned short outputs = (static_cast<unsigned short>(high_byte) << 8) | static_cast<unsigned short>(low_byte);
unsigned short outputs = Private::Wallaby::instance()->readRegister16b(REG_RW_DIG_OE_H);
Digital::Direction ret = outputs & (1 << port) ? Digital::Out : Digital::In;
return ret;
}
Expand All @@ -92,13 +68,7 @@ bool Digital::setDirection(unsigned char port, const Digital::Direction & direct

if (direction == Digital::Unknown) return false;

unsigned char high_byte, low_byte;

// TODO: read both values at the same time, once we support that, or just the byte we need
Private::Wallaby::instance()->readRegister(REG_RW_DIG_OE_H, high_byte);
Private::Wallaby::instance()->readRegister(REG_RW_DIG_OE_L, low_byte);

unsigned short outputs = (static_cast<unsigned short>(high_byte) << 8) | static_cast<unsigned short>(low_byte);
unsigned short outputs = Private::Wallaby::instance()->readRegister16b(REG_RW_DIG_OE_H);

if (direction == Digital::Out)
{
Expand All @@ -109,27 +79,17 @@ bool Digital::setDirection(unsigned char port, const Digital::Direction & direct
outputs &= ~(1 << port);
}

high_byte = static_cast<unsigned char>((outputs & 0xFF00) >> 8);
low_byte = static_cast<unsigned char>(outputs & 0x00FF);
Private::Wallaby::instance()->writeRegister16b(REG_RW_DIG_OE_H, outputs);


bool success = Private::Wallaby::instance()->writeRegister(REG_RW_DIG_OE_H, high_byte)
& Private::Wallaby::instance()->writeRegister(REG_RW_DIG_OE_L, low_byte);

return success;
return true; // TODO: based on success;
}


bool Digital::pullup(unsigned char port) const
{
// TODO: bounds checking (return false if out of range)
unsigned char high_byte, low_byte;

// TODO: read both values at the same time, once we support that, or just the byte we need
Private::Wallaby::instance()->readRegister(REG_RW_DIG_PE_H, high_byte);
Private::Wallaby::instance()->readRegister(REG_RW_DIG_PE_L, low_byte);

unsigned short pullups = (static_cast<unsigned short>(high_byte) << 8) | static_cast<unsigned short>(low_byte);
unsigned short pullups = Private::Wallaby::instance()->readRegister16b(REG_RW_DIG_PE_H);

bool ret = pullups & (1 << port);
return ret;
Expand All @@ -138,13 +98,7 @@ bool Digital::pullup(unsigned char port) const
bool Digital::setPullup(unsigned char port, bool pullup)
{
// TODO: bounds checking (return false if out of range)
unsigned char high_byte, low_byte;

// TODO: read both values at the same time, once we support that, or just the byte we need
Private::Wallaby::instance()->readRegister(REG_RW_DIG_PE_H, high_byte);
Private::Wallaby::instance()->readRegister(REG_RW_DIG_PE_L, low_byte);

unsigned short pullups = (static_cast<unsigned short>(high_byte) << 8) | static_cast<unsigned short>(low_byte);
unsigned short pullups = Private::Wallaby::instance()->readRegister16b(REG_RW_DIG_PE_H);

if (pullup)
{
Expand All @@ -155,14 +109,9 @@ bool Digital::setPullup(unsigned char port, bool pullup)
pullups &= ~(1 << port);
}

high_byte = static_cast<unsigned char>((pullups & 0xFF00) >> 8);
low_byte = static_cast<unsigned char>(pullups & 0x00FF);


bool success = Private::Wallaby::instance()->writeRegister(REG_RW_DIG_PE_H, high_byte)
& Private::Wallaby::instance()->writeRegister(REG_RW_DIG_PE_L, low_byte);
Private::Wallaby::instance()->writeRegister16b(REG_RW_DIG_PE_H, pullups);

return success;
return true; // TODO: based on success;
}

Digital * Digital::instance()
Expand Down
96 changes: 84 additions & 12 deletions src/wallaby_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,23 @@ bool Wallaby::transfer()
return true;
}

bool Wallaby::readRegister(unsigned short address, unsigned char & value)
unsigned char Wallaby::readRegister8b(unsigned char address)
{
if (address >= REG_ALL_COUNT) return false; // TODO: feedback
if (address >= REG_READABLE_COUNT) return 0;// false; // TODO: feedback

clear_buffers();

bool success = transfer();
//bool success = transfer();
//TODO: if (success == false) return false;
transfer();

if (success == false) return false;

value = read_buffer_[address];

return true;
unsigned char value = read_buffer_[address];
return value;
}

bool Wallaby::writeRegister(unsigned short address, unsigned char value)
void Wallaby::writeRegister8b(unsigned char address, unsigned char value)
{
if (address >= REG_ALL_COUNT) return false; // TODO: feedback
if (address >= REG_ALL_COUNT) return;// false; // TODO: feedback

clear_buffers();

Expand All @@ -112,9 +111,82 @@ bool Wallaby::writeRegister(unsigned short address, unsigned char value)
write_buffer_[3] = address; // at address 'address'
write_buffer_[4] = value; // with value 'value'

bool success = transfer();
//TODO: bool success = transfer();
//return success;
transfer();
}

unsigned short Wallaby::readRegister16b(unsigned char address)
{
if (address >= REG_READABLE_COUNT || address+1 >= REG_READABLE_COUNT) return 0;// false; // TODO: feedback

clear_buffers();

//TODO: bool success = transfer();
//return success;
transfer();

unsigned short value = (static_cast<unsigned short>(read_buffer_[address]) << 8) | read_buffer_[address+1];
return value;
}

void Wallaby::writeRegister16b(unsigned char address, unsigned short value)
{
if (address >= REG_ALL_COUNT || address+1 >= REG_ALL_COUNT) return;// false; // TODO: feedback

clear_buffers();

// TODO definitions for buffer inds
write_buffer_[2] = 2; // write 2 registers
write_buffer_[3] = address; // at address 'address'
write_buffer_[4] = static_cast<unsigned char>((value & 0xFF00) >> 8);
write_buffer_[5] = address + 1;
write_buffer_[6] = static_cast<unsigned char>(value & 0x00FF);

//TODO: bool success = transfer();
//return success;
transfer();
}

unsigned int Wallaby::readRegister32b(unsigned char address)
{
if (address >= REG_READABLE_COUNT || address+3 >= REG_READABLE_COUNT) return 0;// false; // TODO: feedback

clear_buffers();

//TODO: bool success = transfer();
//return success;
transfer();

unsigned short value =
(static_cast<unsigned int>(read_buffer_[address]) << 24)
| (static_cast<unsigned int>(read_buffer_[address+1]) << 16)
| (static_cast<unsigned int>(read_buffer_[address+2]) << 8)
| (static_cast<unsigned int>(read_buffer_[address+3]));

return success;
return value;
}

void Wallaby::writeRegister32b(unsigned char address, unsigned int value)
{
if (address >= REG_ALL_COUNT || address+3 >= REG_ALL_COUNT) return;// false; // TODO: feedback

clear_buffers();

// TODO definitions for buffer inds
write_buffer_[2] = 2; // write 2 registers
write_buffer_[3] = address; // at address 'address'
write_buffer_[4] = static_cast<unsigned char>((value & 0xFF000000) >> 24);
write_buffer_[5] = address + 1;
write_buffer_[6] = static_cast<unsigned char>((value & 0x00FF0000) >> 16);
write_buffer_[7] = address + 2;
write_buffer_[8] = static_cast<unsigned char>((value & 0x0000FF00) >> 8);
write_buffer_[9] = address + 3;
write_buffer_[10] = static_cast<unsigned char>((value & 0x000000FF));

//TODO: bool success = transfer();
//return success;
transfer();
}

void Wallaby::clear_buffers()
Expand Down
11 changes: 9 additions & 2 deletions src/wallaby_p.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ class Wallaby

public:
static Wallaby * instance();
bool readRegister(unsigned short address, unsigned char & value);
bool writeRegister(unsigned short address, unsigned char value);

unsigned char readRegister8b(unsigned char address);
void writeRegister8b(unsigned char address, unsigned char value);

unsigned short readRegister16b(unsigned char address);
void writeRegister16b(unsigned char address, unsigned short value);

unsigned int readRegister32b(unsigned char address);
void writeRegister32b(unsigned char address, unsigned int value);

virtual ~Wallaby();

Expand Down