element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
NexGen Flight Simuator Software Dev: Arduino sketch & eclipse on Linux - 3.0
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: phoenixcomm
  • Date Created: 4 Jul 2018 1:04 AM Date Created
  • Views 1056 views
  • Likes 1 like
  • Comments 10 comments
  • arduino programing
  • broken <pre> tag
  • arduino ide
  • arduino_tutorials
  • ansi c
  • eclipse ide
  • blog editor crap
  • nexgen flight simulator
Related
Recommended

Software Dev: Arduino sketch & eclipse on Linux - 3.0

phoenixcomm
phoenixcomm
4 Jul 2018

OK, so far I have shown you what you need, and what not to do, and never dos and just bad things that we tend not to do as programmers. Now I'm going to so you a program side-by-side Arduino vs eclipse using ANSI C.

 

Arduinoeclipse
/* C-3436A
*
*  C. Harrison Aug 30, 2014 Creation
* */

 

#include "C3436A.h"

 

void setup(){
  void modes();
  Serial.begin(19200);
  Serial.println("C-3436A awake");
  void init();
}

 

void loop(){
  int i;
  //  check for input:
  while( Serial.available()) {
    char data = Serial.read();
    if( data == '!') {
      char cmd = Serial.read();
      char dat = Serial.read();
      char eol = Serial.read();
      if( cmd = 1 ) {
        // panel light
        ;}
      if( dat = 0 ) {
        digitalWrite( dial_light, LOW); }
      if( dat = 1 ) {
        digitalWrite( dial_light, HIGH); }
      }}
     

 

//  read all the data
for( i= 1; i <  6; i++ ) {
    frac[i] = digitalRead( F[i]); 
    hund[i] = digitalRead( H[i]); }
    pwr    = digitalRead( powerpin );
    //  calulate the values:    ftest, Htest;
    int ftest, htest, ftest0, htest0, pwr, pwr0;

 

ftest = decode_F(calc_F());
htest = decode_H(calc_H());

 

if( ftest != ftest0 or htest != htest0 or pwr != pwr0 ) {
  //send message to RSS
  swap();  }
else {
      ; }  // Do Nothing
}}

 

int decode_H( int data ){
  int code[][2] = {{13,0},{9,108},{11,109},{5,110},
              {27,111},{16,112},{30,113},{2,114},
              {23,115},{8,116},{28,117},{1,118},
              {7,119},{4,120},{24,121},{20,122},
              {6,123},{18,124},{17,125},{10,126}};
             
  for( int i= 0; i < 22; i++ ) {
      if( data == code[i][0]) {
        return code[i][1]; }
}}

 

int decode_F( int data ){
  int code[] = {20, 5, 8, 2, 16, 4, 1, 9, 10, 18 };
    for( int i= 0; i < 10; i++ ) {
      if( data == code[i]) {
        return i; }
}}

 

int calc_F(){
  int temp = ( F[22] + (F[21]*2) + (F[20]*4) + (F[8]*8) + (F[7]*16));
  return( temp );
}

 

int calc_H(){
  int temp = ( H[19] + (H[18]*2) + (H[17]*4) + (H[5]*8) + (H[4]*16));
  return( temp );
}

 

void swap(){
  for( int i = 1; i < 6; i++ ) {
    frac0[i] = frac[i];
    hund0[i] = hund[i]; }
    ftest0  = ftest;
    htest0  = htest;
    pwr0    = pwr;
}

 

void modes(){

 

pinMode( F[1], INPUT_PULLUP);
pinMode( F[2], INPUT_PULLUP);
pinMode( F[3], INPUT_PULLUP);
pinMode( F[4], INPUT_PULLUP);
pinMode( F[5], INPUT_PULLUP);

 

// hundreds array
pinMode(H[1], INPUT_PULLUP);
pinMode(H[2], INPUT_PULLUP);
pinMode(H[3], INPUT_PULLUP);
pinMode(H[4], INPUT_PULLUP);
pinMode(H[5], INPUT_PULLUP);

 

// Now we have to assign the power switch pin

 

pinMode( powerpin, INPUT_PULLUP);

 

// Now we have to assign  the digital output pins

 

pinMode(dial_light, OUTPUT);

 

}

 

