Hi @xodballxod,
Great to see another device made accessible to xoders!
I’ve attached a xodball that shows you one way of creating a custom type for your library:
digipot.xodball (7.2 KB)
I don’t have a digital potentiometer, so my testing is limited to checking that the code compiles.
My first step was to create a new node, which I’ve named digipot, with inputs of all the types you would like to combine in your custom type. The only output is an output-self representing your custom type.
The not-implemented-in-xod node contains the following code:
node {
meta {
struct Type {
SPIClass* spi;
uint8_t cs;
bool bits8;
Number MOhm;
uint8_t maxStep;
void writeStep (uint8_t step) {
::digitalWrite(cs, false);
uint8_t address = bits8 ? 0x10 : 0x00 ; // Choose which wiper address (8-bit format)
spi->transfer(address);
spi->transfer(step);
::digitalWrite(cs, true);
}
};
}
void evaluate(Context ctx) {
if (isSettingUp()){
Type digipot;
digipot.spi = getValue<input_SPI>(ctx);
static_assert(isValidDigitalPort(constant_input_CS), "must be a valid digital port");
::pinMode(constant_input_CS, OUTPUT); // set pinmode output for Chip Select;
digipot.cs = constant_input_CS;
digipot.bits8 = getValue<input_bits8>(ctx);
digipot.MOhm = getValue<input_MOhm>(ctx);
digipot.maxStep = digipot.bits8 ? 255 : 128;
emitValue<output_digipot>(ctx, digipot);
}
}
}
I’ve defined your custom type as a struct. Inside the struct is a variable for each of the inputs to the digipot node. I’ve also created one member function, writeStep, for demonstration purposes. A full implementation would include functions for all the actions you require (increment, decrement, etc.).
I modified your digipot-write-step node to have only three inputs:
- the digipot custom type
- number of steps
- update
The not-implement-in-XOD node calls the writeStep function defined above:
node {
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))
return;
auto digipot = getValue<input_digipot>(ctx);
digipot.writeStep(getValue<input_STEP>(ctx));
emitValue<output_DONE>(ctx, 1);
}
}
I was able to compile the following example patch:
You mention your plan to create a library with richer functionality. The quickest and easiest route might be to wrap an existing Arduino library, such as https://github.com/kulbhushanchand/MCP4251