Skip to content
Snippets Groups Projects
VizBlocks.hpp 2.78 KiB
Newer Older
emorgan's avatar
emorgan committed
#ifndef VIZBLOCKS_H
#define VIZBLOCKS_H
Dave Murray-Rust's avatar
Dave Murray-Rust committed
#include "Arduino.h"
#include "Behaviours.h"
#include "ServoBehaviours.h"
#include "LEDBehaviours.h"
Dave Murray-Rust's avatar
Dave Murray-Rust committed

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define MQTT_topic  "new001"
Joe Revans's avatar
Joe Revans committed
#define NUM_BACKGROUND_BEHAVIOURS 5
Dave Murray-Rust's avatar
Dave Murray-Rust committed

emorgan's avatar
emorgan committed
class VizBlocks {
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  char* _ssid;
  char* _wifi_pass;
  char* _id;
  char* _server;
  int _port;
  BehaviourTable _behaviours;
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  Behaviour* _active = nullptr;
Joe Revans's avatar
Joe Revans committed
  Behaviour* _background[NUM_BACKGROUND_BEHAVIOURS];
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  int _loop_time = 5;
  Adafruit_MQTT_Client* _mqtt;
  Adafruit_MQTT_Subscribe* _device_subscription;
  Adafruit_MQTT_Publish* _announce;
  Adafruit_MQTT_Publish* _my_announce;
  String _my_announce_channel;

  Adafruit_MQTT_Publish* _input;
  Adafruit_MQTT_Publish* _my_input;
  String _my_input_channel;

  WiFiClient* _client;
Dave Murray-Rust's avatar
Dave Murray-Rust committed

Joe Revans's avatar
Joe Revans committed
  String capabilitiesJSON[50];

Dave Murray-Rust's avatar
Dave Murray-Rust committed
public:
Joe Revans's avatar
Joe Revans committed
  VizBlocks(char* id, char* ssid="VizBlocksNet", char* wifi_pass="VizBlocksAP",
    char* server="172.20.10.8",int port=1883);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  void command_callback(char *data, uint16_t len);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  void set_wifi(boolean v);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

emorgan's avatar
emorgan committed
   * Set up the VizBlocks node - WiFi, MQTT
  void init();
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  /*
   * Add a behaviour to the list of possible behaviours
   */
  void add(Behaviour *b);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  /*
   * This is the main loop. It should be called from within loop() - really
   * this function is the only thing you should need to call. It will manage
   * it's own delay, so you can call as often as possible.
   */
  void run();
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  /*
   * Read a command from the serial input and process it
   */
  void serial_command();
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  /*
   * Read a command from the serial input and process it. It only waits for
   * 50ms to allow other behaviours to continue.
   */
  void mqtt_command();
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  /*
   * Process a command. This means:
   * - split the command name from the arguments
Joe Revans's avatar
Joe Revans committed
   * - call process_command with the separated command and argument string
  String process(String input);
Joe Revans's avatar
Joe Revans committed

  String input_event(String input);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  /*
   * Process a command and its arguments. This means:
Joe Revans's avatar
Joe Revans committed
   * - look for a Behaviour with the right name
   * - if found, then call that behaviour with the arguments (which are still a single string)
   */
  String process_command(String command, String args);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  /*
  * Function to connect and reconnect as necessary to the MQTT server.
  */
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  // Should be called in the loop function and it will take care if connecting.
  void MQTT_connect();
Dave Murray-Rust's avatar
Dave Murray-Rust committed

  void generateCapabilitiesJSON();
Joe Revans's avatar
Joe Revans committed

  void announce(String doc);
Joe Revans's avatar
Joe Revans committed

  void announce_capabilities();
  void setID(char* id);
  char* getId();
Joe Revans's avatar
Joe Revans committed
};
Dave Murray-Rust's avatar
Dave Murray-Rust committed

Joe Revans's avatar
Joe Revans committed
 * These behaviours depend on VizBlocks class so they must be included after
 * it has been defined.
 */
Joe Revans's avatar
Joe Revans committed
#include "CommsBehaviours.h"
#include "ButtonBehaviours.h"
#include "PotentiometerBehaviours.h"
#include "RotaryEncoderBehaviours.h"
Dave Murray-Rust's avatar
Dave Murray-Rust committed

#endif