Driver for 7-segment display. It supports numbers 0-9999, decimal point position and negative sign.
seven-segment-display
@/seven-segment-display
Driver for 7-segment display. It supports numbers 0-9999, decimal point position and negative sign.
LATport
latch port (def. D4)
CLKport
clock port (def.D7)
DATAport
data port (def. D8)
VALnumber
Number to display 0 to 9999
DOTnumber
Position of decimal dot, from right. Zero means no dot, 1 after frist digit..
NEGboolean
Is a negative sign required?
ERRpulse
Input number out of range
To use the node in your project you should have the marcoaita/malibrary 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 }}
// Segment byte maps for numbers 0 to 9
// const byte SEGMENT_MAP[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0X80,0X90};
//Segment byte maps for numbers 0 to 9 PLUS number with decimal dot, plus negative sign at end
const byte SEGMENT_MAP[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0X80,0X90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xBF};
/* Byte maps to select digit 1 to 4 */
const byte SEGMENT_SELECT[] = {0xF1,0xF2,0xF4,0xF8};
// int LATCH_DIO=4; // These are for the 7-segment on the ks0183 multifuncion shield
// int CLK_DIO=7;
// int DATA_DIO=8;
int LATCH_DIO; // 7-segment pins
int CLK_DIO;
int DATA_DIO;
int Value;
int dot_pos;
bool neg_sign;
void wnts(int Segment, int Value) {
digitalWrite(LATCH_DIO,LOW);
shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_MAP[Value]);
shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_SELECT[Segment] );
digitalWrite(LATCH_DIO,HIGH);
}
void evaluate(Context ctx) {
/* Define shift register pins used for seven segment display */
LATCH_DIO = getValue<input_LAT>(ctx);
CLK_DIO = getValue<input_CLK>(ctx);
DATA_DIO = getValue<input_DATA>(ctx);
/* Set DIO pins to outputs */
pinMode(LATCH_DIO,OUTPUT);
pinMode(CLK_DIO,OUTPUT);
pinMode(DATA_DIO,OUTPUT);
// read value to display
Value = int(getValue<input_VAL>(ctx)); // cast to int, because XOD doesn't allow me an easy conversion to int
dot_pos = int(getValue<input_DOT>(ctx)); //position of decimal dot
neg_sign = getValue<input_NEG>(ctx);//do we need a negative sign to the left ?
if (Value > 9999) {
emitValue<output_ERR>(ctx, 1);
return;
}
if (isTimedOut(ctx)) {
// while(true){
Value = int(getValue<input_VAL>(ctx));
dot_pos = int(getValue<input_DOT>(ctx)); //position of decimal dot
neg_sign = getValue<input_NEG>(ctx); ; //do we need a negative sign to the left ?
if (Value > 9999) {
emitValue<output_ERR>(ctx, 1);
return;
}
wnts( 0 , neg_sign ? 20: (int((Value / 1000) % 10)+(dot_pos== 3 ? 10 : 0)));
wnts( 1 , int((Value / 100) % 10)+ (dot_pos== 2 ? 10 : 0));
wnts( 2 , int((Value / 10) % 10)+(dot_pos== 1 ? 10 : 0));
wnts( 3 , int((Value) % 10));
// wnts( 0 , int((Value / 1000) % 10));
// wnts( 1 , int((Value / 100) % 10));
// wnts( 2 , int((Value / 10) % 10)+ (dot_pos==1 ? 10 : 0 ));
// wnts( 3 , int((Value) % 10));
// }
// Schedule immediate re-evaluation
setTimeout(ctx, 0);
}
setTimeout(ctx, 0);
}