Hi.
I am trying to write a couple of algorithms that take in a 16 bit number, use a mask to move along the bits and if the mask & number = 1, do something.
The first thing to point out is this is going on a microcontroller. For that reason i am not looping through the entire 16 bit value in one go, I only do one at a time, so the program can return to whatever else it was doing in the mean time, and only do the next "bit" when a timer ticks so I need to save the last state of the changing variables between function calls.
I only however need the 12 Most significant bits, the 4 least significant bits I am using for something else, so my boundaries of the mask are 0x8000 and 0x10. One of my algorithms goes from left to right arpUp, and one goes from right to left:
Code:
void Arpeggiator::arpUp(unsigned int chord, int numOctaves) {
if (mask == 0x10) {
mask = 0x8000;
arpPosition = leftBoundary;
}
if (mask & chord) {
arpeggioPlay(arpPosition);
}
mask >>= 1;
arpPosition++;
}
So this checks if it is at the right boundary. If so, move the mask back to the left. and move the variable also.
The problem with that is it doesnt do "arpPlay()" when mask == 0x10 and (mask & chord)
I "could" do if (mask < 0x10) but I cant do the equivelent check for the arpDown function (if (mask > 0x8000)) because thats the end of the memory for the 16 bit value so I have:
Code:
void Arpeggiator::arpDown(unsigned int chord, int numOctaves) {
arpPosition--;
if (arpPosition < startNote){
arpPosition = startNote + 12;
}
if (mask & chord) {
arpeggioPlay(arpPosition);
}
maskDown <<= 1;
if (mask == 0x8000) {
if (mask & chord) {
arpeggioPlay(60);
}
mask = 0x10;
}
}
This ones a bit of a mess, because Ive been trying a few things. It sort of works, but its the end conditions that cause me problems
I also dont like hardcoding in
Code:
if (mask & chord) {
arpeggioPlay(60);
}
when the check to see if mask == 0x8000, seems like a hack.
im also trying to use mask and arpPosition as member variables to save on memory, but that means that depending on which of the above two are called, there needs to be some initial setup.
Thanks guys