About creat User.cylib

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

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
 

0 Likes
19 Replies
markgsaunders
Employee
Employee
50 sign-ins 10 solutions authored 5 solutions authored

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 = Sampl; 
     )

   

 

   

over this:

   

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

   

 

   

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

   

-- mgs

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

@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

0 Likes
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

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.


 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

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.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

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.

   


 

0 Likes
lock attach
Attachments are accessible only for community members.
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

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
 

0 Likes
markgsaunders
Employee
Employee
50 sign-ins 10 solutions authored 5 solutions authored

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.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
markgsaunders
Employee
Employee
50 sign-ins 10 solutions authored 5 solutions authored

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.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
markgsaunders
Employee
Employee
50 sign-ins 10 solutions authored 5 solutions authored

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

   

-- mgs

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
markgsaunders
Employee
Employee
50 sign-ins 10 solutions authored 5 solutions authored

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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?

0 Likes
markgsaunders
Employee
Employee
50 sign-ins 10 solutions authored 5 solutions authored

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

0 Likes
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

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

0 Likes