-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
445 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sharpi | ||
{ | ||
public partial class Pot | ||
{ | ||
// pot (common interface) | ||
|
||
[DllImport("sharpi")] | ||
[return: MarshalAs(UnmanagedType.LPStr)] | ||
internal static extern string pot_get_description(IntPtr sensor); | ||
|
||
[DllImport("sharpi")] | ||
internal static extern void pot_delete(IntPtr sensor); | ||
|
||
|
||
// pot (special functions for each type) | ||
|
||
|
||
// AMG8833 | ||
[DllImport("sharpi")] | ||
internal static extern IntPtr pot_ds3502_new(byte i2cAddress); | ||
|
||
[DllImport("sharpi")] | ||
internal static extern IntPtr pot_ds3502_new_x(byte i2cAddress, [MarshalAs(UnmanagedType.LPStr)] string i2cDevice); | ||
|
||
|
||
[DllImport("sharpi")] | ||
internal static extern void pot_ds3502_power_on(IntPtr sensor); | ||
|
||
[DllImport("sharpi")] | ||
internal static extern void pot_ds3502_power_off(IntPtr sensor); | ||
|
||
[DllImport("sharpi")] | ||
internal static extern byte pot_ds3502_get_wiper(IntPtr pot); | ||
|
||
[DllImport("sharpi")] | ||
internal static extern void pot_ds3502_set_wiper(IntPtr pot, byte value); | ||
|
||
[DllImport("sharpi")] | ||
internal static extern void pot_ds3502_set_wiper_persistent(IntPtr pot, byte value); | ||
} | ||
|
||
// sensor | ||
|
||
public abstract class PotBase : IDisposable | ||
{ | ||
protected IntPtr _handle = IntPtr.Zero; | ||
|
||
public PotBase(IntPtr handle) | ||
{ | ||
_handle = handle; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_handle != IntPtr.Zero) | ||
{ | ||
Pot.pot_delete(_handle); | ||
_handle = IntPtr.Zero; | ||
} | ||
} | ||
|
||
public string Description | ||
{ | ||
get | ||
{ | ||
if (_handle != IntPtr.Zero) | ||
{ | ||
return Pot.pot_get_description(_handle); | ||
} | ||
else | ||
{ | ||
return ""; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
public static partial class Pot | ||
{ | ||
public class Ds3502 : PotBase | ||
{ | ||
/// <summary> | ||
/// Ds3502 digipot, standard i2caddress 0x28 | ||
/// </summary> | ||
public Ds3502(byte i2caddress) | ||
: base(pot_ds3502_new(i2caddress)) | ||
{ | ||
} | ||
|
||
public Ds3502(byte i2caddress, string i2cdevice) | ||
: base(pot_ds3502_new_x(i2caddress, i2cdevice)) | ||
{ | ||
} | ||
|
||
public void PowerOn() | ||
{ | ||
pot_ds3502_power_on(_handle); | ||
} | ||
|
||
public void PowerOff() | ||
{ | ||
pot_ds3502_power_off(_handle); | ||
} | ||
|
||
public byte GetWiper() | ||
{ | ||
return pot_ds3502_get_wiper(_handle); | ||
} | ||
|
||
/// <summary> | ||
/// sets the wiper value | ||
/// </summary> | ||
/// <param name="value">0..127</param> | ||
public void SetWiper(byte value) | ||
{ | ||
pot_ds3502_set_wiper(_handle, value); | ||
} | ||
|
||
/// <summary> | ||
/// sets the wiper value and stores it in the EEPROM (~50000 writes) | ||
/// </summary> | ||
/// <param name="value">0..127</param> | ||
public void SetWiperPersistent(byte value) | ||
{ | ||
pot_ds3502_set_wiper_persistent(_handle, value); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
/// <summary> | ||
/// The base class for pots, all pots participating in the C-Api have to derive from this | ||
/// </summary> | ||
class Pot | ||
{ | ||
|
||
public: | ||
Pot() {}; | ||
virtual ~Pot() {}; | ||
virtual std::string GetDescription() = 0; | ||
virtual const char* GetDescriptionC() = 0; | ||
virtual void PowerOn() = 0; | ||
virtual void PowerOff() = 0; | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#include "PotDs3502.h" | ||
|
||
string PotDs3502::description = | ||
"----------------------------------------------\n" | ||
" Maxim DS3502 \n" | ||
" \n" | ||
" The DS3502 is a 7-bit, nonvolatile (NV) \n" | ||
" digital potentiometer (POT) featuring an \n" | ||
" output voltage range of up to 15.5V. \n" | ||
" Programming is accomplished by an I2C- \n" | ||
" compatible interface, which can operate at \n" | ||
" speeds of up to 400kHz.External voltages are \n" | ||
" applied at the RL and RH inputs to define the\n" | ||
" lowest and highest potentiometer outputs. \n" | ||
" (10 uSOP package) \n" | ||
" \n" | ||
" I2c slave address adjustable via A0 and A1 \n" | ||
" \n" | ||
" A convenient breakout board is available \n" | ||
" from Adafruit. \n" | ||
" \n" | ||
"config: \n" | ||
" \n" | ||
" edit /boot/config.txt \n" | ||
" dtparam=i2c_arm=on,i2c_arm_baudrate=400000 \n" | ||
" \n" | ||
"wiring: \n" | ||
" \n" | ||
" rpi physical pins \n" | ||
" \n" | ||
" vin -- 1 2 \n" | ||
" sda -- 3 4 \n" | ||
" scl -- 5 6 \n" | ||
" 7 8 \n" | ||
" gnd -- 9 10 ---------- \n" | ||
" (int) -- 11 12 sda -| + |- scl \n" | ||
" 13 14 gnd -| |- v+ \n" | ||
" 15 16 vcc -| ds3502 |- rl \n" | ||
" 17 18 a1 -| |- rw \n" | ||
" 19 20 a0 -| |- rh \n" | ||
" 21 22 ---------- \n" | ||
" 23 24 \n" | ||
" 25 26 \n" | ||
" 27 28 \n" | ||
" 29 30 \n" | ||
" 31 32 \n" | ||
" 33 34 \n" | ||
" 35 36 \n" | ||
" 37 38 \n" | ||
" 39 40 \n" | ||
" \n" | ||
"----------------------------------------------\n" | ||
; | ||
|
||
const string PotDs3502::I2CDEVICE = "/dev/i2c-1"; | ||
|
||
PotDs3502::PotDs3502(uint8_t i2cAddress) | ||
: PotDs3502::PotDs3502(i2cAddress, I2CDEVICE) | ||
{ | ||
} | ||
|
||
PotDs3502::PotDs3502(uint8_t i2cAddress, string i2cDevice) | ||
{ | ||
_i2cAddress = i2cAddress; | ||
_i2cDevice = i2cDevice; | ||
} | ||
|
||
PotDs3502::~PotDs3502() | ||
{ | ||
} | ||
|
||
string PotDs3502::GetDescription() | ||
{ | ||
return description; | ||
}; | ||
|
||
const char* PotDs3502::GetDescriptionC() | ||
{ | ||
return description.c_str(); | ||
} | ||
|
||
void PotDs3502::PowerOn() | ||
{ | ||
if (_devI2c == NULL) | ||
{ | ||
_devI2c = new DevI2c(_i2cDevice, _i2cAddress); | ||
if (!_devI2c->Open()) | ||
{ | ||
delete _devI2c; | ||
_devI2c = NULL; | ||
} | ||
|
||
_devI2c->WriteRegU8(Regs::CR::addr, Regs::CR::Modes::Mode_1_WR_ONLY); | ||
} | ||
} | ||
|
||
void PotDs3502::PowerOff() | ||
{ | ||
if (_devI2c != NULL) | ||
{ | ||
_devI2c->Close(); | ||
delete _devI2c; | ||
_devI2c = NULL; | ||
} | ||
} | ||
|
||
|
||
uint8_t PotDs3502::GetWiper(void) | ||
{ | ||
if (_devI2c == NULL) { return 0; }; | ||
|
||
uint8_t value; | ||
_devI2c->ReadRegU8(Regs::WR::addr, &value); | ||
return value; | ||
} | ||
|
||
void PotDs3502::SetWiper(uint8_t value) | ||
{ | ||
if (_devI2c == NULL) { return; }; | ||
|
||
if (value > 127) { return; } | ||
|
||
_devI2c->WriteRegU8(Regs::WR::addr, value); | ||
} | ||
|
||
void PotDs3502::SetWiperPersistent(uint8_t value) | ||
{ | ||
if (_devI2c == NULL) { return; }; | ||
|
||
if (value > 127) { return; } | ||
|
||
_devI2c->WriteRegU8(Regs::CR::addr, Regs::CR::Modes::Mode_0_WR_IVR); | ||
_devI2c->WriteRegU8(Regs::WR::addr, value); | ||
usleep(20000); // max EEPROM write time 20ms | ||
_devI2c->WriteRegU8(Regs::CR::addr, Regs::CR::Modes::Mode_1_WR_ONLY); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#pragma once | ||
|
||
#include "Pot.h" | ||
#include "DevI2c.h" | ||
|
||
using namespace std; | ||
|
||
class PotDs3502 : Pot | ||
{ | ||
private: | ||
static string description; | ||
|
||
private: | ||
static const string I2CDEVICE; | ||
|
||
string _i2cDevice = I2CDEVICE; | ||
DevI2c* _devI2c = NULL; | ||
uint8_t _i2cAddress; | ||
|
||
public: | ||
PotDs3502(uint8_t i2cAddress); | ||
PotDs3502(uint8_t i2cAddress, string i2cDevice); | ||
~PotDs3502(); | ||
|
||
string GetDescription(); | ||
const char* GetDescriptionC(); | ||
|
||
void PowerOn(); | ||
void PowerOff(); | ||
|
||
uint8_t GetWiper(void); | ||
void SetWiper(uint8_t value); | ||
void SetWiperPersistent(uint8_t value); // 50000 - 200000 EEPROM writes | ||
|
||
public: | ||
class Regs | ||
{ | ||
public: | ||
/// <summary> | ||
/// Control Register | ||
/// </summary> | ||
class CR | ||
{ | ||
public: | ||
static const uint8_t addr = 0x02; | ||
class Modes | ||
{ | ||
public: | ||
enum : uint8_t | ||
{ | ||
/// <summary> | ||
/// mode0: write to memory address 00h, write to both WR (Wiper Register) and IVR (Initial Value Register), read from WR | ||
/// mode1: write to memory address 00h, write to WR, read from WR, for writing from SRAM to EEPROM an i2c stop must occur | ||
/// </summary> | ||
Mode_0_WR_IVR = 0x00, | ||
Mode_1_WR_ONLY = 0x80 | ||
}; | ||
}; | ||
}; | ||
|
||
/// <summary> | ||
/// Wiper Register | ||
/// </summary> | ||
class WR | ||
{ | ||
public: | ||
static const uint8_t addr = 0x00; | ||
|
||
}; | ||
}; | ||
}; |
Oops, something went wrong.