A little help please

Home Forums General Programming A little help please

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #73197
    Anton
    Participant
      • Topics: 1
      • Replies: 1
      • Total: 2

      Hey guys,

      I have the following code inserted in the “Called when the modulator value changes” method of a uiListBox. It sends four Sysex messeges per list item that is clicked.

      
      My_Knobs_1 = function(value)
      
      list = panel:getModulatorByName("list"):getModulatorValue()
      
      cc = -1
      
      if list == 0 then
      	for k = 40,61,7 do
      	string.format('%x', k)
      	cc = cc + 1
      m1 = CtrlrMidiMessage({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, k, cc, 0xf7}) 
      panel:sendMidiMessageNow(m1)
      
      end
      
      elseif list == 1 then
      	cc = 3
      	for k = 40,61,7 do
      	string.format('%x', k)
      	cc = cc + 1
      m1 = CtrlrMidiMessage({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, k, cc, 0xf7}) 
      panel:sendMidiMessageNow(m1)
      
      end
      
      elseif list == 2 then
      	cc = 7
      	for k = 40,61,7 do
      	string.format('%x', k)
      	cc = cc + 1
      m1 = CtrlrMidiMessage({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, k, cc, 0xf7}) 
      panel:sendMidiMessageNow(m1)
      
      end
      end
      end
      

      What I’m wondering…, is there a way to do this that doesn’t require a bunch of “if” statements? The actual list contains 32
      items, but I’ve shortened the code for posting here.

      When item one is clicked it sends:
      F0 47 00 72 31 00 04 01 03 28 00 F7
      F0 47 00 72 31 00 04 01 03 2F 01 F7
      F0 47 00 72 31 00 04 01 03 36 02 F7
      F0 47 00 72 31 00 04 01 03 3D 03 F7

      When item two is clicked it sends:
      F0 47 00 72 31 00 04 01 03 28 04 F7
      F0 47 00 72 31 00 04 01 03 2F 05 F7
      F0 47 00 72 31 00 04 01 03 36 06 F7
      F0 47 00 72 31 00 04 01 03 3D 07 F7

      etc…

      The code works, it’s just I’d rather not use so many “ifs”, if possible?

      Any help is greatly appreciated.

      Thanks

      • This topic was modified 6 years, 6 months ago by Anton.
      #73199
      human fly
      Participant
        • Topics: 124
        • Replies: 1070
        • Total: 1194
        • ★★★★

        if it works? …
        … why worry? 😀
        looks ok.

        you could have some table if refers to(?)
        but it might not end up any shorter.

        #73231
        dnaldoog
        Participant
          • Topics: 4
          • Replies: 480
          • Total: 484
          • ★★

          Hi there Anton,

          If ‘list’ can have values ranging from 0-31, then I think this would work:

          
          My_Knobs_1 = function(value)
          list = panel:getModulatorByName("list"):getModulatorValue()
          local multiplier = list * 4
          local loopEnd = multiplier + 3
          k=40
          for i=multiplier,loopEnd  do
          m1 = panel:sendMidiMessageNow(CtrlrMidiMessage({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, k, i, 0xf7}))
          k=k+7
          end
          

          Note: in the attached panel I mistakenly left in the redundant i=i+2

          Regards,

          Attachments:
          You must be logged in to view attached files.
          #73234
          goodweather
          Participant
            • Topics: 45
            • Replies: 550
            • Total: 595
            • ★★★

            Hi, saw that question the other day and also wanted to answer it.
            Here is a simpler code I think…

            --
            -- Called when a modulator value changes
            -- @mod   http://ctrlr.org/api/class_ctrlr_modulator.html
            -- @value    new numeric value of the modulator
            --
            Combo_OnChange = function(--[[ CtrlrModulator --]] mod, --[[ number --]] value, --[[ number --]] source)
            
            	lblSysex = panel:getModulatorByName("lblSysex")
            	modValues = panel:getModulatorByName("cbValues")
            
            	sText =""
            	for i=0,3 do
            		mbTemp= MemoryBlock ({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, 40+i*7, value*4+i, 0xf7})
            		panel:sendMidiMessageNow (CtrlrMidiMessage(mbTemp))
            		sText = sText..mbTemp:toHexString(1).."\n"
            	end
            	lblSysex:getComponent():setPropertyString("uiLabelText", sText)
            
            end

            and the panel from dnaldoog adapted 🙂

            Attachments:
            You must be logged in to view attached files.
            #73237
            dnaldoog
            Participant
              • Topics: 4
              • Replies: 480
              • Total: 484
              • ★★

              Wow – that’s better and even more concise!

              #73240
              Anton
              Participant
                • Topics: 1
                • Replies: 1
                • Total: 2

                @ dnaldoog & goodweather,

                Both scripts work as expected, and I learned a thing or two. 😀

                Thank you both, very much appreciated!

                #73275
                goodweather
                Participant
                  • Topics: 45
                  • Replies: 550
                  • Total: 595
                  • ★★★

                  cool 😉

                Viewing 7 posts - 1 through 7 (of 7 total)
                • The forum ‘Programming’ is closed to new topics and replies.
                There is currently 0 users and 98 guests online
                No users are currently active
                Forum Statistics
                Threads: 2,495, Posts: 17,374, Members: 77,605
                Most users ever online was 12 on January 22, 2019 3:47 pm
                Ctrlr