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 CCS Technical Support

Using[pseudo] Classes in CCS

 
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

Using[pseudo] Classes in CCS
PostPosted: Fri Jan 29, 2010 10:50 pm     Reply with quote

Hello every one, I hope you enjoy this. If you are like me, you have that nicely massaged piece of code that is in your arsenal, and now you need two or more instances of it.

This happened to me tonight, so I ended up doing a find and replaced to make 4 copies of it with unique function names and variables names, I hated it, it was sloppy.

So I wrote this tonight to solve this issue, it allows me to include a file as a class (file needs to be modified for this use)

I hope you can use this as much as I now am.

Here is an example of how it can be used in your source:

File: main.c
Code:

#include "class.h"

#define class clsInstance1
  #include "clsBuffer.c"
  #undef class

#define class clsInstance2
  #include "clsBuffer.c"
  #undef class

void main()
{
   clsInstance1_clearBuffer();
   clsInstance2_clearBuffer();
}


File: clsBuffer.c
Code:

char  clsVAR(aryBuf[31]);

void clsFN(clearBuffer)(void)
{
   clsVAR(aryBuf[0]) = 0x00;
}


And here you go,
File: class.h
Code:

/*
          file: class.h
   description: Enables use of [pseudo] classes. (type of C++ / OOP)
      compiler: CCS C
   written by : Michael Bradley

Usage:
       You must define 'class' as the name of your instance,
       and undefine it after the include

       #define class clsInstance1
         #include "some_file.c"
         #undef class

       #define class clsInstance2
         #include "some_file.c"
         #undef class


       To access a function or variable in the above class;
       
       clsInstance1_coolFunction(); // call a function in the class
       clsInstance1_flagOK = 1;     // set a variable in the class


   In each included class file, all functions must be prefaced
   with clsFN(), and all global variables with clsVAR()


   Example of included file:

            int8 clsVAR(flagOK);
            int8 clsVAR(flagRunning);
           
            void clsFN(coolFunction)(void)
            {
               clsVAR(flagOK) = 1;
            }
           
            void clsFN(setRunFlag)(int8 status)
            {
               int8 localVar; // since local, no class preface needed
               localVar = 1;
               clsVAR(flagRunning) = status;
            }

*/

#define mDEFCLASS(cls,df)  ##cls##_##df
#define clsFN(f)           mDEFCLASS(class,f)
#define clsVAR(f)          mDEFCLASS(class,f)


_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
mbradley



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

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

PostPosted: Mon Feb 01, 2010 11:13 am     Reply with quote

I am just curious, did anyone find this useful?

I know I have read posts in the past about C++ and/or OOP, and this seems to fill the bill doesn't it?

For me, it was for a set of serial routines/manipulation, I moved to a chip with 4 uarts, and I needed 4 instances of the same code, and buffers.
_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

PostPosted: Tue Feb 09, 2010 7:43 am     Reply with quote

Mike,

I havent used it, but keep your goodies coming, its all good stuff
mbradley



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

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

PostPosted: Tue Feb 09, 2010 5:04 pm     Reply with quote

Mike, Thank you. I just didnt want to be "that guy" posting alot of useless stuff.
_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
Gary Smithson



Joined: 13 Feb 2004
Posts: 22

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

PostPosted: Fri Nov 19, 2010 1:39 pm     Reply with quote

Michael,

I wanted to let you know that I find your pseudo class concept very clever and that I have now used it for the first time. It works well for modifying function and variable names thus cloning a single driver into multiple. Very nice.

In one of my drivers I needed to use it for creating a #define but through compiler weakness it failed to work. I have reported this to CCS and ask them to add the functionality. An example of what didn't work properly is below:
Code:

#define class   example_class

#define cls(FLAG)  // Does not create example_class_FLAG
#if defined (cls(FLAG))  // Condition test causes compilation problems
#if !defined (cls(FLAG))  // Condition test causes compilation problems

I hope CCS will make a contribution here to fully enable this functionality.

Thanks for the fresh idea,
Gary Smithson
mbradley



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

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

PostPosted: Fri Nov 19, 2010 8:46 pm     Reply with quote

Thank you so much. I wasnt sure if I just got into a roll of posting useless stuff.

This piece has helped me alot, I had a set of serial functions for buffered in and out in the background, and I needed to use it across 4 serial ports. What a nightmare modifying the source.

Thank you again.
_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
Gary Smithson



Joined: 13 Feb 2004
Posts: 22

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

PostPosted: Fri Nov 19, 2010 9:31 pm     Reply with quote

Michael,

Speaking of your class driver for UART; what is your preferred method for tying the class to the specific UART peripheral? In other words, if the class is defined as uart1, how do you cause the generic UART class driver to compile for and manipulate only UART 1 registers?

Does the UART class driver contain something of this fashion:
Code:
#define cls(IN_USE)

#if defined (uart1_IN_USE)
   // function calls specific to UART 1
#endif

#if defined (uart2_IN_USE)
   // function calls specific to UART 2
#endif

... and so on


Obviously the code above is not exactly what you are using because the compiler doesn't seem to be able to handle it, so I would enjoy learning your solution for this.

Thanks,
Gary
mbradley



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

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

PostPosted: Fri Nov 19, 2010 10:36 pm     Reply with quote

Here is a snippet of some code I pulled up
Code:

#use rs232(UART1,baud=BAUD_NMEA,parity=N,bits=8,stream=ttys0)
#use rs232(UART2,baud=BAUD_CMDIO,parity=N,bits=8,stream=ttys1)


#include "class.h"

#define class clsTTY0
  #include "clsTTY.c"
  #undef class

#define class clsTTY1
  #include "clsTTY.c"
  #undef class


// internal serial port
#int_RDA
void  isr_serRx_ttls0(void)
{
   clsTTY0_intRX( fgetc(ttls0) );
}


#int_RDA2
void  isr_serRx_ttls1(void)
{
   clsTTY0_intRX( fgetc(ttls1) );
}


****** snipped of my tty code *****

void clsFN(intRX)(int8 data)
{
 
   if (clsVAR(serFifoPntr_ttls1) < SERIAL_FIFO_SIZE)
     {
     if (data == 0x0D) { clsVAR(cntSerHasStrings_ttls1)++; }
     clsVAR(serFifo_ttls1[serFifoPntr_ttls1]) = data;
     clsVAR(serFifoPntr_ttls1)++;
     clsVAR(serFifo_ttls1[serFifoPntr_ttls1]) = 0x00;
     }

   if (clsVAR(serFifoPntr_ttls1) == SERIAL_FIFO_SIZE)
     {
     clsVAR(flagSerOverFlow_ttls1) = TRUE;
     }

}



_________________
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