From af1f1a2b7564ac367d283b8a28f607012e7bc5f7 Mon Sep 17 00:00:00 2001 From: Sergey Karmanov Date: Thu, 6 Apr 2023 20:38:28 +0300 Subject: [PATCH] Upload script --- src/shiftOut.lua | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/shiftOut.lua diff --git a/src/shiftOut.lua b/src/shiftOut.lua new file mode 100644 index 0000000..79c9217 --- /dev/null +++ b/src/shiftOut.lua @@ -0,0 +1,61 @@ +shiftOut = {} + +local GPIO = require('periphery').GPIO +local bit = require("bit") + +LSBFIRST = 1 +MSBFIRST = 0 + +function shiftOut:new(latchPin, dataPin, clockPin) + + local obj = {} + obj.latchGPIO = GPIO(latchPin, "out") + obj.dataGPIO = GPIO(dataPin, "out") + obj.clockGPIO = GPIO(clockPin, "out") + + obj.latchGPIO:write(false) + obj.dataGPIO:write(false) + obj.clockGPIO:write(false) + + function obj:shiftOut(value, bitOrder) + if bitOrder == nil then + bitOrder = MSBFIRST + end + + if type(value) == "number" then -- decimal + for i = 1, 8 do + if bitOrder == LSBFIRST then + self.dataGPIO:write(bit.band(value, 1)) + value = bit.rshift(value, 1) + else + self.dataGPIO:write(bit.band(value, 128) ~= 0) + value = bit.lshift(value, 1) + end + + self.clockGPIO:write(true) + self.clockGPIO:write(false) + end + elseif type(value) == "string" then -- string bits (Ex: "10100001") + if bitOrder == LSBFIRST then + value = string.reverse(value) + end + + for i = 1, 8 do + self.dataGPIO:write(string.sub(value, i, i) == "1") + + self.clockGPIO:write(true) + self.clockGPIO:write(false) + end + + end + + self.latchGPIO:write(true) + self.latchGPIO:write(false) + end + + setmetatable(obj, self) + self.__index = self; + return obj +end + +return shiftOut