text-lcd-16x2-i2c

xod/common-hardware/text-lcd-16x2-i2c

Drives a text LCD screen with a PCF8574 or PCA8574 I²C expander module. Usually have a value in range 0x20-0x27 or 0x38-0x3F. Consult LCD/expander documentation to know the exact value. Possible errors: — Invalid I2C address
text-lcd-16x2-i2c
@/text-lcd-16x2-i2c
Deprecated: Use `xod-dev/text-lcd/text-lcd-i2c-16x2` instead.
Drives a text LCD screen with a PCF8574 or PCA8574 I²C expander module. Usually have a value in range 0x20-0x27 or 0x38-0x3F. Consult LCD/expander documentation to know the exact value. Possible errors: — Invalid I2C address
ADDRbyte
I²C address of the expander chip.
BLboolean
Backlight enable/disable
L1string
Text for the first line
L2string
Text for the second line
UPDpulse
Triggers new write
text-lcd-16x2-i2c
ADDR
BL
L1
L2
UPD
DONE
DONEpulse
Fires when write is done

C++ implementation

#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_UPD
#pragma XOD error_raise enable

{{#global}}
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
{{/global}}

struct State {
    LiquidCrystal_I2C* lcd;
};

{{ GENERATED_CODE }}

void printLine(LiquidCrystal_I2C* lcd, uint8_t lineIndex, XString str) {
    lcd->setCursor(0, lineIndex);
    uint8_t whitespace = 16;
    for (auto it = str.iterate(); it; ++it, --whitespace)
        lcd->write(*it);

    // Clear the rest of the line
    while (whitespace--)
        lcd->write(' ');
}

void evaluate(Context ctx) {
    if (!isInputDirty<input_UPD>(ctx))
        return;

    State* state = getState(ctx);
    auto lcd = state->lcd;
    if (!state->lcd) {
        uint8_t addr = getValue<input_ADDR>(ctx);
        if (addr > 127) {
            raiseError(ctx);
            return;
        }
        state->lcd = lcd = new LiquidCrystal_I2C(addr, 16, 2);
        lcd->begin();
    }

    printLine(lcd, 0, getValue<input_L1>(ctx));
    printLine(lcd, 1, getValue<input_L2>(ctx));
    lcd->setBacklight(getValue<input_BL>(ctx));
    emitValue<output_DONE>(ctx, 1);
}