Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

BufferedSpi Class Reference

BufferedSpi Class Reference

A spi port (SPI) for communication with wifi device. More...

#include <BufferedSpi.h>

Public Member Functions

 BufferedSpi (PinName mosi, PinName miso, PinName sclk, PinName nss, PinName datareadypin, uint32_t buf_size=2500, uint32_t tx_multiple=1, const char *name=NULL)
 Create a BufferedSpi Port, connected to the specified transmit and receive pins.
virtual ~BufferedSpi (void)
 Destroy a BufferedSpi Port.
virtual void frequency (int hz)
 call to SPI frequency Function
virtual void flush_txbuf (void)
 clear the transmit buffer
virtual void format (int bits, int mode)
 call to SPI format function
virtual int readable (void)
 Check on how many bytes are in the rx buffer.
virtual int writeable (void)
 Check to see if the tx buffer has room.
virtual int getc (void)
 Get a single byte from the BufferedSpi Port.
virtual int putc (int c)
 Write a single byte to the BufferedSpi Port.
virtual int puts (const char *s)
 Write a string to the BufferedSpi Port.
virtual int printf (const char *format,...)
 Write a formatted string to the BufferedSpi Port.
virtual ssize_t buffwrite (const void *s, std::size_t length)
 Write data to the Buffered Spi Port.
virtual ssize_t buffsend (size_t length)
 Send datas to the Spi port that are already present in the internal _txbuffer.
virtual ssize_t read ()
 Read data from the Spi Port to the _rxbuf.
void setTimeout (int timeout)
 Allows timeout to be changed between commands.
virtual void sigio (Callback< void()> func)
 Register a callback once any data is ready for sockets.
virtual void attach (Callback< void()> func, IrqType type=RxIrq)
 Attach a function to call whenever a serial interrupt is generated.
template<typename T >
void attach (T *obj, void(T::*method)(), IrqType type=RxIrq)
 Attach a member function to call whenever a serial interrupt is generated.
template<typename T >
void attach (T *obj, void(*method)(T *), IrqType type=RxIrq)
 Attach a member function to call whenever a serial interrupt is generated.

Detailed Description

A spi port (SPI) for communication with wifi device.

Can be used for Full Duplex communication, or Simplex by specifying one pin as NC (Not Connected)

Example:

  #include "mbed.h"
  #include "BufferedSerial.h"

  BufferedSerial pc(USBTX, USBRX);

  int main()
  {
      while(1)
      {
          Timer s;

          s.start();
          pc.printf("Hello World - buffered\n");
          int buffered_time = s.read_us();
          wait(0.1f); // give time for the buffer to empty

          s.reset();
          printf("Hello World - blocking\n");
          int polled_time = s.read_us();
          s.stop();
          wait(0.1f); // give time for the buffer to empty

          pc.printf("printf buffered took %d us\n", buffered_time);
          pc.printf("printf blocking took %d us\n", polled_time);
          wait(0.5f);
      }
  }

Software buffers and interrupt driven tx and rx for Serial

Definition at line 71 of file BufferedSpi.h.


Constructor & Destructor Documentation

BufferedSpi ( PinName  mosi,
PinName  miso,
PinName  sclk,
PinName  nss,
PinName  datareadypin,
uint32_t  buf_size = 2500,
uint32_t  tx_multiple = 1,
const char *  name = NULL 
)

Create a BufferedSpi Port, connected to the specified transmit and receive pins.

Parameters:
SPImosi pin
SPImiso pin
SPIsclk pin
SPInss pin
Datareadypin
buf_sizeprintf() buffer size
tx_multipleamount of max printf() present in the internal ring buffer at one time
nameoptional name

Definition at line 77 of file BufferedSpi.cpp.

~BufferedSpi ( void   ) [virtual]

Destroy a BufferedSpi Port.

Definition at line 93 of file BufferedSpi.cpp.


Member Function Documentation

