Skip to content
Snippets Groups Projects
web.cpp 5.87 KiB
Newer Older
David Gauchard's avatar
David Gauchard committed

// This is the ESPUI example
// it does nothing more yet and is not connected with the stepper motor code

#include <ESPUI.h>              // https://github.com/s00500/ESPUI

#include "common.h"
guigur's avatar
guigur committed
#include "syringe.h"
David Gauchard's avatar
David Gauchard committed

uint16_t status;
uint16_t button1;
uint16_t millisLabelId;
uint16_t switchOne;

David Gauchard's avatar
David Gauchard committed

void numberCall( Control* sender, int type ) {
  Serial.println( sender->value );
}

void slider(Control *sender, int type) {
  Serial.print("Slider: ID: ");
  Serial.print(sender->id);
  Serial.print(", Value: ");
  Serial.println(sender->value);
}

void buttonCallback(Control *sender, int type) {
  switch (type) {
  case B_DOWN:
    Serial.println("Button DOWN");
David Gauchard's avatar
David Gauchard committed
    //syringe.push(1, 1);
David Gauchard's avatar
David Gauchard committed
    break;

  case B_UP:
    Serial.println("Button UP");
    break;
  }
}

void buttonExample(Control *sender, int type) {
  switch (type) {
  case B_DOWN:
    Serial.println("Status: Start");
    ESPUI.updateControlValue(status, "Start");

    ESPUI.getControl(button1)->color = ControlColor::Carrot;
    ESPUI.updateControl(button1);
    break;

  case B_UP:
    Serial.println("Status: Stop");
    ESPUI.updateControlValue(status, "Stop");

    ESPUI.getControl(button1)->color = ControlColor::Peterriver;
    ESPUI.updateControl(button1);
    break;
  }
}


void switchExample(Control *sender, int value) {
  switch (value) {
  case S_ACTIVE:
Jean Alinei's avatar
Jean Alinei committed
    Serial.print("Tirer:");
David Gauchard's avatar
David Gauchard committed
    break;

  case S_INACTIVE:
Jean Alinei's avatar
Jean Alinei committed
    Serial.print("Pousser");
David Gauchard's avatar
David Gauchard committed
    break;
  }

  Serial.print(" ");
  Serial.println(sender->id);
}

void selectExample(Control *sender, int value) {
  Serial.print("Select: ID: ");
  Serial.print(sender->id);
  Serial.print(", Value: ");
  Serial.println(sender->value);
}

David Gauchard's avatar
David Gauchard committed

void webSetup ()
{
David Gauchard's avatar
David Gauchard committed
    //ESPUI.setVerbosity(Verbosity::VerboseJSON);
David Gauchard's avatar
David Gauchard committed

    status = ESPUI.addControl(ControlType::Label, "Status:", "Stop", ControlColor::Turquoise);

Jean Alinei's avatar
Jean Alinei committed
    uint16_t select1 = ESPUI.addControl(ControlType::Select, "Select the seringe volume:", "", ControlColor::Alizarin, Control::noParent, &selectExample);
David Gauchard's avatar
David Gauchard committed

Jean Alinei's avatar
Jean Alinei committed
    ESPUI.addControl(ControlType::Option, "1mL", "Opt1", ControlColor::Alizarin, select1);
    ESPUI.addControl(ControlType::Option, "2mL", "Opt2", ControlColor::Alizarin, select1);
    ESPUI.addControl(ControlType::Option, "5mL", "Opt3", ControlColor::Alizarin, select1);
    ESPUI.addControl(ControlType::Option, "10mL", "Opt3", ControlColor::Alizarin, select1);
    ESPUI.addControl(ControlType::Option, "20mL", "Opt3", ControlColor::Alizarin, select1);

guigur's avatar
guigur committed
    ESPUI.addControl(ControlType::Slider, "Select seringe diameter (mm)", "30", ControlColor::Alizarin, Control::noParent, &slider);
David Gauchard's avatar
David Gauchard committed


    millisLabelId = ESPUI.addControl(ControlType::Label, "Millis:", "0", ControlColor::Emerald, Control::noParent);
    switchOne = ESPUI.addControl(ControlType::Switcher, "Switch one", "", ControlColor::Alizarin, Control::noParent, &switchExample);
guigur's avatar
guigur committed
    uint16_t constantFlow = ESPUI.addControl(ControlType::Slider, "Select a constant flow (mL/s)", "30", ControlColor::Alizarin, Control::noParent, &slider);
    ESPUI.addControl(ControlType::Min, "", "0", ControlColor::None, constantFlow);
guigur's avatar
guigur committed
    ESPUI.addControl(ControlType::Max, "", "30", ControlColor::None, constantFlow);
    //uint16_t displacementDistance = ESPUI.addControl(ControlType::Slider, "Select a seringe displacement distance (mm)", "100", ControlColor::Alizarin, Control::noParent, &slider);
    uint16_t displacementDistance = ESPUI.addControl(ControlType::Slider, "Select a seringe displacement distance (mm)", "100", ControlColor::Alizarin, Control::noParent,
        [](Control *sender, int type)
        {
            Serial.printf("val=%s\n", sender->value.c_str()); 
            Syringe::Syringe_configuration_t conf = syringe.configuration();
            conf.volume_value = syringe.mmToMm3(atof(sender->value.c_str()));
            syringe.configureSyringe(conf);
        });
guigur's avatar
guigur committed
    ESPUI.addControl(ControlType::Min, "", "0", ControlColor::None, displacementDistance);
    ESPUI.addControl(ControlType::Max, "", "100", ControlColor::None, displacementDistance);

    uint16_t displacementTime = ESPUI.addControl(ControlType::Slider, "Select a seringe displacement time (s)", "100", ControlColor::Alizarin, Control::noParent, &slider);
    ESPUI.addControl(ControlType::Min, "", "0", ControlColor::None, displacementTime);
    ESPUI.addControl(ControlType::Max, "", "100", ControlColor::None, displacementTime);
Jean Alinei's avatar
Jean Alinei committed

    //button1 = ESPUI.addControl(ControlType::Button, "Actuate", "Press", ControlColor::Peterriver, Control::noParent, &buttonCallback);
    button1 = ESPUI.addControl(ControlType::Button, "Actuate", "Press", ControlColor::Peterriver, Control::noParent,
        [](Control *sender, int type)
        {
            if (type == B_UP)
                syringe.fill();
        });
David Gauchard's avatar
David Gauchard committed

    /*
     * .begin loads and serves all files from PROGMEM directly.
     * If you want to serve the files from SPIFFS use ESPUI.beginSPIFFS
     * (.prepareFileSystem has to be run in an empty sketch before)
     */

    // Enable this option if you want sliders to be continuous (update during move) and not discrete (update on stop)
    // ESPUI.sliderContinuous = true;

    /*
     * Optionally you can use HTTP BasicAuth. Keep in mind that this is NOT a
     * SECURE way of limiting access.
     * Anyone who is able to sniff traffic will be able to intercept your password
     * since it is transmitted in cleartext. Just add a string as username and
     * password, for example begin("ESPUI Control", "username", "password")
     */

    ESPUI.begin("ESPUI Control");
}


void webLoop ()
{
    ///////////////////
    // ESPUI management

    static decltype(millis()) oldTime = 0;
David Gauchard's avatar
David Gauchard committed
    static bool testSwitchState = false;
    if (millis() - oldTime > 5000)
David Gauchard's avatar
David Gauchard committed
    {
        ESPUI.updateControlValue(millisLabelId, String(millis()));
        testSwitchState = !testSwitchState;
        ESPUI.updateControlValue(switchOne, testSwitchState ? "1" : "0");
        oldTime = millis();