Seven segment display

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
IcarusLives
Posts: 117
Joined: 17 Jan 2016 23:55

Seven segment display

#1 Post by IcarusLives » 01 Nov 2018 08:42

Hello all ^-^

WINDOWS 10 ONLY!!!

Image

The usage is very simple

Code: Select all

call :sevenSegmentDisplay index color X Y
Example:

Code: Select all

call :sevenSegmentDisplay 3 10 8 8
Which means basically

Display number 3,
color is 10,
x = 8
y = 8
Enjoy!

THIS CODE RELIES ON %PLOT% which draws pixels in color in the console. Paste this at the top of your code, or the seven segment wont work

Code: Select all

rem ----------------------------------------------------------------------------------------------------------------------------
set ^"LF=^

^" Above empty line is required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
<nul set /p "=!esc![?25l"

rem %plot% x y 0-255 CHAR
set plot=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-4" %%1 in ("^!args^!") do (%\n%
  set "screen=^!screen^!!esc![%%2;%%1H!esc![38;5;%%3m%%~4!esc![0m"%\n%
)) else set args=

rem data for seven segment display
set /a "index=0", "n[0]=0x7E", "n[1]=0x30", "n[2]=0x6D", "n[3]=0x79", "n[4]=0x33", "n[5]=0x5B", "n[6]=0x5F", "n[7]=0x70", "n[8]=0x7F", "n[9]=0x7B"
rem ----------------------------------------------------------------------------------------------------------------------------
Example code:

Code: Select all

@echo off & setlocal enableDelayedExpansion & mode 50,50

rem ----------------------------------------------------------------------------------------------------------------------------
set ^"LF=^

^" Above empty line is required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
<nul set /p "=!esc![?25l"

rem %plot% x y 0-255 CHAR
set plot=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-4" %%1 in ("^!args^!") do (%\n%
  set "screen=^!screen^!!esc![%%2;%%1H!esc![38;5;%%3m%%~4!esc![0m"%\n%
)) else set args=
set /a "index=0", "n[0]=0x7E", "n[1]=0x30", "n[2]=0x6D", "n[3]=0x79", "n[4]=0x33", "n[5]=0x5B", "n[6]=0x5F", "n[7]=0x70", "n[8]=0x7F", "n[9]=0x7B"
rem ----------------------------------------------------------------------------------------------------------------------------


rem main loop ------------------------------------------------------------------------------------------------------------------
set /a "index=0", "color=10"
:main
 call :sevenSegmentDisplay index color 8 8
 
 rem change index
 set /a "index=(index + 1) %% 10"
 rem create slight delay for visual purposes
 for /l %%a in (1,15,1000000) do rem
 rem display the screen
 <nul set /p "=!screen!" & set "screen="
goto :main
rem ----------------------------------------------------------------------------------------------------------------------------



:sevenSegmentDisplay
 set /a "index=%~1", "posX_1=%~3", "posX_2=%~3 + 1", "posX_3=%~3 + 2", "posX_4=%~3 - 1", "posY_1=%~4", "posY_2=%~4 + 1", "posY_3=%~4 + 2", "posY_4=%~4 + 3", "posY_5=%~4 + 4", "posY_6=%~4 + 5", "posY_7=%~4 + 6"

 for %%i in (!index!) do (
 set /a "a=!%~2! * ((n[%%i] >> 6) & 1)"
 %plot% !posX_1! !posY_1! !a! Û
 %plot% !posX_2! !posY_1! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 5) & 1)"
 %plot% !posX_3! !posY_2! !a! Û
 %plot% !posX_3! !posY_3! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 4) & 1)"
 %plot% !posX_3! !posY_5! !a! Û
 %plot% !posX_3! !posY_6! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 3) & 1)"
 %plot% !posX_1! !posY_7! !a! Û
 %plot% !posX_2! !posY_7! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 2) & 1)"
 %plot% !posX_4! !posY_5! !a! Û
 %plot% !posX_4! !posY_6! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 1) & 1)"
 %plot% !posX_4! !posY_2! !a! Û
 %plot% !posX_4! !posY_3! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 0) & 1)"
 %plot% !posX_1! !posY_4! !a! Û
 %plot% !posX_2! !posY_4! !a! Û
 )
goto :eof
The function:

Code: Select all

:sevenSegmentDisplay
 set /a "index=%~1", "posX_1=%~3", "posX_2=%~3 + 1", "posX_3=%~3 + 2", "posX_4=%~3 - 1", "posY_1=%~4", "posY_2=%~4 + 1", "posY_3=%~4 + 2", "posY_4=%~4 + 3", "posY_5=%~4 + 4", "posY_6=%~4 + 5", "posY_7=%~4 + 6"

 for %%i in (!index!) do (
 set /a "a=!%~2! * ((n[%%i] >> 6) & 1)"
 %plot% !posX_1! !posY_1! !a! Û
 %plot% !posX_2! !posY_1! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 5) & 1)"
 %plot% !posX_3! !posY_2! !a! Û
 %plot% !posX_3! !posY_3! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 4) & 1)"
 %plot% !posX_3! !posY_5! !a! Û
 %plot% !posX_3! !posY_6! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 3) & 1)"
 %plot% !posX_1! !posY_7! !a! Û
 %plot% !posX_2! !posY_7! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 2) & 1)"
 %plot% !posX_4! !posY_5! !a! Û
 %plot% !posX_4! !posY_6! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 1) & 1)"
 %plot% !posX_4! !posY_2! !a! Û
 %plot% !posX_4! !posY_3! !a! Û
 set /a "a=!%~2! * ((n[%%i] >> 0) & 1)"
 %plot% !posX_1! !posY_4! !a! Û
 %plot% !posX_2! !posY_4! !a! Û
 )
goto :eof

einstein1969
Expert
Posts: 763
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Seven segment display

#2 Post by einstein1969 » 01 Nov 2018 10:24

Hi Icarus, very nice work!
This is very fast color graphics

IcarusLives
Posts: 117
Joined: 17 Jan 2016 23:55

Re: Seven segment display

#3 Post by IcarusLives » 01 Nov 2018 10:41

Yes! Unfortunately it relies on the windows 10 VT100 escape sequences so this will not work for any windows version below that.. :(

Post Reply