Home › Forums › General › Programming › [resolved] mask to 7 bits
- This topic has 7 replies, 3 voices, and was last updated 10 years, 7 months ago by atom.
-
AuthorPosts
-
August 26, 2013 at 7:06 pm #12466
I’m working with a checksum value whos requirements are:
Sum of all data bytes truncated to 7 bits. The addition is done
in 8 bit format, the result is masked to 7 bits (00h to 7Fh).
How do i mask a value to 7 bits in Lua? I did some if statements to see how large the sum value is then subtracted 128 or 256 from it to get the value lower than 127 but I’m wondering if there’s a more elegant way with Lua?
- This topic was modified 10 years, 8 months ago by msepsis.
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
August 26, 2013 at 8:26 pm #12476(MyData%127) ?
see the topic Math round() by me
you can do a moduloAugust 27, 2013 at 8:56 pm #12527Thanks Zeoka, just to clarify the simple answer in clear language:
The character % will round to the proceeding value.
myValue % 128
will round “myValue” to 7 bits.
It’s that simple, no other reference or links needed 🙂
- This reply was modified 10 years, 8 months ago by msepsis.
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
August 28, 2013 at 12:21 am #12543Module % gives you the remainder http://en.wikipedia.org/wiki/Modulo_operation
If you want only the last 7 bits of a 16bit value you can use BigInteger http://www.juce.com/juce/api/classBigInteger.html
myValue = 12300 bi = BigInteger(myValue) last7Bits = bi:getBitRangeAsInt(0,7) -- get 7 bits from bit 0
BUT if you want to scale a large range of numbers, like 0 – 13000 you need a more complex operation (downsample?), something like
myValue = 456 myLargeMax = 13000 v = (myValue / myLargeMax) * 127 console (string.format ("v=%d",v)) -- v = 4, 13000 will be 127 etc.
here “v” is the scaled down value
OR
myMax = 14000 -- this is our MAX x = 456 -- we want to scale this v = (x / myMax) * 127 -- this is x scaled down bi = BigInteger(v) -- put v into BITS console (bi:toString(2,8)) -- print the bits on the console, v i actualy 4.1365714285714 -- 00000100 this will give you 8 bits of the calculated value, there are more, sine all numbers in Lua are of type "double" so there is more bits. You can safely put the result of bi:getBitRangeAsInt(0,7) as a byte in a MIDI message
Or even better
myMax = 14000 x = 456 v = (x / myMax) * 127 r,q = math.modf(v) bi = BigInteger(v) console ("v="..v) console ("r="..r) console ("q="..q) v=4.1365714285714 r=4 q=0.13657142857143
August 28, 2013 at 11:10 pm #12606Very complete demo. useful
Thank youSeptember 30, 2013 at 7:03 pm #13799I’m not sure about %127 modulo This is not working with the RA
I tried %128 and this working
%127 is working until checksum 7e and after this makes chksum errors
-- -- Called when a modulator value changes -- @mod http://ctrlr.org/api/class_ctrlr_modulator.html -- @value new numeric value of the modulator -- tempo2 = function(mod, value) progtmp = panel:getModulatorByName("Program Tempo"):getModulatorValue() prgtxt = panel:getComponent("text PRG Tempo") prgval = progtmp / 10 prchks1 = (83+(progtmp / 128))%128 prchks2 = (84+(progtmp%128))%128 panel:sendMidiMessageNow(CtrlrMidiMessage{0xF0,0x3E,0x11,0x00,0x21,0x20,0x00,0x00,0x12,(progtmp / 128),prchks1,0xF7}) panel:sendMidiMessageNow(CtrlrMidiMessage{0xF0,0x3E,0x11,0x00,0x21,0x20,0x00,0x00,0x13,(progtmp%128),prchks2,0xF7}) if progtmp == 0 then prgtxt:setComponentText (string.format ( "Global")) elseif progtmp == 1 then prgtxt:setComponentText (string.format ( "Extern")) elseif progtmp >= 2 and progtmp <= 99 then prgtxt:setComponentText (string.format ( "00%.1f Bpm",prgval)) elseif progtmp >= 100 and progtmp <= 999 then prgtxt:setComponentText (string.format ( "0%.1f Bpm",prgval)) elseif progtmp >= 1000 then prgtxt:setComponentText (string.format ( "%.1f Bpm",prgval)) end end
Can you give a feedback please Msepsis if you encounter errors with your chksum stuff ?
October 1, 2013 at 12:35 am #13810Modulo operation just checks the result of the division of one number by the other (explained in detail) http://en.wikipedia.org/wiki/Modulo_operation) i doubt it will work as a checksuming mechanism for anything. Find out exactly what you need.
October 1, 2013 at 12:40 am #13811Here is a few modulot operation results to better illustrate what this does
0 % 128 = 0 16 % 128 = 16 32 % 128 = 32 48 % 128 = 48 64 % 128 = 64 80 % 128 = 80 96 % 128 = 96 112 % 128 = 112 128 % 128 = 0 144 % 128 = 16 160 % 128 = 32 176 % 128 = 48 192 % 128 = 64 208 % 128 = 80 224 % 128 = 96 240 % 128 = 112 256 % 128 = 0 272 % 128 = 16 288 % 128 = 32 304 % 128 = 48 320 % 128 = 64 336 % 128 = 80 352 % 128 = 96 368 % 128 = 112 384 % 128 = 0 400 % 128 = 16 416 % 128 = 32 432 % 128 = 48 448 % 128 = 64 464 % 128 = 80 480 % 128 = 96 496 % 128 = 112
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.