Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® Software > About creat User.cylib

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



About creat User.cylib
Moderator:
JFMD

Post Reply
Follow this topic



About creat User.cylib

pavloven posted on 29 Mar 2012 12:25 PM PST
Top Contributor
78 Forum Posts

I'm using Component Author Guide for creating my components, but I'm new to C.
I'm using VoltageDisplay.cywrk and creat  my first element (Simple moving average filter), and will use it as a model for others.
I want to ask questions:

- My  RunMean.cylib has an error that I did not see?
- What has been done irrationally?
- Is there a library of components created by other users?

RunMean.cylib element within the attached project.
thank you
 




Re: About creat User.cylib

yfs posted on 30 Mar 2012 08:59 AM PST
Cypress Employee
77 Forum Posts

I see nothing specifically wrong with your design. You do not say what fails to work but it builds OK - your library is inherently in the search path because you have included it in the workspace with the design project.

I did not see a check for a negative swing in the input though - you only check for a +2000 change, not a -2000.

I also recommend the following style:

    for(i = 0; i< MAX_SAMPLE; i++)
    {
        RunMeanArray[i] = Sampl; 
     )

 

over this:

    for(i = 0; i< MAX_SAMPLE; i++)
    RunMeanArray[i] = Sampl; 

 

I've bitten myself by not writing defensively so many times!

-- mgs



Re: About creat User.cylib

Bob Marlowe posted on 30 Mar 2012 09:41 AM PST
Top Contributor
1768 Forum Posts

@yfs

The interesting fact is not why you bit your nails not to have "written defensively so many times" but why this happened to EVERY programming guy several times. It is not the lack of learning from the very first error, so repeating that sort of bugs from time to time. The very astonishing thing is that we all (if I might say so) have suffered from constructs like that you showed up.

I've found open-source projects where contributions where rejected when ifs, fors and whiles did not follow a given structure -they obviously have bitten their nails up to the elbows once!

 

Happy defensive coding!

Bob

 



Re: About creat User.cylib

pavloven posted on 30 Mar 2012 11:11 AM PST
Top Contributor
78 Forum Posts

I am glad that you did not see any obvious errors which means that I have correctly understood the principles of creating a library.
I am very pleased that PSoC Creator allows me to create my own element and paste it into any project, a couple of mouse clicks.
I was hoping that there are resources where users share their elements.
If I find them - they will be a good example for me, and I do not need to create is already created by others.
I'll use your advice. Thank you.


 



Re: About creat User.cylib

Bob Marlowe posted on 30 Mar 2012 02:55 PM PST
Top Contributor
1768 Forum Posts

Hi pavloven,

I'm afraid to tell you about some errors in your C-program.

Between two calls of RunMean_Get all the local variables become undefined except if you define them with the storage-class "static" and give them an initial value.

In line 29 the variable "outdata" is undefined.

The resources the sliding average computation uses are not few. If you think a bit (or a bit more), you may find some math-tricks to reduce memory and CPU usage.

Your idea with the shifting of the summed result is pretty good, last time I programmed something similar I used float arithmetic.

Good work for the datasheet! How many hours did you spend for the complete project?

 

Bob



Re: About creat User.cylib

pavloven posted on 31 Mar 2012 12:15 AM PST
Top Contributor
78 Forum Posts

O yes, I'm sorry. I did a project for CY8CKIT - 050 and forgot to correct folder PSOC3.

RunMean.cylib contains RunMean.c  I hope everything is true:
 . . . . . . .
22   static int32 outdata =0 ;
static int16 index=0;     
 . . . . . . .

I used the project CD from CY8CKIT - 030 ----> Firmware ---> VoltageDisplay.cywrk
so I just moved the function of the moving average in a separate project RunMean.cylib.

It took five evenings in the company with an electronic translator. Hopefully soon it will be faster.



Re: About creat User.cylib

Bob Marlowe posted on 31 Mar 2012 01:13 AM PST
Top Contributor
1768 Forum Posts

As a warning for futer projects:

Due to the (comparable) poor addressing-modes of the 8051µP the Keil compiler allocates local variables on fixed memory locations. That might look like "static"-storage-class, but it is not. When you change the compiler; for instance when switching from PSoC 3 to PSoC5 or PSoC 1 (even when you declare a function as reentrant in PSoC3) or when there is a new / different compiler all programs relying on such behaveour will fail. To increase code-portability you should stick to the C-definitions which strictly says that local variables become undefined between two calls of the function.

As for a different (math) approach to "gliding average": there is a chance not to store every single sample thus saving you 2K of memory (which is more than some PSoCs have on chip).

Imagine the following: your array is filled and a new sample arrives. You have to kick out the first sample and insert the new. So, which value does the first sample have? We don't know? Well, no, but we can estimate it: it has the (estimated) value of the average! So, if we drop an averaged value and insert the new, everything would look (mathematical) fine. In terms of programming: you just have just to keep the sum of samples and then calculate the new sum with the new sample.

Sharpen your pencil and give it a try!

 

Happy math

Bob



Re: About creat User.cylib

pavloven posted on 31 Mar 2012 02:36 AM PST
Top Contributor
78 Forum Posts

Thanks, I'll ask my friend the programmer will explain to me your advice.

As for a your approach to "gliding average":
This method is familiar to me. Its uses my friend, I plan to add it to RunMean.cylib in the form of another function
and compare the two methods.  I'll let you know about the result.
I plan to create a     *.Cylib  collection on my page, so I ask lots of questions to avoid serious mistakes.


 



