CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Need help writing if statements for multiple OR'ed exprs

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
picj1984



Joined: 01 Mar 2010
Posts: 73

View user's profile Send private message

Need help writing if statements for multiple OR'ed exprs
PostPosted: Fri Jul 30, 2010 4:56 pm     Reply with quote

Hi everyone,

This routine I wrote works but it's taking a bunch of badly needed ROM. I'm counting midi beat clock ticks to change a sequence. When "whole notes" are selected it waits 96 ticks... when "half notes" are selected it waits 48 ticks.

The issue here is that I can only allow the program to change from "whole note mode" to "half note mode" if it's on a tick number that's allowable for that mode. I think the code should be reasonable self explanatory. Basically just need a way to compress this.

Code:

      if(tick_max == whole)
      {
         if(tick_measure == 96)
         tick_change_ok = true;
      }
      else if(tick_max == half)
      {
         if(tick_measure == 96 || tick_measure == 48)
         tick_change_ok = true;
      }
      else if(tick_max == quarter)
      {
         if(tick_measure == 96 || tick_measure == 72 || tick_measure == 48 || tick_measure == 24)
         tick_change_ok = true;
      }
      else if(tick_max == triplet)
      {
         if(tick_measure == 96 || tick_measure == 80 || tick_measure == 64 || tick_measure == 48 || tick_measure == 32 || tick_measure == 16)
         tick_change_ok = true;
      }
      else if(tick_max == eighth)
      {
         if(tick_measure == 96 || tick_measure == 84 || tick_measure == 72 || tick_measure == 60 || tick_measure == 48 || tick_measure == 36 || tick_measure == 24 || tick_measure == 12)
         tick_change_ok = true;
      }
      else if(tick_max == sextuplet)
      {
         if(tick_measure == 96 || tick_measure == 88 || tick_measure == 80 || tick_measure == 72 || tick_measure == 64 || tick_measure == 56 || tick_measure == 48 || tick_measure == 40 || tick_measure == 32 || tick_measure == 24 || tick_measure == 16 || tick_measure == 8)
         tick_change_ok = true;
      }
      else if(tick_max == sixteenth)
      {
         if(tick_measure == 96 || tick_measure == 90 || tick_measure == 84 || tick_measure == 78 || tick_measure == 72 || tick_measure == 66 || tick_measure == 60 || tick_measure == 54 || tick_measure == 48 || tick_measure == 42 || tick_measure == 36 || tick_measure == 30 || tick_measure == 24 || tick_measure == 18 || tick_measure == 12 || tick_measure == 6)
         tick_change_ok = true;
      }
      else if(tick_max == thirtytwo)
      {
         if(tick_measure == 96 || tick_measure == 93 || tick_measure == 90 || tick_measure == 87 || tick_measure == 84 || tick_measure == 81 || tick_measure == 78 || tick_measure == 75 || tick_measure == 72 || tick_measure == 69 || tick_measure == 66 || tick_measure == 63 || tick_measure == 60 || tick_measure == 57 || tick_measure == 54 || tick_measure == 51 || tick_measure == 48 || tick_measure == 45 || tick_measure == 42 || tick_measure == 39 || tick_measure == 36 || tick_measure == 33 || tick_measure == 30 || tick_measure == 27 || tick_measure == 24 || tick_measure == 21 || tick_measure == 18 || tick_measure == 15 || tick_measure == 12 || tick_measure == 6 || tick_measure == 3)
         tick_change_ok = true;
      }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 30, 2010 7:12 pm     Reply with quote

It would be nice if you would post this code as part of a test program.
It doesn't have to be much, but if you could add a standard framework
with the #include for the PIC, #fuses, #use delay, main(), and all
variable and constant declarations, it would make it a lot easier for us.
It should be copy-and-paste compilable, if we drop it into an MPLAB project.

Also post your compiler version.
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Sat Jul 31, 2010 2:57 am     Reply with quote

Consider writing a 'tick_compare' function.
Something like:
Code:

int1 tick_compare(int8 to_check,int8 vals[],int8 number) {
   int8 ctr;
   int1 rval=FALSE;
   for (ctr=0;ctr<number;ctr++){
      if (vals[ctr]==to_check) {
          rval=TRUE;
          break;
      }
   }
   return rval;
}
//Then have your comparison values as arrays
int8 sixteentests[] = {96,90,84,78,72,66,60,54,48,42,36,30,24,18,12,6};
//Hope I have this right....

//Then you can call
if (tick_compare(tick_measure,sixteentests,sizeof(sixteentests))


Even more to the point though, most of the number sequences seem to be contained in the ones below (the exception here is the triplet, and twelfth), so in fact the 'sixteen' test, could be done with the 'thirtytwo' table, just advancing the counter by two, and the eighth by advancing the counter by four, etc...
So if you used just one table, and handed the number to advance, rather than the table size, the storage space would 'leap' downwards....

Best Wishes
RoGuE_StreaK



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

PostPosted: Sat Jul 31, 2010 4:05 am     Reply with quote

I was going to suggest using modulus instead of all of those "OR" lines, but a bit of research seems to indicate that, while neater, it'd probably take a lot longer to process.

eg.
Code:

      else if(tick_max == thirtytwo)
      {
         if(tick_measure % 3 == 0) tick_change_ok = true;
      }


But hey, might be worth a try to see how it effects things. You might have enough free time, and it might possibly bring the filesize down?

DISCLAIMER:I have not tested this in any way, shape, or form.
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Sat Jul 31, 2010 4:25 am     Reply with quote

A good point.
In fact for several of the entries, the right result can be given by simple logic. For example, 'twelfth', would be:
if ((tick_measure & 0xF8)==tick_measure)

Best Wishes
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Sat Jul 31, 2010 10:18 am     Reply with quote

Looking at the posted tables, some values seem to be missing. For example, '32', should I'd have thought, include 9.
Surely also, looking at the progression, there should be a 'sixth' between the quarter and eighth, and a triplet down between half and a quarter?.
Now, with that said, using mod _provided you encapsulate the function_, would save space, but at a cost of speed. However assuming the arriving value is an int8, the cost would not be that bad. But it might actually be quicker to do a repeated subtraction (a quick 'back of hand' calculation suggests this will be slower for the 32 case only...).
Ideally, if the defines for tick_max are changed to be the division factor required, so 'whole' = 96, 'half'=48 etc., to 'thirtysecond'=3, then the code could become something like:
Code:

int1 tick_compare(int8 value,int8 factor) {
    while (value>=factor) {
        value-=factor;
        if (value=0) return TRUE;
    }
    return FALSE;
}

The line:

if (tick_compare(tick_measure,tick_max))

Would answer all the questions.....

The same would apply, using mod, with it being much easier if tick_max was the factor required.

Best Wishes
picj1984



Joined: 01 Mar 2010
Posts: 73

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 11:20 am     Reply with quote

Seriously thanks so much you guys. I didn't even know modulus existed. You cut my rom usage by 7% and I'm already thinking of some easy ways to free up some more if I needed it.

I ended up going with this function and it's working flawlessly. Thanks again so much!!!

Code:


short tick_compare(unsigned char value, unsigned char factor) {

   if(value % factor == 0)
        return TRUE;
   else
   return FALSE;
}

picj1984



Joined: 01 Mar 2010
Posts: 73

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 11:22 am     Reply with quote

In the code itself it's of course just

Code:


tick_change_ok = tick_compare(tick_measure,tick_max);

picj1984



Joined: 01 Mar 2010
Posts: 73

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 11:26 am     Reply with quote

You were correct Ttelmah my tables had some errors in them. At least modulus will idiot proof my code Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group