7-segment-convertor-cpp

dancojocaru2000/7-segment/7-segment-convertor-cpp

7-segment-convertor-cpp
@/7-segment-convertor-cpp
VALstring
Value (digit and optional dot to turn on decimal separator). Examples are "2" and "4."
UPDpulse
Updates the value.
CCboolean
True if the 7-segment display is Common Cathode (the middle pins are connected to ground)
7-segment-convertor-cpp
VAL
UPD
CC
RES
OK
ERR
ERRpulse
Pulsed if the input value is invalid.
OKpulse
Pulsed after a successful update.
RESbyte
To use the node in your project you should have the dancojocaru2000/7-segment library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

struct State {
};

{{ GENERATED_CODE }}

const int digits[] = {0b1111110, 0b0011000, 0b1101101, 0b0111101, 0b0011011, 0b0110111, 0b1110111, 0b0011100, 0b1111111, 0b01111111};

void evaluate(Context ctx) {
    if (!isInputDirty<input_UPD>(ctx)) return;
    auto value = getValue<input_VAL>(ctx);
    auto common_gnd = getValue<input_CC>(ctx);

    int digit;

    auto iter = value.iterate();

    char c = *iter;
    if (!('0' <= c && c <= '9')) {
        emitValue<output_ERR>(ctx, true);
        return;
    }
    digit = c - '0';

    ++iter;
    bool dot;
    if (iter && *iter == '.') {
        dot = true;
    }
    else dot = false;

    auto bits = digits[digit];
    if (dot) {
        bits |= (1 << 7);
    }
    else {
        bits = ~(~bits | (1 << 7));
    }

    if (!common_gnd) bits = ~bits;

    emitValue<output_RES>(ctx, bits);

//     emitValue<output_DOT>(ctx, bits & (1 << 7));
//     emitValue<output_A>(ctx, bits & (1 << 6));
//     emitValue<output_B>(ctx, bits & (1 << 5));
//     emitValue<output_C>(ctx, bits & (1 << 4));
//     emitValue<output_D>(ctx, bits & (1 << 3));
//     emitValue<output_E>(ctx, bits & (1 << 2));
//     emitValue<output_F>(ctx, bits & (1 << 1));
//     emitValue<output_G>(ctx, bits & (1 << 0));

    emitValue<output_OK>(ctx, true);
}