Page 55 - Programming the Photon Getting Started With the Internet of Things
P. 55

3




                                           Particle Syntax











        The programming language used to program the Photon is called C. In this chapter

        you will learn and understand some of the basic programming terms using this language.
        You can use what you will learn here and apply this to most of the firmware you will write
        throughout the book. To get the best out of using your Photon, you will need to learn these
        basic programming fundamentals.




        What Is Programming?


        It  may  not  seem  obvious  to  a  beginner  what  programming  actually  is  and  what  a

        programming language is. When you look at the Photon’s firmware you could probably
        hazard a guess as to what it is actually doing without any programming knowledge, but we
        need to look a bit further into how the code goes from being lines of text to something in
        real time, like turning a light-emitting diode (LED) on or off.

             When  you  press  the  FLASH  button  on  the  Particle  Build  integrated  development

        environment  (IDE),  it  then  implements  a  chain  of  events  that  results  in  your  firmware
        being  uploaded  to  the  Photon  and  run.  What  it  actually  does  is  something  called
        compilation, where it takes your lines of code as text and translates them into something
        called  binary,  which  is  a  series  of  1’s  and  0’s  that  that  the  Photon’s  hardware  will

        understand. Recall from the previous chapter that you clicked the verify button before you
        actually flashed any of your code to the Photon. This attempts to precompile the C code
        that you have written without actually flashing it. Verifying your code also makes sure that
        what you have written makes sense in the C programming language. If you have written

        some code that is not within the C programming language, then when you verify your
        firmware, it will return an error. The same is true when you try compiling a firmware with
        no written code at all—the error returned tells us that there is no setup or loop function in

        your  code.  As  we  mentioned  in  the  previous  chapter,  these  two  functions  of  code  are
        required and must always be present within your firmware.

             Let’s add the following functions to our firmware and see if it compiles:


        void setup (){
        }
        void loop() {

        }
   50   51   52   53   54   55   56   57   58   59   60