Re: About creat User.cylib

pavloven posted on 31 Mar 2012 05:32 AM PST
Top Contributor
78 Forum Posts

I added this variant:

 RunMeanSum = RunMeanSum - outdata2;    // Remove old outdata  (Mean) 
        RunMeanSum = RunMeanSum + Sampl;     // add new sample 
        outdata2 = RunMeanSum >> Mean_Range;        // result 
   

this option reduces the dynamics:
Replaced by a mean value of the array, it is not always the same oldest  element
 



Re: About creat User.cylib

yfs posted on 02 Apr 2012 09:19 AM PST
Cypress Employee
77 Forum Posts

But, Bob, it takes sooooooooo long to press the tab key and add those braces!!!!

Maybe we all make those mistakes because we convince ourselves we'll have time to go back and "make it look pretty."

-- Mark.

 



Re: About creat User.cylib

Bob Marlowe posted on 02 Apr 2012 09:42 AM PST
Top Contributor
1768 Forum Posts

Yes, and it takes soooooooo long to find that ******* semicolon in the line above the neatly indented one-line-loop-action which never executed at all, but until out of a sudden the slider crashes into....

 

while (!(MotorStatus = GetMotorStatus()) & MotorAtEndpointLeftMask);   // Is it really ready

    Motor_Turn(!Clockwise);

 

Bob



Re: About creat User.cylib

yfs posted on 03 Apr 2012 01:15 PM PST
Cypress Employee
77 Forum Posts

Ouch! I do draw the line there. No semis at the end of a loop statement. Ever. Even if I will throw the code away. Someone could go through the trash and I'd live in shame for life!

Are you an empty block or a lonely semicolon guy?
    for(  agoodreason() )
        {
        }

    for( anothergoodreason() )
        ;

-- Mark.

 



Re: About creat User.cylib

Bob Marlowe posted on 06 Apr 2012 01:35 PM PST
Top Contributor
1768 Forum Posts

Sorry, could not reply sooner, my "Post Reply"-button was gone.

I like to use:

if (something)

{

     // Do nothing yet

}

else

{

     This_is_a_Function_Call();

}

 

Bob



Re: About creat User.cylib

yfs posted on 09 Apr 2012 09:30 AM PST
Cypress Employee
77 Forum Posts

Good style, sir. I'm inspired to stick to my own rules now!

-- mgs



Re: About creat User.cylib

Bob Marlowe posted on 09 Apr 2012 02:18 PM PST
Top Contributor
1768 Forum Posts

Style, and even "good" style is always bound to a person or a community. Some people are "Stylish", some communities are "Stylish" and last, not least: style undergoes changes.

When Cypress supports it, we could open up a "Stylish Boutique of Programming Skills" or a "Skillful Boutique of Programming Styles". So we can avoid the word "School". As I usually try to express: Most of the engeneers working with PSoCs have learnt C, but only a few have learnt programming. Knowledge about programming techniques as "Circular Buffer", "Gliding Average", "Parsers" are rare and sometimes need support here in a forum.

 

Any idea how to accomplish that?

Bob



Re: About creat User.cylib

Bob Marlowe posted on 13 Apr 2012 11:39 AM PST
Top Contributor
1768 Forum Posts

Hi Mark,

I really wanted to close this thread, but please have a look at this: http://www.cypress.com/?app=forum&id=1573&rID=61933

 

Bob



Re: About creat User.cylib

yfs posted on 18 Apr 2012 09:28 AM PST
Cypress Employee
77 Forum Posts

I am not sure how we can help. C is not easy to learn on-the-fly.

One option would be to build a style-checker and weed out scary constructs. However, I fear it would annoy as many as it saves!

-- mgs

 



Re: About creat User.cylib

Bob Marlowe posted on 18 Apr 2012 10:08 AM PST
Top Contributor
1768 Forum Posts

Hi Mark,

we could write a "Handbook of DOs and DON'Ts"! I think we've got examples enough to fill it.

Did you see the last one that topped all

It red (shortened)

PRT0DR ^= 0x02; // Toggle bit 2 of port0

It took its time until I found the bug (for my excusion: it was a bit more hidden as here and I was focussed on a different point)

With the help of a handfull of macros errors like these can be omitted (if you use them).

As I usually say: "They learnt C. But nobody tought them programming"

Bob

BTW: what is the difference between your signatures: -- Mark and mgs?

 

 

 



Re: About creat User.cylib

yfs posted on 23 Apr 2012 09:09 AM PST
Cypress Employee
77 Forum Posts

Hello!

The difference between Mark and mgs and yfs is basically old age - I cannot remember which one I use in a given situation!

My name is Mark and my mother thinks my initials are mgs but Cypress calls me yfs. I tell people this stands for "young, free and single" but none of those are actually true.

-- mgysarfk

 



Re: About creat User.cylib

pavloven posted on 04 May 2012 01:42 PM PST
Top Contributor
78 Forum Posts

I tried to use DMA PSoC5 to send data to the memory of the graphic display 128 x 64 (GLCD).
 
in the archive: component  GLCD_DMA.cylib   and example  for POWERTIP PG-12864A.
  
I hope this is helpful to someone.
  
Text in Russian. I hope that the electronic translator will solve this problem.

DMA_for_GLCD_128x64.zip :

http://mylab.wmsite.ru/ftpgetfile.php?id=82

screenshot:

http://mylab.wmsite.ru/_mod_files/ce_images/cypress/glcd_dma.gif






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20