Skip to content

Instantly share code, notes, and snippets.

@ingenieroariel
Last active September 9, 2024 15:18
Show Gist options
  • Save ingenieroariel/9e849f7c1ff1cd32c1cdf130eacc8fb7 to your computer and use it in GitHub Desktop.
Save ingenieroariel/9e849f7c1ff1cd32c1cdf130eacc8fb7 to your computer and use it in GitHub Desktop.
Sample Nix Flake Written in Fennel
(global analogtbl {})
(global digitaltbl {})
(global translatetbl {})
(global statustbl {})
(global touchtbl {})
(global analogdata {})
(set-forcibly! drawline
(fn [text size]
(render-text (.. "\\f," (tostring size) " " text))))
(fn reposition []
(let [w3 (/ VRESW 3)
h2 (/ VRESH 2)]
(when (not (valid-vid analabel)) (lua "return "))
(move-image analabel 0 0)
(move-image digilabel w3 0)
(move-image translabel (+ w3 w3) 0)
(move-image touchlabel 0 h2)
(move-image statuslabel (+ w3 w3) h2)))
(set-forcibly! eventtest (fn []
(global symtable
((system-load :builtin/keyboard.lua)))
(global analabel (drawline "\\bAnalog" 12))
(global digilabel (drawline "\\bDigital" 12))
(global touchlabel (drawline "\\bTouch" 12))
(global statuslabel (drawline "\\bStatus" 12))
(global translabel (drawline "\\bTranslated" 12))
(reposition)
(inputanalog-toggle 1)
(global tc (null-surface 1 1))
(show-image [statuslabel
analabel
digilabel
translabel
touchlabel])
(global restbl (inputanalog-query))
(print (length restbl) "entries found")
(each [k v (ipairs restbl)]
(print "-- new device -- ")
(each [i j (pairs v)] (print i j)))
(local vid
(target-alloc :eventinject
(fn [source status]
(if (= status.kind :input)
(eventtest-input status)
(print "non-IO event on eventinject connection point:"
status.kind)))))
(target-flags vid TARGET_ALLOWINPUT)
(inputanalog-toggle 1)))
(set-forcibly! round
(fn [inn dec]
(math.floor (/ (* inn (^ 10 dec)) (^ 10 dec)))))
(set-forcibly! touch-str (fn [iotbl]
(table.insert touchtbl
(string.format "dev(%d:%d) @ %d, %d, press: %.2f, size: %.2f, active: %s"
iotbl.devid iotbl.subid
iotbl.x iotbl.y
iotbl.pressure
iotbl.size
(or (and iotbl.active
:yes)
:no)))
(when (> (length touchtbl) 10)
(table.remove touchtbl 1))
(local line (table.concat touchtbl "\\r\\n"))
(when touchimg (delete-image touchimg))
(global touchimg (drawline line 10))
(link-image touchimg touchlabel)
(nudge-image touchimg 0 20)
(show-image touchimg)))
(set-forcibly! digital-str
(fn [iotbl]
(table.insert digitaltbl
(string.format "dev(%d:%d) %s" iotbl.devid
iotbl.subid
(or (and iotbl.active :press)
:release)))
(when (> (length digitaltbl) 10) (table.remove digitaltbl 1))
(local line (table.concat digitaltbl "\\r\\n"))
(when digitalimg (delete-image digitalimg))
(global digitalimg (drawline line 10))
(link-image digitalimg digilabel)
(nudge-image digitalimg 0 20)
(show-image digitalimg)))
(set-forcibly! translate-str
(fn [iotbl]
(let [label (symtable.tolabel iotbl.keysym)]
(table.insert translatetbl
(string.format "dev(%d:%d)%d[%s] => %s, %s, %s, %s"
iotbl.devid iotbl.subid
iotbl.number
(table.concat (decode-modifiers iotbl.modifiers)
",")
iotbl.keysym
(or (and iotbl.active :press)
:release)
(or label :_nil) iotbl.utf8))
(when (> (length translatetbl) 10)
(table.remove translatetbl 1))
(local tbl [""])
(each [k v (ipairs translatetbl)] (table.insert tbl v)
(table.insert tbl "\\r\\n"))
(when translateimg (delete-image translateimg))
(global translateimg (render-text tbl))
(link-image translateimg translabel)
(nudge-image translateimg 0 20)
(show-image translateimg))))
(set-forcibly! analog-str
(fn [intbl]
(string.format "%d:%.2f/%.2f avg: %.2f" intbl.count
(round intbl.min 2) (round intbl.max 2)
(round intbl.avg 2))))
(global tick-counter 500)
(set-forcibly! eventtest-clock-pulse
(fn [stamp delta]
(when analogimg (delete-image analogimg))
(global line "")
(each [ak ad (pairs analogdata)]
(global workline (.. "\\n\\rDevice(" ak "):\\n\\r\\t"))
(each [id iv (pairs ad)]
(global workline
(.. workline " axis: " id " # " (analog-str iv)
"\\n\\r\\t")))
(global line (.. line workline "\\r\\n")))
(global analogimg (drawline line 10))
(link-image analogimg analabel)
(nudge-image analogimg 0 20)
(show-image analogimg)
(global tick-counter (- tick-counter 1))
(if (= tick-counter 0) (shutdown :timeout)
(do
(delete-image tc)
(global tc
(render-text (.. "Shutdown in "
(tostring tick-counter))))
(show-image tc)
(move-image tc 0 (- VRESH 20))))))
(set-forcibly! eventtest-input
(fn [iotbl]
(if iotbl.digital
(do
(global tick-counter 500)
(if iotbl.translated (translate-str iotbl)
(digital-str iotbl)))
iotbl.touch (touch-str iotbl) iotbl.analog
(do
(var anatbl {})
(when (= (. analogdata iotbl.devid) nil)
(tset analogdata iotbl.devid {}))
(when (= (. (. analogdata iotbl.devid) iotbl.subid) nil)
(tset (. analogdata iotbl.devid) iotbl.subid anatbl)
(set anatbl.count 0)
(set anatbl.min 65535)
(set anatbl.max 0)
(set anatbl.avg 1)
(set anatbl.samples {}))
(set anatbl (. (. analogdata iotbl.devid) iotbl.subid))
(set anatbl.count (+ anatbl.count 1))
(table.insert anatbl.samples (. iotbl.samples 1))
(when (< (. iotbl.samples 1) anatbl.min)
(set anatbl.min (. iotbl.samples 1)))
(when (> (. iotbl.samples 1) anatbl.max)
(set anatbl.max (. iotbl.samples 1)))
(set anatbl.avg (/ (+ anatbl.avg (. iotbl.samples 1)) 2))
(when (> (length anatbl.samples) 10)
(table.remove anatbl.samples 1))
(set anatbl.match tbl))
(= iotbl.kind :status)
(warning (string.format "status(%d) - %s, %s, %s"
iotbl.devid iotbl.devkind
iotbl.label iotbl.action)))))
(set-forcibly! eventtest-display-state
(fn [status] (resize-video-canvas VRESW VRESH) (reposition)))
(local flake {})
(fn flake.input [name url]
`{:name ,name :url ,url})
(fn flake.output [system pkgs]
`{:system ,system :packages ,pkgs})
(macro define-flake [& body]
(let [input-sym (gensym :input)
output-sym (gensym :output)
name-sym (gensym :name)
url-sym (gensym :url)
pkg-sym (gensym :pkg)
self-sym (gensym :self)
inputs-sym (gensym :inputs)]
`{:description ,(. body 1)
:inputs (collect [,input-sym ,name-sym ,url-sym (ipairs ,(. body 2))]
(values (. ,input-sym :name) (. ,input-sym :url)))
:outputs (fn [,self-sym ,inputs-sym]
(collect [,output-sym ,name-sym ,pkg-sym (ipairs ,(. body 3))]
(values (. ,output-sym :system)
{:packages (. ,output-sym :packages)})))}))
(fn generate-nix [flake-def]
(.. "{
description = \"" (. flake-def :description) "\";
inputs = {
"
(table.concat
(icollect [name url (pairs (. flake-def :inputs))]
(.. " " name " = " url ";"))
"\n")
"
};
outputs = { self, nixpkgs }: {
"
(table.concat
(icollect [system output (pairs ((. flake-def :outputs) {} {:nixpkgs "nixpkgs"}))]
(.. " " system " = {
packages = {
"
(table.concat
(icollect [name pkg (pairs (. output :packages))]
(.. " " name " = nixpkgs." pkg ";"))
"\n")
"
};
};"))
"\n")
"
};
}"))
;; Usage
(local my-flake
(define-flake
"A sample flake written in Fennel"
[(flake.input :nixpkgs "github:NixOS/nixpkgs/nixos-unstable")]
[(flake.output "x86_64-linux"
{:hello "hello"
:cowsay "cowsay"})
(flake.output "aarch64-darwin"
{:hello "hello"})]))
(print (generate-nix my-flake))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment