Teensy Core Libraries

repository·master·Indexed 20 days ago

https://github.com/paulstoffregen/cores

Core libraries for supporting Teensy 2.0, LC, 3.x, and 4.x microcontrollers within the Arduino development environment. Includes abstract base classes for network communication, such as the Client class (inheriting from Stream) for connection management and data I/O, and the Server class (inheriting from Print) for network services.

Tokens
1.7K
Snippets
6
Records
8
Agent score
68%

What's inside paulstoffregen-cores

  1. Install Teensy core libraries for Arduino

    master

    To use Teensy 2.0, LC, 3.x, or 4.x boards with the Arduino IDE, you should use the official Teensyduino installer. This installer provides the latest stable version of the Teensy core libraries. You can download the installer from the official PJRC website.

    http://www.pjrc.com/teensy/td_download.html
  2. Implement or use the Client class for network communication

    master

    The Client class is an abstract base class used for network communication in the Teensy core. It inherits from Stream, meaning any implementation of Client can be used with functions that expect a Stream object (like Serial or File).

    Because it is an abstract class, you cannot instantiate Client directly. Instead, you use concrete implementations provided by specific network libraries (such as those for Ethernet or WiFi).

  3. Use Print methods with Server implementations

    master

    Because Server inherits from Print, any class that implements the Server interface can be used with standard Arduino printing functions. This allows you to send formatted strings, integers, and other data types directly to your network service using .print() and .println().

    // Assuming MyNetworkServer implements Server
    MyNetworkServer myServer;
    
    void setup() {
      myServer.begin();
      myServer.println("Hello, Network!");
    }
  4. Implement a custom Server class using the Server base class

    master

    The Server class is an abstract base class designed to provide a common interface for network services on Teensy. It inherits from Print, allowing server instances to be used with standard Arduino printing functions.

    To use this class, you must create a subclass that implements the pure virtual begin() method. This method is responsible for initializing the underlying network hardware or service.

    #include <Server.h>
    
    class MyCustomServer : public Server {
    public:
      void begin() override {
        // Initialize your network service here
      }
    };
    
    MyCustomServer myServer;
    
    void setup() {
      myServer.begin();
    }
  5. Use the Client class for network communication

    master

    The Client class is an abstract base class used for network communication in the Teensy core. It inherits from Stream, meaning it can be used anywhere a Stream (like Serial) is expected. Because it is an abstract class, you do not instantiate Client directly; instead, you use a concrete implementation (such as a TCP client provided by a specific network hardware driver) that implements the following interface.

    Connection Methods

    • connect(IPAddress ip, uint16_t port): Establishes a connection to a specific IP address and port. Returns an integer status code.
    • connect(const char *host, uint16_t port): Establishes a connection to a hostname and port. Returns an integer status code.

    Data Transmission (via Stream)

    • write(uint8_t): Writes a single byte.
    • write(const uint8_t *buf, size_t size): Writes a buffer of bytes.
    • available(): Returns the number of bytes available to read.
    • read(): Reads a single byte.
    • read(uint8_t *buf, size_t size): Reads a buffer of bytes.
    • peek(): Returns the next available byte without removing it from the buffer.
    • flush(): Ensures all outgoing data is transmitted.

    Connection Management

    • stop(): Closes the connection.
    • connected(): Returns 1 if the client is connected, 0 otherwise.
    • operator bool(): Allows the client to be used in boolean contexts (e.g., if (client) { ... }) to check connection status.
    // Example conceptual usage of a class implementing Client
    // Note: Client itself is abstract and cannot be instantiated.
    
    void loop() {
      if (client.available() > 0) {
        int data = client.read();
        // Process data
      }
    
      if (client.connected()) {
        client.write('A');
      } else {
        client.connect(IPAddress(192, 168, 1, 1), 80);
      }
    }
  6. Implement the Server base class

    master

    The Server class is an abstract base class designed for network services. It inherits from the Arduino Print class, allowing any implementation to use standard print() and println() methods to send data over the network.

    To use Server, you must create a derived class that implements the pure virtual begin() method. This method is responsible for initializing the underlying network hardware or service.

    class MyNetworkServer : public Server {
    public:
      virtual void begin() override {
        // Initialize network hardware here
      }
    };
  7. Client interface methods

    master

    The Client class defines the following pure virtual methods that must be implemented by concrete network subclasses. These methods handle connection management and data I/O:

    Connection Management

    • virtual int connect(IPAddress ip, uint16_t port) = 0: Establishes a connection to a specific IP address and port. Returns 0 on success or an error code.
    • virtual int connect(const char *host, uint16_t port) = 0: Establishes a connection to a hostname and port.
    • virtual void stop() = 0: Closes the connection.
    • virtual uint8_t connected() = 0: Returns a non-zero value if the client is currently connected.
    • virtual operator bool() = 0: Allows the client to be used in boolean contexts (e.g., if (client)) to check connection status.

    Data I/O (Stream Interface)

    • virtual size_t write(uint8_t) = 0: Writes a single byte.
    • virtual size_t write(const uint8_t *buf, size_t size) = 0: Writes a buffer of bytes.
    • virtual int available() = 0: Returns the number of bytes available to read.
    • virtual int read() = 0: Reads a single byte.
    • virtual int read(uint8_t *buf, size_t size) = 0: Reads a buffer of bytes.
    • virtual int peek() = 0: Returns the next byte in the buffer without removing it.
    • virtual void flush() = 0: Ensures all outgoing data has been sent.
  8. Use the Server::begin() method

    master

    The begin() method is a required virtual function in the Server class. It is used to initialize the underlying network service or hardware required for the server to start accepting connections or processing requests. Because it is a pure virtual function (=0), it must be implemented by any non-abstract subclass.

    // In your derived class implementation:
    void begin() override {
      // Setup logic
    }