Skip to content

Instantly share code, notes, and snippets.

@poorandunlucky
Created November 16, 2019 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poorandunlucky/7f0e29cf694ed37bb9e569752412ac09 to your computer and use it in GitHub Desktop.
Save poorandunlucky/7f0e29cf694ed37bb9e569752412ac09 to your computer and use it in GitHub Desktop.
NodeMCU 9 > 9 = true
-- -------------------------------------------------------------------
-- FILE: GIST/bcdrtc.lua
--
-- PURPOSE: NodeMCU error demo.
--
-- AUTHOR: Patrick Dorion <dorionpatrick@outlook.com>
--
-- DATE: November 16, 2019.
--
-- LICENSE: You need permission to use, or reproduce this code.
-- -------------------------------------------------------------------
varlib = {}
function varlib.zeropad ( num, size )
--[[
Zero-pads a binary.
num string The binary to convert.
size number The chunk size to pad for.
Returns: string Zero-padded binary.
]]--
num = tostring ( num )
if ( size == nil ) then size = 4 end
while ( #num ) % size ~= 0 do
num = "0" .. num
end
return num
end -- End of function.
function varlib.dec2base ( int, base )
--[[
Converts a decimal integer to any other base.
int number Integer.
base number The base to convert to.
Returns: string Converted value.
This code is mostly from from Peter Prade <prade@phenomic.net>.
]]--
local digits = { }
for i = 0, 9 do
digits[i] = string.char ( string.byte ( '0' ) + i )
end
for i = 10, 36 do
digits[i] = string.char ( string.byte ( 'A' ) + i - 10 )
end
function convert ( number, base )
local string = ''
repeat
local remainder = number % base
string = digits[remainder] .. string
number = ( number - remainder ) / base
until number == 0
return string
end
return convert ( int, base )
end -- End of function.
function dec2bcd ( dec )
--[[
-- Returns a binary-coded decimal.
--
-- It does that this way, and this that way. Be careful.
--
-- dec number Number to be converted.
--
-- Returns: string Binary-coded decimal.
--
-- Depends: module varlib
]]--
--require ( 'varlib' )
local bcd
dec = tostring ( dec )
for i = 1, #dec, 1 do
if ( i == 1 ) then
bcd = varlib.zeropad ( varlib.dec2base ( string.sub ( dec, i, i ), 2 ), 4 )
else
bcd = bcd .. varlib.zeropad ( varlib.dec2base ( string.sub ( dec, i, i ), 2 ), 4 )
end
end
return bcd
end -- End of function.
function bin2dec ( bin )
local dec = 0
bin = varlib.zeropad ( tostring ( bin ), 4 )
for i = 1, #bin, 1 do
if ( string.sub ( bin, i, i ) == '1' ) then
dec = dec + 2 ^ (#bin - i)
end
end
return dec
end
function bcd2dec ( bcd )
local dec, digit = '', ''
bcd = varlib.zeropad ( bcd )
for i = #bcd, 1, -4 do
digit = bin2dec ( string.sub ( bcd, i-3, i ) )
print ( 'digit = ' .. digit )
if ( tonumber ( digit) > 9 ) then
print ( "Error: BCD nibble > 9." )
return 1
else
dec = digit .. dec
end
end
return tonumber ( dec )
end
print ( bcd2dec ( '1001' ) )
-- Creates error 'BCD(1001) > 9' on my board. Runs without error on Lua 5.1.5 on computer. Board running build September 19, 2019 powered by Lua 5.1.4 on SDK 3.0.1-dev(fce080e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment