Problems understanding Struct in this example !


hi,

link http://maniacbug.github.io/rf24network/

i trying understand how struct works in example found below. have tried find out using internet find explanations confusing.my questions relate  lines marked (1)  (2)  (3) respectively.

this think may happening ?

(1) assigns members  unsigned long ms;  unsigned long counter;, not put memory or take space.

(2) payload seems become variable here ?  also   millis(),   packets_sent    used here how related (1)

(3) does  &payload  ?

it seems  unsigned long ms;   unsigned long counter; never used ?



i welcome clear , easy understand reply have never used structs before.

thanks

regards  gary


code: [select]

/*
copyright (c) 2012 james coliz, jr. <maniacbug@ymail.com>

program free software; can redistribute and/or
modify under terms of gnu general public license
version 2 published free software foundation.
*/

/**
* simplest possible example of using rf24network
*
* transmitter node
* every 2 seconds, send payload receiver node.
*/

#include <rf24network.h>
#include <rf24.h>
#include <spi.h>

// nrf24l01(+) radio attached using getting started board
rf24 radio(9,10);

// network uses radio
rf24network network(radio);

// address of our node
const uint16_t this_node = 1;

// address of other node
const uint16_t other_node = 0;

// how send 'hello world other unit
const unsigned long interval = 2000; //ms

// when did last send?
unsigned long last_sent;

// how many have sent already
unsigned long packets_sent;

// structure of our payload
struct payload_t                              // (1)
{
  unsigned long ms;
  unsigned long counter;
};

void setup(void)
{
  serial.begin(57600);
  serial.println("rf24network/examples/helloworld_tx/");

  spi.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
}

void loop(void)
{
  // pump network regularly
  network.update();

  // if it's time send message, send it!
  unsigned long = millis();
  if ( - last_sent >= interval  )
  {
    last_sent = now;

    serial.print("sending...");
    payload_t   payload = { millis(), packets_sent++ };                                // (2)
    rf24networkheader header(/*to node*/ other_node);
    bool ok = network.write(header,&payload,sizeof(payload));                   // (3)
    if (ok)
      serial.println("ok.");
    else
      serial.println("failed.");
  }
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

1) define structure (a template if like)
2) make instance of structure , put values in it.
3) take address of structure.


Arduino Forum > Using Arduino > Programming Questions > Problems understanding Struct in this example !


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support