// we need to read the first time so while in loop the test will not fail.
// read all the pins and put then in arrays
void init(){
int i;

 

    for( i=1; i< 7; i++){
      frac0[i] = digitalRead(F[i]);
      hund0[i] = digitalRead(H[i]); }
    ftest0  = decode_F(calc_F());
    htest0  = decode_H(calc_H());
    pwr      = digitalRead(powerpin); }
* C-3436A.c
**  C. Harrison Aug 30, 2014 Creation
*              July 3, 2018 Re-Creation from arduino source * */

 

#include "C3436A.h"
#include "C3436A.subs.c"

 

void main( ){
  init(); //one time call to do some housekeeping
  // the next two lines deal with arduino serial library
  Serial.begin(19200);
  Serial.println("C-3436A awake");

 

  do{
      int i;      //  check for input:
      while( Serial.available()) {
          char data = Serial.read();
            if( data == '!') {
                char cmd = Serial.read();
                char dat = Serial.read();
                char eol = Serial.read();
            if( cmd = 1 ) {
                // panel light }
            if( dat = 0 ) {
                digitalWrite( dial_light, LOW); }
            if( dat = 1 ) {
                digitalWrite( dial_light, HIGH); }}
      //  read all the data
      for( i= 1; i <  6; i++ ) {
          frac[i] = digitalRead( F[i]);
          hund[i] = digitalRead( H[i]); }
          pwr = digitalRead( powerpin );
          // calulate the values:ftest, Htest;
          int ftest, htest, ftest0, htest0, pwr, pwr0;
          ftest = decode_F(calc_F());
          htest = decode_H(calc_H());
          if( ftest != ftest0 or htest != htest0 or pwr != pwr0 ) {
            //send message to RSS
            swap(); }
          else {
            ;  }}(while TRUE ); // This is a forever loop with out an exit
}  // Do Nothing
return; }

 

NOTES:
  • Notice this is much more readable than the with the Arduino IDE.
  • Also notice that in using .h files I have hid the blood of the pins, and their modes.
  • You shouldn't care where your data comes from its just data. So, isolate it, and or encapsulate it.
  • Now let take a look at my logic I want this chunk of code well to run forever but maybe I would like to stop it.
    • while( TEST ){} you test first.
    • do{ }while( TEST) you test last
    • for( int x=0; TEST; x++[normal thing]) this would be good if I wanted to run x times
    • for(;;) is a forever loop this notation is fround on as it contains no information
      • #define FOREVER for(;;)
      • #define LOOP  kind of the same but again without information
    • Whoops, Duh, I forgot to tell you that whenever you use the pre-processor directives such as  #define, etc.  they should be CAPED,  this is your clue to go look in the header file. This is not a compiler thing, it is more of a convention.
/* C-3436A.h
*
*  C. Harrison    Aug 21, 2014    Creation
*
*/
// we need to list the digital input pins:
// Pin defs
// please note: arrays F[] & H[] are being indexed 1-5
int F[]= { 0, 22, 23, 24, 25, 26 };
int H[]= { 0, 30, 31, 32, 33, 34 };
int powerpin = 20;
int dial_light = 40;
// now we need the variables & arrays
int frac[6];
int hund[6];
int frac0[6];
int hund0[6];
int pwr, pwr0; int ftest, ftest0;
int htest, htest0;
/* C-3436A.subs.c
* *  C. Harrison Aug 30, 2014 Creation
*              July 3, 2018 Re-Creation from arduino source
*
*/
int decode_H( int data ){
  int code[][2] = {{13,0},{9,108},{11,109},{5,110},
              {27,111},{16,112},{30,113},{2,114},
              {23,115},{8,116},{28,117},{1,118},
              {7,119},{4,120},{24,121},{20,122},
              {6,123},{18,124},{17,125},{10,126}};
   
  for( int i= 0; i < 22; i++ ) {
      if( data == code[i][0]) {
        return code[i][1]; }}}


int decode_F( int data ){
  int code[] = {20, 5, 8, 2, 16, 4, 1, 9, 10, 18 };

 
  for( int i= 0; i < 10; i++ ) {
      if( data == code[i]) {
        return i; }}}

int calc_F(){
  int temp = ( F[22] + (F[21]*2) + (F[20]*4) + (F[8]*8) + (F[7]*16));
  return( temp ); }

 

int calc_H(){
  int temp = ( H[19] + (H[18]*2) + (H[17]*4) + (H[5]*8) + (H[4]*16));
  return( temp ); }

void swap(){
  for( int i = 1; i < 6; i++ ) {
    frac0[i] = frac[i];
    hund0[i] = hund[i]; }
    ftest0  = ftest;
    htest0  = htest;
    pwr0    = pwr; }

 

void init(){
  // we need to read the first time so while in the loop the test will not fail.
  // read all the pins and put them in arrays
  int i;


  for( i=1; i< 7; i++){
      frac0[i] = digitalRead(F[i]);
      hund0[i] = digitalRead(H[i]); }
  ftest0  = decode_F(calc_F());
  htest0  = decode_H(calc_H());
  pwr      = digitalRead(powerpin); }

/*

* C-3436A.h

*

*  C. Harrison Aug 30, 2014 Creation

*              July 3, 2018 Re-Creation from arduino source

*

*/

 

#define TRUE 1;

#include "include/pins.h"

#include "include/modes.h"

/* C-3436A.subs.h
*
*  C. Harrison Aug 30, 2014 Creation
*              July 3, 2018 Re-Creation from arduino source
*
*
int calc_F();
int calc_H();
int decode_F( int data );
int decode_H( int data );
void init();
void swap();
/*

*  pins.h

*

*  C. Harrison Aug 21, 2014 Creation

*              July 3, 2018 Re-Creation from arduino source

*  (https://www.hackster.io/phoenixcomm/nexgen-flight-simulator-radio-interface-5a1f52 )

*/

// we need to list the digital input pins:

 

// Pin defs

// please note: arrays F[] & H[] are being indexed 1-5

int F[]= { 0, 22, 23, 24, 25, 26 };

int H[]= { 0, 30, 31, 32, 33, 34 };

 

int powerpin = 20;

int dial_light = 40;

/*
*  modes.h
*
*  C. Harrison Aug 21, 2014 Creation
*              July 3, 2018 Re-Creation from arduino source
*/

void modes(){
  // units array
  pinMode( F[1], INPUT_PULLUP);
  pinMode( F[2], INPUT_PULLUP);
  pinMode( F[3], INPUT_PULLUP);
  pinMode( F[4], INPUT_PULLUP);
  pinMode( F[5], INPUT_PULLUP);
  // hundreds array
  pinMode(H[1], INPUT_PULLUP);
  pinMode(H[2], INPUT_PULLUP);
  pinMode(H[3], INPUT_PULLUP);
  pinMode(H[4], INPUT_PULLUP);
  pinMode(H[5], INPUT_PULLUP);

  // Now we have to assign the power switch pin
  pinMode( powerpin, INPUT_PULLUP);

  // Now we have to assign  the digital output pins
  pinMode(dial_light, OUTPUT); }

 

Revisions:

 

  1. Purists, please don't jump down my throat. I broke a general rule on header files that it should not contain executable code. The header file modes.h has a function modes() this function should go into C-3436A.subs.c and then called placed in the top of the function init without the function wrapper.  [Wednesday, July 4, 2018 5:40:21 PM GMT]
  2. This is a letter that I just (a few hours ago to the folks at Element14. This letter/note discusses the <pre> tag and how it does not work as advertised!

    You all know how I barely tolerate this really crappy editor with all its stupid quirks (me being nice!) You should do everybody a favor and JUST EMBRACE HTML5 with CSS (just like eBay !) Like your stupid reason forbidding <style> tags (local tags CAN NOT BE HACKED!) so that now we can use classes!; Yes you could inject malicious code with this external stylesheet! Duh so don't allow this: "<link rel="stylesheet" type="text/css" href="mystyle.css">" (local tags CAN NOT BE HACKED!))! But you guys tossed out the bathwater, the baby went with it. LOL!

    But whats wrong with this:

    "<style>

    Arial10 {font-family: 'arial'; font-size: 10px;}

    </style>"

    useage: <div class="Arial10" style="....

    But this one is OFF THE CHART~!
    First Offical reference the lowly <pre>...</pre> tag!
    https://developer.mozilla.org/en-US/docs/Web/HTML...

    Text in a pre element
    is displayed in a fixed-width
    font, and it preserves
    both      spaces and
    line breaks.
    

    ---------- REAL LIFE case in point:
    "NexGen: Software Development: arduino sketch & eclipse on Linux. Part 3"
    ---------- WAS:

    /*
    * C-3436A.subs.h

    *
    *  C. Harrison Aug 30, 2014 Creation

    *              July 3, 2018 Re-Creation from arduino source
    *
    */

    int calc_F();
    int calc_H();
    int decode_F( int data );
    int decode_H( int data );
    void init();
    void swap();


    ----- GOT -train wreck this is the source!

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;">/*<br /> * C-3436A.subs.h</span></p>

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;"> *<br /> *  C. Harrison Aug 30, 2014 Creation</span></p>

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;"> *              July 3, 2018 Re-Creation from arduino source</span></p>

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;"> *<br /> */<br /></span></p>

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;">int calc_F();</span></p>

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;">int calc_H();</span></p>

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;">int decode_F( int data );</span></p>

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;">int decode_H( int data );</span></p>

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;">void init();</span></p>

    <p><span style="font-family: 'andale mono', times; font-size: 8pt;">void swap();</span></p>

    <p style="color: #000000;"><span style="font-family: 'andale mono', times; font-size: 8pt;"></span></p>

     

    ---- FIXED This is better.

    <pre><span style="font-family: 'andale mono', times; font-size: 8pt;">/*<br /> * C-3436A.subs.h<br /> *<br /> *  C. Harrison Aug 30, 2014 Creation<br /> *              July 3, 2018 Re-Creation from arduino source</span><br /> *<br /> */<br /><br />int calc_F();<br />int calc_H();<br />int decode_F( int data );<br />int decode_H( int data );<br />void init()void swap();<br /><br /><br /></span></pre>

     

    ---- prolog I have spent many an hour last night on the 3rd when by all rights I should be in the pub for it is now the 4th of July (Brits see ref 1776). Last night with too many brews, I have come to this conclusion: That even after I put together a blog in Blue Fish Editor, I know that when I paste it into your editor something WILL BRAKE!! Please for G-d's sake and everybody's sanity GET OVER IT embraces  HTML5 and CSS. Thanks for the quality time. Now I have to get the Champaign in the fridge for this eve.

     

     

  • Sign in to reply

Top Comments

  • DAB
    DAB over 6 years ago +1
    Nice comparison Chris. DAB
  • neuromodulator
    neuromodulator over 5 years ago +1
    Well objects aint much big of a deal. Its more or less a single unit that contains data and functions. You still could do OOP with ansi C, the way you would do it is by defininig a struct that contains…
  • phoenixcomm
    phoenixcomm over 5 years ago in reply to neuromodulator +1
    neuromodulator use struct all the time some have struct in struct
  • phoenixcomm
    phoenixcomm over 5 years ago in reply to neuromodulator

    neuromodulator use struct all the time some have struct in struct

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neuromodulator
    neuromodulator over 5 years ago

    Well objects aint much big of a deal. Its more or less a single unit that contains data and functions. You still could do OOP with ansi C, the way you would do it is by defininig a struct that contains all the data that represents something, for instance a serial port, and then a set of functions that will use that struct and perform an operation (ie: read or write to the serial port). Of course its much more convenient to use the C++ style, but nothing stops you from doing OOP in good ol' C

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • phoenixcomm
    phoenixcomm over 5 years ago in reply to phoenixcomm

    reply you know I would just rather use a pointer to the pin and get it over with or a device descriptor for RS232, Ethernet, I2C, etc. And for where is the good stuff like printf() or fprintf()? A long time ago a guy named  Bjarne Stroustrup wrote a book called C++ and we thought he was nuts! C++ supports both OOP and non-OOP programming styles. When I write code I use the GCC ANSI C compiler.  As I am still struggling with OOP in JAVA I have no wish to learn C++ at this time. "Serial.read() does a lot for you or I would have to do to something like this:

    char buffer[500];

    // make buffer NULL

    cnt = 0;

    do {

        buffer[cnt] = NULL; }(cnt <500; cnt++);

    serialFD = open(/dev/ser1);  //open the device p-code

    //fill buffer

    do {

        buffer[cnt] = readChar(serialFD);

        }( buffer[cnt++] != NULL);  // check for NUL and bump cntr.

    so now since i know the lenth of the string I can play games like this

    if ( buffer[5] == myAddress) { Do Something;

    length = buffer[6];  // say 20

    command = buffer[8];

    cnt =10;

    do {

         pstring = buffer[10]++; }( cnt < ( buffer[6] + 10 );

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neuromodulator
    neuromodulator over 5 years ago in reply to phoenixcomm

    Which is a C++ lib image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • phoenixcomm
    phoenixcomm over 5 years ago in reply to neuromodulator

    Its the Arduino Library Call you are being too picky image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube