Move to own folder

This commit is contained in:
Brahim Hadriche committed 2022-11-03 19:00:11 -04:00
1 parent 94192446af
commit d13976fd83
27 files changed
+5 -3

No files matched your search

@@ -0,0 +1,145 @@
sub init()
m.top.observeFieldScoped("qrcode", "onQRCodeChanged")
m.top.observeFieldScoped("text", "onTextChanged")
end sub
' @private
sub onQRCodeChanged(msg as object)
qrcode = msg.getData()
if qrcode <> invalid
if qrcode.status = "ready"
m.top.qrstring = toASCII(qrcode, m.top.cellSize, m.top.padding)
end if
else
m.top.qrstring = ""
end if
end sub
' @private
sub onTextChanged(msg as object)
qrcode = createObject("roSGNode", "QRCode")
qrcode.callFunc("addData", msg.getData())
qrcode.callFunc("make")
m.top.qrcode = qrcode
end sub
' @public
function toASCII(qrcode as object, cellSize = 1 as integer, padding = 1 as integer) as string
if cellSize < 1
cellSize = 1
end if
if cellSize < 2
return toHalfASCII(qrcode, padding)
end if
cellSize -= 1
if padding < 0
padding = cellSize * 2
end if
size = (qrcode.moduleCount * cellSize) + (padding * 2)
min = padding
max = size - padding
white = String(cellSize, "██")
black = String(cellSize, " ")
ascii = ""
line = ""
NL = chr(10)
for y = 0 to size - 1 step 1
r = fix((y - min) / cellSize)
line = ""
for x = 0 to size - 1 step 1
p = 1
if min <= x and x < max and min <= y and y < max and qrcode.callFunc("isDark", r, (x - min) \ cellSize)
p = 0
end if
' Output 2 characters per pixel, to create full square. 1 character per pixels gives only half width of square.
if p = 1
line += white
else
line += black
end if
end for
for r = 0 to cellSize - 1 step 1
ascii += line + NL
end for
end for
return ascii.left(ascii.len() - 1)
end function
' @private
function toHalfASCII(qrcode as object, padding = 1 as integer) as string
cellSize = 1
if padding < 0
padding = 2
end if
size = (qrcode.moduleCount * cellSize) + (padding * 2)
min = padding
max = size - padding
blocks = {
"██": "█"
"█ ": "▀"
" █": "▄"
" ": " "
}
blocksLastLineNoPadding = {
"██": "▀"
"█ ": "▀"
" █": " "
" ": " "
}
white = "█"
black = " "
ascii = ""
NL = chr(10)
for y = 0 to size - 1 step 2
r1 = fix((y - min) / cellSize)
r2 = fix((y + 1 - min) / cellSize)
for x = 0 to size - 1 step 1
p = white
if min <= x and x < max and min <= y and y < max and qrcode.callFunc("isDark", r1, fix((x - min) / cellSize))
p = black
end if
if min <= x and x < max and min <= y + 1 and y + 1 < max and qrcode.callFunc("isDark", r2, fix((x - min) / cellSize))
p += black
else
p += white
end if
' Output 2 characters per pixel, to create full square. 1 character per pixels gives only half width of square.
if padding < 1 and y + 1 >= max
ascii += blocksLastLineNoPadding[p]
else
ascii += blocks[p]
end if
end for
ascii += NL
end for
if size mod 2 <> 0 and padding > 0
return ascii.left(ascii.len() - size - 1) + String(size, "▀")
end if
return ascii.left(ascii.len() - 1)
end function
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<component name="QRPrinter" extends="Node">
<script type="text/brightscript" uri="QRPrinter.brs" />
<interface>
<!--
Usage:
1. Set `cellSize` to desired value, e.g., 1 means 1 character per module of QRCode.
2. Set `padding` to desired "width" of a "frame" around QRCode rendered using UTF-8 blocks.
3. Set one of:
3.a. `qrcode` to pre-made QRCode object,
3.b. `text` to a value that you want QRPrinter to render QRCode string data for.
4. print `qrstring` or set some Label.text to its value (remember to use monospaced font!)
Setting `text` will also set `qrcode` to a new QRCode object.
Alternative usage:
1. callFunc("toASCII", qrcode, cellSize, padding) to get string of UTF-8 characters.
This will not update any of the fields!
-->
<!-- Set to number of characters to be used per module (or "dot") of QRCode -->
<field id="cellSize" type="integer" value="1" />
<!-- Set to number of characters to be added to create a "frame" around QRCode -->
<!-- For example: 1 means 1 character on the left, one on the right, 1 above and 1 below -->
<field id="padding" type="integer" value="1" />
<!-- Set QRCode object to render to `qrstring` as UTF-8 characters -->
<field id="qrcode" type="node" alwaysNotify="true" />
<!-- Set to a new value to replace `qrcode` and update `qrstring` -->
<field id="text" type="string" value="" />
<!-- Read-only! This will contain QRCode rendered as UTF-8 blocks and spaces -->
<field id="qrstring" type="string" value="" />
<!--
WARNING: It's called `toASCII` even though it uses UTF-8 blocks
because of terms like `ASCII Art`, i.e., "drawing using text only".
-->
<function name="toASCII" /><!-- QRCode, cellSize=1 integer, padding=1 integer -->
</interface>
</component>