Norns: scripting

norns

#181

cheeky comment: bring your norns to work!

non-cheekily, I’d appreciate this being somewhere online as well. Though I’ve definitely made a recent habit of tucking norns in my bag and booting it up in free moments :sunglasses:

edit: perhaps this is already somewhere accessible in one of the github repos? but I’ve not found it yet

@tehn thanks for the response, I got it working using the metro method, but the util.time() seems like a really handy thing to check out!


#182

getting the docs live online somewhere is on my list, thanks for the reminder!


#183

You could actually download the norns master to your computer and read the docs offline, I believe.


#184

How to recognize key up events on norns?

If I want to trigger an action when key 3 on norns is released, I think this

if n==3 and z==1 then
   if n==3 and z==0 then ACTION end
end

is one way to find out if key 3 goes up again. I wonder if there is an alternative way to do this, like a dedicated “key 3 up” event? Thanks in advance!


#185

when z=0 the key is up


#186

I just do

 if n == 3 and z == 0 then
   stuff 
 end

#187

controlspec questions:

  1. Is there a way to use a controlspec outside the confines of a param setup? I’d like to set up a controlspec for use against a standard variable, and do the equivalent of a params:delta call. You know, convenience…

  2. What is the syntax for creating a controlspec that has enumerated values: “fwd”, “bwd”, “updn”? I thought I saw it somewhere, then I didn’t… Or is it just that params can have options, but not controls (where I might just have to use indices)?

Trying to wrap my head around this - but having a ton of fun doing it!

[ddg]


#188

yeah, something like this should work (untested)…

local ControlSpec = require 'controlspec'
local Control = require 'params/control'

local mycontrol = Control.new("mycontrolname", ControlSpec.new(20, 20000, 'exp', 0, 10000, "Hz"))
print(mycontrol:string())
mycontrol:delta(1)
print(mycontrol:string())
mycontrol:delta(-1)
print(mycontrol:string())

Option may be a better alternative if you need to use delta() for enumerated values.

local Option = require 'params/option'

local myoption = Option.new("myoptionname", {"fwd", "bwd", "updn"})
print(myoption:string())
myoption:delta(1)
print(myoption:string())
myoption:delta(-1)
print(myoption:string())

(again, untested as I’m not at my device, but something like above should do)

(also FYI note that i tend to Capitalize OOP classes in Lua such as ControlSpec, Control and Option. this is not required, and you will see in many scripts that classes are not capitalized)


#189

@jah - this was some brilliant help, and also pointed me to the ol’ repo, were digging into params/* provided a lot of fruit!

Thanks!

[ddg]


#190

And my little norns piece (#1!)

You can get it here:


#191

Need more help now:

I’m trying to implement a more robust MIDI implementation, but I’m falling off track with the Midi.add and Midi.remove functions. I see in midi.lua that, when a device is added, it checks to see if Midi.add exists; if so, it calls it. In my code, I can override this as so:

local Midi = require 'midi'

Midi.add = function(dev)
  print("ddg adding " .. dev.name)
end

but if I try to override the remove function, like this:

Midi.remove = function(dev)
  print("ddg removing " .. dev.name)
end

I get an error:

ddg removing mio
lua: /home/we/dust/scripts/ddg/midi_override.lua:16: attempt to index a nil value (local 'dev')
stack traceback:
	/home/we/dust/scripts/ddg/midi_override.lua:16: in function 'midi.remove'
	/home/we/norns/lua/midi.lua:68: in function </home/we/norns/lua/midi.lua:62>

This is a little weird, because it seems to actually work (it says the name of the thing it is removing, using my unique ‘ddg’ syntax…), but then croaks on that same line. This error implies that it is calling the device-specific remove() - which has no arguments - rather than the class-level remove() - which provides the device. How do I override the class function instead?

Any ideas would be greatly appreciated!

[ddg]


#192

What does work is to do this:

function Midi.remove(dev)
  if (dev) then
    print("ddg removing " .. dev.name)
  end
end

But I’m not sure why this remove is being called for both the class-level and device-level remove function. Is it because, in the Midi.new function, the d.remove should actually be set to an empty function so it doesn’t ripple down to the class?

[ddg]


#193

apologies, you’re diving into this just before we’re about to release a big update, followed by a midi/grid study.

see here for the major details: https://github.com/monome/norns/pull/476


#194

Ah, a great read. :popcorn::cup_with_straw::chocolate_bar:

Looking forward to it!

[ddg]


#195

How could I approach making a single pixel, circle around a given point at x radius n times per second? Sort of lacking in my trigonemtric functions knowledge.

EDIT: currently on the right track… Will update when I find success.

EDIT#2: success

have a look

function init()
r = 10
a = 2
x = 64 + (r * math.cos(a))
y = 32 + (r * math.sin(a))
end

function enc(n,d)
  if n == 3 then
    r = r + d/2
    x = 64 + (r * math.cos(a))
    y = 32 + (r * math.sin(a))
    print("r:"..r)
    redraw()
  end
  if n == 2 then
    a = a + d/36
    x = 64 + (r * math.cos(a))
    y = 32 + (r * math.sin(a))
    print("a:"..a)
    redraw()
  end
  redraw()
end

function redraw()
screen.aa(0)
screen.line_width(0.5)
screen.clear()
screen.move(64,32)
screen.pixel(x,y)
screen.close()
screen.stroke()
screen.update()
end

Now I can get started on a patch I’ve been thinking about for a while!


#196

When I am away from norns (God forbid!), what is a recommended editor to continue working on Lua scripts for norns on macOS?


#197

Atom is pretty flexible and looks nice.


#198

I personally like Sublime Text, but nearly all decent text editors will be fine. Atom is a good and free choice.

edit: Plus, they have their own Teletype - what’s not to like? :smiley:


#199

BBEdit is fantastic. It’s commercial software, but has a free/lite mode after a 30-day evaluation.


#200

I use Atom and love it - it can be as non-invasive as you need!

[ddg]