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

BlockSlice - Slice manager (aka state machine)

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
mbradley



Joined: 11 Jul 2009
Posts: 118
Location: California, USA

View user's profile Send private message Visit poster's website

BlockSlice - Slice manager (aka state machine)
PostPosted: Sun Jun 14, 2015 3:21 pm     Reply with quote

Hello everyone,

I have posted in a little while, but I wanted to share a recent lib I created.

Its a way to have only a part of your function execute every time your function is called. The parts (slices) in your function will execute in order.

For me, I had some background math that needed to be done, that was rather long. I noticed I had a bunch of free time while doing output pulses, and input captures, so I decided I would execute parts of the code in these areas, and it started to look really ugly.

So now, when ever I know I have free cycles, I make a call to my function, and I know it won't hang up because only a portion of it runs at a time.


Here is the code, I hope you enjoy it.

--mike

blockslice.h
Code:

/*


Example usage:
#include "blockslice.h"


#define LED_GREEN    PIN_B5
#define LED_YELLOW   PIN_B4
#define LED_RED      PIN_B3

mDefineBlockSlice(2); // number of block slices to define

void procStatus(void)
{
   mBlockSliceStart(0); // this is used to track which state machine
   mBlockSlice() { output_high(LED_YELLOW); }
   mBlockSlice() { output_low(LED_YELLOW); }
   mBlockSliceEnd();
}

void procLights(void)
{
   mBlockSliceStart(1); // this is used to track which state machine
   mBlockSlice() { output_high(LED_GREEN); }
   mBlockSlice() { output_low(LED_GREEN); }
   mBlockSlice() { output_high(LED_RED); }
   mBlockSlice() { output_low(LED_RED); }
   mBlockSliceEnd();
}


void main()
{
   output_high(LED_GREEN);

   // initialize the state machines
   mBlockSliceIni(0);
   mBlockSliceIni(1);

   while(TRUE)
     {
     procStatus();
     procLights();
     delay_ms(500);
     }
 
}


*/



// yes, I know there is no ';' at the end of this line, its on purpose, see translate below
#define mDefineBlockSlice(cnt) unsigned int8 __bsActive = 0; unsigned int8 __bsNdx[cnt], __bsState[cnt]
/*
   mDefineBlockSlice(1);
   
   translates to:
   
   unsigned int8 __bsActive = 0; unsigned int8 __bsNdx[1], __bsState[1];
*/

#define mBlockSliceIni(ndx)   __bsState[ndx] = 0;

#define mBlockSliceStart(ndx) __bsActive = ndx; __bsNdx[ndx] = 255;
/*
   mBlockSliceStart(0);
   
   translates to:
   
   __bsActive = 0; __bsNdx[0] = 255;
*/


#define mBlockSlice()  __bsNdx[__bsActive]++; if ( __bsState[__bsActive] == __bsNdx[__bsActive] )
/*
   mBlockSlice()
     {
     output_high(LED_ERR);
     delay_ms(500);
     }
   
   translates to:
   
   __bsNdx[__bsActive]++; if ( __bsState[__bsActive] == __bsNdx[__bsActive]) )   
     {
     output_high(LED_ERR);
     delay_ms(500);
     }
*/


#define mBlockSliceEnd() __bsState[__bsActive]++; if ( __bsState[__bsActive] > __bsNdx[__bsActive] ) { __bsState[__bsActive] = 0; }
/*
   mBlockSliceEnd();
   
   translates to:
   
   __bsState[__bsActive]++; if ( __bsState[__bsActive] > __bsNdx[__bsActive] ) { __bsState[__bsActive] = 0; }
*/



_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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