// Implements 16,14, and 7-segment display characters // Adapted from the Segmented LED Display ASCII Library // at https://github.com/dmadison/LED-Segment-ASCII // (MIT Licensed by Dave Madison) // p5.js version by Golan Levin, June 2025 let CURRENT_FONT; let GLYPH_ASPECT_RATIO = 0.55; let GLYPH_SPACING = 0.875; let GLYPH_SLANT = 5; // degrees function setup() { createCanvas(800, 400); } function draw() { background(0); strokeWeight(1); strokeCap(ROUND); stroke(255); let str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; let str2 = "0123456789 &@* HELLO WORLD"; CURRENT_FONT = SixteenSegmentASCII; drawString(str1, 50, 50, 25); drawString(str2, 50, 90, 25); CURRENT_FONT = FourteenSegmentASCII; drawString(str1, 50, 170, 25); drawString(str2, 50, 210, 25); CURRENT_FONT = SevenSegmentASCII; drawString(str1, 50, 290, 25); drawString(str2, 50, 330, 25); fill(127); noStroke(); text("SixteenSegmentASCII", 640, 60); text("FourteenSegmentASCII", 640, 180); text("SevenSegmentASCII", 640, 300); } function keyPressed(){ if (key == ' '){ save("multisegment_display.png"); } } //---------------------------------------------- function drawGlyph7Segments (ch, px, py, sc) { let charCode = ch.charCodeAt(0) - 32; if (charCode > 0 && charCode < CURRENT_FONT.length) { let charData = CURRENT_FONT[charCode]; if (!charData) return; const sx = sc * 0.5 * GLYPH_ASPECT_RATIO; const sy = sc * 0.5; const d = 0.25; const e = d * GLYPH_ASPECT_RATIO; const f = 0.7; const drawCoords = [ [0 + d, 0, 2 - d, 0], [2, 0 + e, 2, 1 - e * f], [2, 1 + e * f, 2, 2 - e], [2 - d, 2, 0 + d, 2], [0, 2 - e, 0, 1 + e * f], [0, 1 - e * f, 0, 0 + e], [0 + d, 1, 2 - d, 1], [2.5, 2 - e, 2.5 - d, 2], ]; push(); translate(px, py); shearX(radians(0 - GLYPH_SLANT)); for (let i = 0; i < charData.length; i++) { let b = charData[i]; if (b == "1") { let L = drawCoords[7 - i]; line(L[0] * sx, L[1] * sy, L[2] * sx, L[3] * sy); } } pop(); } } //---------------------------------------------- function drawGlyph14Segments (ch, px, py, sc) { let charCode = ch.charCodeAt(0) - 32; if (charCode > 0 && charCode < CURRENT_FONT.length) { let charData = CURRENT_FONT[charCode]; if (!charData) return; const sx = sc * 0.5 * GLYPH_ASPECT_RATIO; const sy = sc * 0.5; const d = 0.25; const e = d * GLYPH_ASPECT_RATIO; const f = 0.7; const drawCoords = [ [0 + d, 0, 2 - d, 0], [2, 0 + e, 2, 1 - e * f], [2, 1 + e * f, 2, 2 - e], [2 - d, 2, 0 + d, 2], [0, 2 - e, 0, 1 + e * f], [0, 1 - e * f, 0, 0 + e], [0 + d, 1, 1 - d * f, 1], [1 + d * f, 1, 2 - d, 1], [0 + d, 0 + e, 1 - d, 1 - e], [1, 0 + e, 1, 1 - e], [2 - d, 0 + e, 1 + d, 1 - e], [0 + d, 2 - e, 1 - d, 1 + e], [1, 2 - e, 1, 1 + e], [2 - d, 2 - e, 1 + d, 1 + e], [2.5, 2 - e, 2.5 - d, 2], ]; push(); translate(px, py); shearX(radians(0 - GLYPH_SLANT)); for (let i = 0; i < charData.length; i++) { let b = charData[i]; if (b == "1") { let L = drawCoords[14 - i]; line(L[0] * sx, L[1] * sy, L[2] * sx, L[3] * sy); } } pop(); } } //---------------------------------------------- function drawGlyph16Segments(ch, px, py, sc) { let charCode = ch.charCodeAt(0) - 32; if (charCode > 0 && charCode < CURRENT_FONT.length) { let charData = CURRENT_FONT[charCode]; if (!charData) return; const sx = sc * 0.5 * GLYPH_ASPECT_RATIO; const sy = sc * 0.5; const d = 0.25; const e = d * GLYPH_ASPECT_RATIO; const f = 0.7; const drawCoords = [ [0 + d, 0, 1 - d * f, 0], [1 + d * f, 0, 2 - d, 0], [2, 0 + e, 2, 1 - e * f], [2, 1 + e * f, 2, 2 - e], [2 - d, 2, 1 + d * f, 2], [1 - d * f, 2, 0 + d, 2], [0, 2 - e, 0, 1 + e * f], [0, 1 - e * f, 0, 0 + e], [0 + d, 0 + e, 1 - d, 1 - e], [1, 0 + e, 1, 1 - e], [2 - d, 0 + e, 1 + d, 1 - e], [2 - d, 1, 1 + d * f, 1], [2 - d, 2 - e, 1 + d, 1 + e], [1, 2 - e, 1, 1 + e], [0 + d, 2 - e, 1 - d, 1 + e], [0 + d, 1, 1 - d * f, 1], [2.5, 2 - e, 2.5 - d, 2], ]; push(); translate(px, py); shearX(radians(0 - GLYPH_SLANT)); for (let i = 0; i < charData.length; i++) { let b = charData[i]; if (b == "1") { let L = drawCoords[16 - i]; line(L[0] * sx, L[1] * sy, L[2] * sx, L[3] * sy); } } pop(); } } function drawString(str, px, py, sc) { for (let i = 0; i < str.length; i++) { if (CURRENT_FONT === SevenSegmentASCII){ drawGlyph7Segments(str[i], px, py, sc); } else if (CURRENT_FONT === FourteenSegmentASCII){ drawGlyph14Segments(str[i], px, py, sc); } else if (CURRENT_FONT === SixteenSegmentASCII){ drawGlyph16Segments(str[i], px, py, sc); } px += sc * GLYPH_SPACING; } } // ASCII codes 32-126, adapted from: // https://github.com/dmadison/LED-Segment-ASCII // Yes, I know there are faster ways of implementing // this than using strings. I kept this for simplicity. const FourteenSegmentASCII = [ "000000000000000", "100000000000110", "000001000000010", "001001011001110", "001001011101101", "011111111100100", "010001101011001", "000001000000000", "010010000000000", "000100100000000", "011111111000000", "001001011000000", "000100000000000", "000000011000000", "100000000000000", "000110000000000", "000110000111111", "000010000000110", "000000011011011", "000000010001111", "000000011100110", "010000001101001", "000000011111101", "000000000000111", "000000011111111", "000000011101111", "001001000000000", "000101000000000", "010010001000000", "000000011001000", "000100110000000", "101000010000011", "000001010111011", "000000011110111", "001001010001111", "000000000111001", "001001000001111", "000000001111001", "000000001110001", "000000010111101", "000000011110110", "001001000001001", "000000000011110", "010010001110000", "000000000111000", "000010100110110", "010000100110110", "000000000111111", "000000011110011", "010000000111111", "010000011110011", "000000011101101", "001001000000001", "000000000111110", "000110000110000", "010100000110110", "010110100000000", "001010100000000", "000110000001001", "000000000111001", "010000100000000", "000000000001111", "010100000000000", "000000000001000", "000000100000000", "001000001011000", "010000001111000", "000000011011000", "000100010001110", "000100001011000", "001010011000000", "000010010001110", "001000001110000", "001000000000000", "000101000010000", "011011000000000", "000000000110000", "001000011010100", "001000001010000", "000000011011100", "000000101110000", "000010010000110", "000000001010000", "010000010001000", "000000001111000", "000000000011100", "000100000010000", "010100000010100", "010110100000000", "000001010001110", "000100001001000", "000100101001001", "001001000000000", "010010010001001", "000110011000000", ]; const SixteenSegmentASCII = [ "00000000000000000", "10000000000001100", "00000001000000100", "01010101000111100", "01010101010111011", "01110111010011001", "01001001101110001", "00000001000000000", "00001010000000000", "00100000100000000", "01111111100000000", "01010101000000000", "00100000000000000", "01000100000000000", "10000000000000000", "00100010000000000", "00100010011111111", "00000010000001100", "01000100001110111", "00000100000111111", "01000100010001100", "01001000010110011", "01000100011111011", "00000000000001111", "01000100011111111", "01000100010111111", "00010001000000000", "00100001000000000", "01001010000000000", "01000100000110000", "00100100100000000", "10010100000000111", "00000101011110111", "01000100011001111", "00010101000111111", "00000000011110011", "00010001000111111", "01000000011110011", "01000000011000011", "00000100011111011", "01000100011001100", "00010001000110011", "00000000001111100", "01001010011000000", "00000000011110000", "00000010111001100", "00001000111001100", "00000000011111111", "01000100011000111", "00001000011111111", "01001100011000111", "01000100010111011", "00010001000000011", "00000000011111100", "00100010011000000", "00101000011001100", "00101010100000000", "00010010100000000", "00100010000110011", "00010001000010010", "00001000100000000", "00010001000100001", "00101000000000000", "00000000000110000", "00000000100000000", "01010000001110000", "01010000011100000", "01000000001100000", "00010100000011100", "01100000001100000", "01010101000000010", "01010001010100001", "01010000011000000", "00010000000000000", "00010001001100000", "00011011000000000", "00000000011000000", "01010100001001000", "01010000001000000", "01010000001100000", "01000001011000001", "01010001010000001", "01000000001000000", "01010000010100001", "01000000011100000", "00010000001100000", "00100000001000000", "00101000001001000", "00101010100000000", "00000101000011100", "01100000000100000", "01010001000010010", "00010001000000000", "00010101000100001", "01100110000000000", "00000000000000000", ]; const SevenSegmentASCII = [ "00000000", "10000110", "00100010", "01111110", "01101101", "11010010", "01000110", "00100000", "00101001", "00001011", "00100001", "01110000", "00010000", "01000000", "10000000", "01010010", "00111111", "00000110", "01011011", "01001111", "01100110", "01101101", "01111101", "00000111", "01111111", "01101111", "00001001", "00001101", "01100001", "01001000", "01000011", "11010011", "01011111", "01110111", "01111100", "00111001", "01011110", "01111001", "01110001", "00111101", "01110110", "00110000", "00011110", "01110101", "00111000", "00010101", "00110111", "00111111", "01110011", "01101011", "00110011", "01101101", "01111000", "00111110", "00111110", "00101010", "01110110", "01101110", "01011011", "00111001", "01100100", "00001111", "00100011", "00001000", "00000010", "01011111", "01111100", "01011000", "01011110", "01111011", "01110001", "01101111", "01110100", "00010000", "00001100", "01110101", "00110000", "00010100", "01010100", "01011100", "01110011", "01100111", "01010000", "01101101", "01111000", "00011100", "00011100", "00010100", "01110110", "01101110", "01011011", "01000110", "00110000", "01110000", "00000001", "00000000", ]; //https://github.com/dmadison/LED-Segment-ASCII //https://scruss.com/blog/2016/05/21/twentyfoursixteen-a-17-segment-alpha-lcd-font/ // derived from the HP/Siemens/Litronix DL-2416 17-segment alphanumeric 17 segment LED display matrix.