void attach ( Callback< void()>  func,
IrqType  type = RxIrq 
) [virtual]

Attach a function to call whenever a serial interrupt is generated.

Parameters:
funcA pointer to a void function, or 0 to set as none
typeWhich serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)

Definition at line 316 of file BufferedSpi.cpp.

void attach ( T *  obj,
void(T::*)()  method,
IrqType  type = RxIrq 
)

Attach a member function to call whenever a serial interrupt is generated.

Parameters:
objpointer to the object to call the member function on
methodpointer to the member function to call
typeWhich serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)

Definition at line 218 of file BufferedSpi.h.

void attach ( T *  obj,
void(*)(T *)  method,
IrqType  type = RxIrq 
)

Attach a member function to call whenever a serial interrupt is generated.

Parameters:
objpointer to the object to call the member function on
methodpointer to the member function to call
typeWhich serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)

Definition at line 229 of file BufferedSpi.h.

ssize_t buffsend ( size_t  length ) [virtual]

Send datas to the Spi port that are already present in the internal _txbuffer.

Parameters:
length
Returns:
the number of bytes written on the SPI port

Definition at line 218 of file BufferedSpi.cpp.

virtual ssize_t buffwrite ( const void *  s,
std::size_t  length 
) [virtual]

Write data to the Buffered Spi Port.

Parameters:
sA pointer to data to send
lengthThe amount of data being pointed to
Returns:
The number of bytes written to the Spi Port Buffer
void flush_txbuf ( void   ) [virtual]

clear the transmit buffer

Definition at line 147 of file BufferedSpi.cpp.

void format ( int  bits,
int  mode 
) [virtual]

call to SPI format function

Definition at line 104 of file BufferedSpi.cpp.

void frequency ( int  hz ) [virtual]

call to SPI frequency Function

Definition at line 99 of file BufferedSpi.cpp.

int getc ( void   ) [virtual]

Get a single byte from the BufferedSpi Port.

Should check readable() before calling this.

Returns:
A byte that came in on the SPI Port

Definition at line 131 of file BufferedSpi.cpp.

int printf ( const char *  format,
  ... 
) [virtual]

Write a formatted string to the BufferedSpi Port.

Parameters:
formatThe string + format specifiers to write to the Spi Port
Returns:
The number of bytes written to the Spi Port Buffer

Definition at line 173 of file BufferedSpi.cpp.

int putc ( int  c ) [virtual]

Write a single byte to the BufferedSpi Port.

Parameters:
cThe byte to write to the SPI Port
Returns:
The byte that was written to the SPI Port Buffer

Definition at line 140 of file BufferedSpi.cpp.

int puts ( const char *  s ) [virtual]

Write a string to the BufferedSpi Port.

Must be NULL terminated

Parameters:
sThe string to write to the Spi Port
Returns:
The number of bytes written to the Spi Port Buffer

Definition at line 152 of file BufferedSpi.cpp.

ssize_t read (  ) [virtual]

Read data from the Spi Port to the _rxbuf.

Parameters:
max,:optional. = max sieze of the input read
Returns:
The number of bytes read from the SPI port and written to the _rxbuf

Definition at line 241 of file BufferedSpi.cpp.

int readable ( void   ) [virtual]

Check on how many bytes are in the rx buffer.

Returns:
1 if something exists, 0 otherwise

Definition at line 121 of file BufferedSpi.cpp.

void setTimeout ( int  timeout )

Allows timeout to be changed between commands.

Parameters:
timeouttimeout of the connection in ms

Definition at line 195 of file BufferedSpi.h.

void sigio ( Callback< void()>  func ) [virtual]

Register a callback once any data is ready for sockets.

Parameters:
funcFunction to call on state change

Definition at line 321 of file BufferedSpi.cpp.

int writeable ( void   ) [virtual]

Check to see if the tx buffer has room.

Returns:
1 always has room and can overwrite previous content if too small / slow

Definition at line 126 of file BufferedSpi.cpp.