Author Topic: posting data on wifi server example  (Read 11047 times)

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: posting data on wifi server example
« Reply #15 on: December 13, 2013, 11:54:47 am »
After testing several solutions - including directly reading the bits (doesn't work either) - this seems to be the only working solution and explanation of why the others don't work:

http://forum.arduino.cc/index.php?topic=185291.0


also here is what I did to direct read the bits - but it only works if the pin is an input unless you do the above fix, after which digitalRead works just fine too:

inline int digitalReadDirect(int pin){
 
  int r = !!(g_APinDescription[pin].pPort -> PIO_PDSR & g_APinDescription[pin].ulPin);
 
}

dbell

  • Newbie
  • *
  • Posts: 44
Re: posting data on wifi server example
« Reply #16 on: December 13, 2013, 12:26:25 pm »
Yep, read that. Tried it. Doesn't seem to work.
If it works, then this should toggle the LED; it sets it on and leaves it on:

int pin=13;

void setup() {
  pinMode(pin, INPUT);
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
}

void loop() {
  digitalWrite(pin, !digitalRead(pin));
  delay(500);
}

MichaelMeissner

  • Full Member
  • ***
  • Posts: 166
Re: posting data on wifi server example
« Reply #17 on: December 13, 2013, 12:56:32 pm »
It sounds like the simplest solution is to use a wrapper function for digitalWrite that keeps tract of the state of the pin, and use that instead of trying to see what the entrails of the pin mapping does on the DigiX.

A simple minded approach would be:

Code: [Select]
const int max_pins = 120;
uint8_t pin_state[max_pins];

void
myDigitalWrite (uint8_t pin, uint8_t value)
{
  pin_state[pin] = value;
  digitalWrite (pin, value);
}

dbell

  • Newbie
  • *
  • Posts: 44
Re: posting data on wifi server example
« Reply #18 on: December 13, 2013, 05:48:28 pm »
Good suggestion, Michael!
With a corresponding mydigitalRead(), it would become nearly transparent.

Thanks,

Dave

MichaelMeissner

  • Full Member
  • ***
  • Posts: 166
Re: posting data on wifi server example
« Reply #19 on: December 13, 2013, 06:35:10 pm »
If you are worried about the memory space, you could use shifting to reduce the pin state array to 1/8th the size, but given the DigiX has 96k, saving 100 bytes might not be as important as it is for the Digispark where you only have 512 bytes (0.5k).

dbell

  • Newbie
  • *
  • Posts: 44
Re: posting data on wifi server example
« Reply #20 on: December 13, 2013, 06:54:48 pm »
Sure,

bitRead(pin_states[pin/8], pin%8);
Probably worth it for space, but the server is pretty slow, as it is...

MichaelMeissner

  • Full Member
  • ***
  • Posts: 166
Re: posting data on wifi server example
« Reply #21 on: December 13, 2013, 07:18:23 pm »
Sure,

bitRead(pin_states[pin/8], pin%8);
Probably worth it for space, but the server is pretty slow, as it is...
If you want optimal code, you want to make sure pin is an unsigned type like unsigned int, uint8_t, etc.  That way the compiler can change the /8 and %8 into LOGICAL SHIFT RIGHT/AND operations (1-2 instructions in most architectures).  The %8 on signed types involves a few extra instructions to get the sign right.

dbell

  • Newbie
  • *
  • Posts: 44
Re: posting data on wifi server example
« Reply #22 on: December 13, 2013, 07:35:04 pm »
Good pointer - thanks!

dbell

  • Newbie
  • *
  • Posts: 44
Re: posting data on wifi server example
« Reply #23 on: December 14, 2013, 10:35:48 am »
I added Analog input pin display, with floating point output this morning.

Dave