1 2 Previous Next 19 Replies Latest reply on May 4, 2012 3:42 PM by user_49271930

    About creat User.cylib

    user_49271930

      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
       

        • 1. Re: About creat User.cylib
          yfs

          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

          • 2. Re: About creat User.cylib
            user_1377889

            @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

            • 3. Re: About creat User.cylib
              user_49271930

              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.


               

              • 4. Re: About creat User.cylib
                user_1377889

                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

                • 5. Re: About creat User.cylib
                  user_49271930

                  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.

                  • 6. Re: About creat User.cylib
                    user_1377889

                    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

                    • 7. Re: About creat User.cylib
                      user_49271930

                      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.

                         


                       

                      • 8. Re: About creat User.cylib
                        user_49271930

                        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
                         

                        • 9. Re: About creat User.cylib
                          yfs

                          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.

                          • 10. Re: About creat User.cylib
                            user_1377889

                            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

                            • 11. Re: About creat User.cylib
                              yfs

                              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.

                              • 12. Re: About creat User.cylib
                                user_1377889

                                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

                                • 13. Re: About creat User.cylib
                                  yfs

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

                                     

                                  -- mgs

                                  • 14. Re: About creat User.cylib
                                    user_1377889

                                    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

                                    1 2 Previous Next