Skip to content
Snippets Groups Projects
VizBlocks.h 3.63 KiB
Newer Older
Joe Revans's avatar
Joe Revans committed
#ifndef VizBlock_H
#define VizBlock_H
#include <Arduino.h>
Dave Murray-Rust's avatar
Dave Murray-Rust committed
#include "Behaviours.h"
Joe Revans's avatar
Joe Revans committed
#include "NameDictionary.h"
Dave Murray-Rust's avatar
Dave Murray-Rust committed
#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"

/** \file VizBlocks.h
    \brief Contains everything you need to get started.

    This file includes all of the libraries and dependancies needed to
		start experimenting with the framework.
*/

/** \class VizBlock
    \brief The main VizBlock class.

    This class contains all of the connection information for the WiFi network
		and Node-RED. It checks for commands recieved via serial/mqtt and executes
		behaviours in reponse.
*/
Joe Revans's avatar
Joe Revans committed
class VizBlock
Matthew's avatar
Matthew committed
{
	char* _ssid;
	char* _wifi_pass;
	char* _id;
	char* _server;
	int _port;

	static const int NUM_BACKGROUND_BEHAVIOURS = 5;
Matthew's avatar
Matthew committed
	BehaviourTable _behaviours;
	Behaviour* _active = nullptr;
	Behaviour* _background[NUM_BACKGROUND_BEHAVIOURS];
Matthew's avatar
Matthew committed
	int _loop_time = 5;
emorgan's avatar
emorgan committed
	unsigned long _wifi_timeout; // How long to wait for wifi until we go into deep sleep
	unsigned long _wifi_timeout_initialMillis;
Matthew's avatar
Matthew committed
	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;
Matthew's avatar
Matthew committed
	boolean _wifi;

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

Dave Murray-Rust's avatar
Dave Murray-Rust committed
public:
Joe Revans's avatar
Joe Revans committed
	VizBlock(char* id, char* ssid="VizBlockNet", char* wifi_pass="VizBlocksAP",
Matthew's avatar
Matthew committed
	          char* server="172.20.10.8",int port=1883);
Matthew's avatar
Matthew committed
	/**
		 Turns the block's wifi on or off.
Matthew's avatar
Matthew committed
	 */
Matthew's avatar
Matthew committed
	void set_wifi(boolean v);
Dave Murray-Rust's avatar
Dave Murray-Rust committed

Matthew's avatar
Matthew committed
	/**
	 * Set up the block's WiFi and MQTT.
Matthew's avatar
Matthew committed
	 */
	void init();
Dave Murray-Rust's avatar
Dave Murray-Rust committed

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

Matthew's avatar
Matthew committed
	/**
Matthew's avatar
Matthew 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

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

Matthew's avatar
Matthew committed
	/**
Matthew's avatar
Matthew 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

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

Matthew's avatar
Matthew committed
	/**
Matthew's avatar
Matthew committed
	 * Process a command and its arguments. This means:
	 * - 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

Matthew's avatar
Matthew committed
	/**
Matthew's avatar
Matthew committed
	 * Function to connect and reconnect as necessary to the MQTT server.
	 */
	void MQTT_connect();
emorgan's avatar
emorgan committed
	/**
	 * Function to connect and reconnect as necessary to the WIFI.
	 */
	void wifi_connect();

Matthew's avatar
Matthew committed
	/**
	   Generates an array json formatted string containing information about
		 each of the behaviours the block can perform.
Matthew's avatar
Matthew committed
	 */
Matthew's avatar
Matthew committed
	void generateCapabilitiesJSON();
Matthew's avatar
Matthew committed
	/**
	   Publishes a message to the block's mqtt announcement channel.
Matthew's avatar
Matthew committed
	 */
Matthew's avatar
Matthew committed
	void announce(String doc);
Matthew's avatar
Matthew committed
	/**
	   Loops through the block's behaviours and announces each one over mqtt/
Matthew's avatar
Matthew committed
	 */
Matthew's avatar
Matthew committed
	void announce_capabilities();
Matthew's avatar
Matthew committed
	/**
	   Sets the ID of the block.
Matthew's avatar
Matthew committed
	 */
Matthew's avatar
Matthew committed
	void setID(char* id);
Matthew's avatar
Matthew committed
	/**
	   Returns the ID of the block.
Matthew's avatar
Matthew committed
	 */
Matthew's avatar
Matthew committed
	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 VizBlock 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