Pages: 1 2 3 [4]   Go Down
Author Topic: Reforming datatype practices  (Read 4338 times)
0 Members and 1 Guest are viewing this topic.
Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am all for making the arduino easier to program by adding useful abstractions and more documentation rather than changing the current the current syntax.

Can you name some useful abstractions? smiley

I'm rather invested in doing something about this. It's bugged me for some time, without any particular reason. And I feel now is a time for me to try to do something. Creating some useful abstractions, or producing some documentation. Actually both would be nice.
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can you name some useful abstractions?
digital and analog read and write, millis,  delay, map,  shiftOut and pulseIn for example.



there are still quite a few hardware capabilities that would be more accessable if someone wrapped some easy to use functions around them.  Two examples:

input Capture ( measuring pulse width  in background using timer1)

event scheduling ( I wrote something for my own purposes and posted it in a thread but  think its not intuitive enough for general use, see: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1217881285)
« Last Edit: February 06, 2009, 08:43:38 am by mem » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
adding useful abstractions

I was interested in some of the new abstractions. smiley


Event cheduling is indeed something to look at, will investigate your code later.



I am rather pleased with the SPin struct, it masks data in one byte that typically use 4 bytes. (one int for pin, one int for last state)
This is 4 times more space efficient.


example.pde
Quote

#include "Pins.h"

SPin led, pot, sw;

void setup()
{
  set( led, 13, OUTPUT, ON );
  set( pot, 2,  INPUT );
  set( sw,  12, INPUT,  PULLUP );
}

void loop()
{
  if(digitalRead2(sw))
  {
    set(led,ON);
  }
  set(led,OFF);
}
 

Pins.h
Quote

#include "WProgram.h"

#define constant (static const)

#define PULLUP true
#define ON true
#define OFF false

struct SPin
{
  byte pin : 5;
  byte mode : 1;
  byte state : 1;
};

void set(SPin& pin,byte pinNumber,boolean mode,boolean state=false)
{
  pin.pin = pinNumber;
  pin.mode = mode;
  pin.state = state;
  pinMode( pin.pin, pin.mode);
  digitalWrite( pin.pin, pin.state);
}

void set(SPin& pin,boolean state)
{
  pin.state = state;
  digitalWrite( pin.pin, pin.state);
}

boolean digitalRead2(SPin& pin)
{
  return (digitalRead(pin.pin)!=pin.state ? true : false);
}

unsigned int analogRead2(SPin& pin)
{
  return analogRead(pin.pin);
}

void toggle(SPin& pin)
{
  if ( pin.mode==OUTPUT )
  {
    pin.state ? set(pin,HIGH) : set(pin,LOW);
  }
}
 
« Last Edit: February 06, 2009, 10:47:50 am by AlphaBeta » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I agree heartily in regard to being more careful with memory usage in the library.  For example, the digitalRead routine should be returning a byte, not an int.  There are many such improvements that could be made.

I do not however, want to force programming style on anyone.

lilbuzz
Logged

Atascadero, CA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 468
Arduino addict
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I would love to see some (more) documentation on best practices and optimizations.  I've only recently discovered the Arduino and I have very little programming experience (I've done plenty of bash scripting in Linux) So what I have to work from are the examples and other folks' code.

It's important to me to learn the right way over learning the easy way within a certain scope.  But if what people post as help to others is sub-optimal, then we begin to breed a culture of slop.  I am sensitive to the problem of making things more complicated.  After all, it is the simplicity of Arduino that attracted me in the first place.

I would bet that a cursory review/update of the example code and some of the more popular docs would address the majority of the concern.
Logged

What about elevensies? Luncheon? Afternoon tea? Dinner? Supper?

0
Offline Offline
God Member
*****
Karma: 1
Posts: 513
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"best practices"

I don't mean to nitpick, but I was never fond of the term.  "best" is a marketing term and not a scientific term.  

In some cases, people can learn "best practices" by rote, and not by reason, and thus become inflexible when having to evaluate another approach for any given situation, where that situation might have different objectives than the presumptuous authors of said "best practice".

There is often not a "one size fits all" solution.


Edit: I just read the wikipedia version:
http://en.wikipedia.org/wiki/Best_practices

Seems most of what has been presented to me as "best practices" was really a bit of linguistic drift.  A good idea under certain circumstances being presented as a "best practice".
« Last Edit: February 21, 2009, 10:07:14 am by dcb » Logged

Atascadero, CA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 468
Arduino addict
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As it turns out, I've learned an interesting thing since my last post: you CAN NOT directly replace int with byte just to save memory.  The data types are handled differently by various things.  For instance a Serial.print() will output the ASCII character of a a byte value, not the number you are trying to display (maybe I need to qualify the call better, but it happened).  So I went back through and changed a number of variables to int data types just to read them.  As a noob, it makes me curious about the validity of the original post.

I agree about the "best practices" point.  But I think this thread scopes the issue to things like keeping the memory footprint small and writing code that runs as fast as possible for the needs of a project.
Logged

What about elevensies? Luncheon? Afternoon tea? Dinner? Supper?

Pages: 1 2 3 [4]   Go Up
Jump to: