ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
ESP201.cpp
Go to the documentation of this file.
1 /*
2  ESP201.cpp - ESP3D command class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "../../include/esp3d_config.h"
21 #if defined (DIRECT_PIN_FEATURE)
22 #include "../commands.h"
23 #include "../esp3doutput.h"
24 #include "../settings_esp3d.h"
25 #include "../hal.h"
26 #include "../../modules/authentication/authentication_service.h"
27 //Get/Set pin value
28 //[ESP201]P<pin> V<value> [PULLUP=YES RAW=YES ANALOG=NO ANALOG_RANGE=255 CLEARCHANNELS=NO]pwd=<admin password>
29 //Range can be 255 / 1024 / 2047 / 4095 / 8191
30 bool Commands::ESP201(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
31 {
32  bool response = true;
33  String parameter;
34 #ifdef AUTHENTICATION_FEATURE
35  if (auth_type == LEVEL_GUEST) {
36  output->printERROR("Wrong authentication!", 401);
37  return false;
38  }
39 #else
40  (void)auth_type;
41 #endif //AUTHENTICATION_FEATURE
42 
43  //check if have pin
44  parameter = get_param (cmd_params, "P");
45  log_esp3d("Pin %s", parameter.c_str());
46  if (parameter == "") {
47  output->printERROR ("Invalid parameter!");
48  return false;
49  }
50  int pin = parameter.toInt();
51  //check pin is valid and not serial used pins
52  if ( Hal::is_pin_usable(pin)) {
53  bool isdigital = true;
54  parameter = get_param (cmd_params, "ANALOG=");
55  if (parameter == "YES") {
56  log_esp3d ("Set as analog");
57  isdigital=false;
58  parameter = get_param (cmd_params, "CLEARCHANNELS=");
59  if (parameter == "YES") {
61  }
62  }
63  //check if is set or get
64  parameter = get_param (cmd_params, "V");
65  //it is a get
66  if (parameter == "") {
67  //this is to not set pin mode
68  int value = 0;
69  if(isdigital) {
70  parameter = get_param (cmd_params, "RAW=");
71  if (parameter != "YES") {
72  parameter = get_param (cmd_params, "PULLUP=");
73  if (parameter != "YES") {
74  Hal::pinMode (pin, INPUT);
75  } else {
76  Hal::pinMode (pin, INPUT_PULLUP);
77  }
78  }
79  value = digitalRead (pin);
80  } else {
81  value = Hal::analogRead(pin);
82  }
83 
84  output->printMSG (String(value).c_str());
85  } else {
86  //it is a set
87  int value = parameter.toInt();
88  Hal::pinMode (pin, OUTPUT);
89  if (isdigital) {
90  //verify it is a '0' or a '1'
91  if ( (value == 0) || (value == 1) ) {
92  digitalWrite (pin, (value == 0) ? LOW : HIGH);
93  output->printMSG ("ok");
94  } else {
95  output->printERROR ("Invalid parameter!");
96  response = false;
97  }
98  } else {
99  int analog_range= 255;
100  parameter = get_param (cmd_params, "ANALOG_RANGE=");
101  if (parameter.length() > 0) {
102  analog_range = parameter.toInt();
103  }
104  if ( (value >= 0) || (value <= analog_range+1) ) {
105  Hal::analogWriteRange(analog_range);
106  Hal::analogWriteFreq(1000);
107  if (!Hal::analogWrite(pin, value)) {
108  output->printERROR ("Invalid value!");
109  response = false;
110  }
111  } else {
112  output->printERROR ("Invalid parameter!");
113  response = false;
114  }
115  }
116  }
117  } else {
118  output->printERROR ("Invalid parameter!");
119  response = false;
120  }
121  return response;
122 }
123 
124 #endif //DIRECT_PIN_FEATURE
Commands::get_param
const char * get_param(const char *cmd_params, const char *label)
Definition: commands.cpp:162
Hal::analogWriteRange
static void analogWriteRange(uint32_t range)
Definition: hal.cpp:186
LEVEL_GUEST
@ LEVEL_GUEST
Definition: authentication_service.h:26
Hal::is_pin_usable
static bool is_pin_usable(uint pin)
Definition: hal.cpp:268
Commands::ESP201
bool ESP201(const char *cmd_params, level_authenticate_type auth_level, ESP3DOutput *output)
Definition: ESP201.cpp:30
Hal::analogWriteFreq
static void analogWriteFreq(uint32_t freq)
Definition: hal.cpp:179
Hal::analogWrite
static bool analogWrite(uint8_t pin, uint value)
Definition: hal.cpp:141
level_authenticate_type
level_authenticate_type
Definition: authentication_service.h:25
ESP3DOutput::printMSG
size_t printMSG(const char *s, bool withNL=true)
Definition: esp3doutput.cpp:190
Hal::clearAnalogChannels
static void clearAnalogChannels()
Definition: hal.cpp:57
Hal::analogRead
static int analogRead(uint8_t pin)
Definition: hal.cpp:112
ESP3DOutput::printERROR
size_t printERROR(const char *s, int code_error=200)
Definition: esp3doutput.cpp:247
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
Hal::pinMode
static void pinMode(uint8_t pin, uint8_t mode)
Definition: hal.cpp:69
ESP3DOutput
Definition: esp3doutput.h:48