ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
hal.cpp
Go to the documentation of this file.
1 /*
2  hal.cpp - ESP3D hal 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 
21 #include "../include/esp3d_config.h"
22 
23 #if defined(ARDUINO_ARCH_ESP8266)
24 #include "ESP8266WiFi.h"
25 #endif //ARDUINO_ARCH_ESP8266
26 #if defined(ARDUINO_ARCH_ESP32)
27 #include "WiFi.h"
28 #ifdef __cplusplus
29 extern "C" {
30 #endif //__cplusplus
31 esp_err_t esp_task_wdt_reset();
32 #ifdef __cplusplus
33 }
34 #endif //__cplusplus
35 #endif //ARDUINO_ARCH_ESP32
36 
37 #include "esp3doutput.h"
38 
39 #if defined(ARDUINO_ARCH_ESP32)
40 int ChannelAttached2Pin[16]= {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
41 #endif //ARDUINO_ARCH_ESP32
42 
43 uint32_t Hal::_analogWriteRange = 255;
44 uint32_t Hal::_analogWriteFreq = 1000;
45 
46 //Contructor
48 {
49 
50 }
51 
52 //Destructor
54 {
55 }
56 
58 {
59 #ifdef ARDUINO_ARCH_ESP32
60  for (uint8_t p = 0; p < 16; p++) {
61  if(ChannelAttached2Pin[p] != -1) {
62  ledcDetachPin(ChannelAttached2Pin[p]);
63  ChannelAttached2Pin[p] = -1;
64  }
65  }
66 #endif //ARDUINO_ARCH_ESP32
67 }
68 
69 void Hal::pinMode(uint8_t pin, uint8_t mode)
70 {
71 #if defined (ARDUINO_ARCH_ESP8266)
72  if ((pin == 16) && (mode == INPUT_PULLUP)) {
73  pinMode(pin, INPUT_PULLDOWN_16);
74  return;
75  }
76 #endif
77  pinMode(pin, mode);
78 }
79 
80 void Hal::toneESP(uint8_t pin, unsigned int frequency, unsigned int duration, bool sync)
81 {
82 #if defined(ARDUINO_ARCH_ESP8266)
83  (void) sync; //useless for esp8266
84  tone(pin, frequency, duration);
85 #endif //ARDUINO_ARCH_ESP8266
86 #if defined(ARDUINO_ARCH_ESP32)
87  int channel = getAnalogWriteChannel(pin);
88  if (channel != -1) {
89  ledcAttachPin(pin, channel);
90  ledcWriteTone(channel,frequency);
91  if (sync) {
92  wait(duration);
93  ledcWriteTone(pin,0);
94  }
95  }
96 
97 #endif //ARDUINO_ARCH_ESP32
98 }
99 void Hal::no_tone(uint8_t pin)
100 {
101 #if defined(ARDUINO_ARCH_ESP8266)
102  tone(pin, 0, 0);
103 #endif //ARDUINO_ARCH_ESP8266
104 #if defined(ARDUINO_ARCH_ESP32)
105  int channel = getAnalogWriteChannel(pin);
106  if (channel != -1) {
107  ledcWrite(channel, 0);
108  }
109 #endif //ARDUINO_ARCH_ESP32
110 }
111 
112 int Hal::analogRead(uint8_t pin)
113 {
114 #ifdef ARDUINO_ARCH_ESP8266 //only one ADC on ESP8266 A0
115  (void)pin;
116  return analogRead (A0);
117 #else
118  return analogRead (pin);
119 #endif
120 }
121 
122 #if defined(ARDUINO_ARCH_ESP32)
123 int Hal::getAnalogWriteChannel(uint8_t pin)
124 {
125  for (uint8_t p = 0; p < 16; p++) {
126  if(ChannelAttached2Pin[p] == pin) {
127  return p;
128  }
129  }
130  for (uint8_t p = 0; p < 16; p++) {
131  if(ChannelAttached2Pin[p] == -1) {
132  ChannelAttached2Pin[p] = pin;
133  return p;
134  }
135  }
136 
137  return -1;
138 }
139 #endif //ARDUINO_ARCH_ESP32
140 
141 bool Hal::analogWrite(uint8_t pin, uint value)
142 {
143  if (value > (_analogWriteRange-1)) {
144  return false;
145  }
146 #ifdef ARDUINO_ARCH_ESP8266
147  analogWrite(pin, value);
148 #endif //ARDUINO_ARCH_ESP8266
149 #ifdef ARDUINO_ARCH_ESP32
150  int channel = getAnalogWriteChannel(pin);
151  if (channel==-1) {
152  return false;
153  }
154  uint8_t resolution = 0;
155  switch(_analogWriteRange) {
156  case 8191:
157  resolution=13;
158  break;
159  case 1024:
160  resolution=10;
161  break;
162  case 2047:
163  resolution=11;
164  break;
165  case 4095:
166  resolution=12;
167  break;
168  default:
169  resolution=8;
170  _analogWriteRange = 255;
171  break;
172  }
173  ledcSetup(channel, _analogWriteFreq, resolution);
174  ledcAttachPin(pin, channel);
175  ledcWrite(channel, value);
176 #endif //ARDUINO_ARCH_ESP32
177  return true;
178 }
179 void Hal::analogWriteFreq(uint32_t freq)
180 {
181  _analogWriteFreq = freq;
182 #ifdef ARDUINO_ARCH_ESP8266
183  analogWriteFreq(_analogWriteFreq);
184 #endif //ARDUINO_ARCH_ESP8266
185 }
186 void Hal::analogWriteRange(uint32_t range)
187 {
188  _analogWriteRange = range;
189 #ifdef ARDUINO_ARCH_ESP8266
190  analogWriteRange(_analogWriteRange);
191 #endif //ARDUINO_ARCH_ESP8266
192 }
193 
194 //Setup
196 {
197  //Clear all wifi state
198  WiFi.persistent(false);
199  WiFi.disconnect(true);
200  WiFi.enableSTA (false);
201  WiFi.enableAP (false);
202  WiFi.mode (WIFI_OFF);
203  return true;
204 }
205 
206 //End ESP3D
207 void Hal::end()
208 {
209 #if defined(ARDUINO_ARCH_ESP32)
211 #endif //ARDUINO_ARCH_ESP32
212 }
213 
214 //Watchdog feeder
215 void Hal::wdtFeed()
216 {
217 #ifdef ARDUINO_ARCH_ESP8266
218  ESP.wdtFeed();
219 #endif //ARDUINO_ARCH_ESP8266
220 #ifdef ARDUINO_ARCH_ESP32
221  void esp_task_wdt_feed();
222 #endif //ARDUINO_ARCH_ESP32
223 }
224 
225 //wait function
226 void Hal::wait (uint32_t milliseconds)
227 {
228 #if defined(ASYNCWEBSERVER) || defined(ARDUINO_ARCH_ESP32)
229  uint32_t timeout = millis();
230  while ( (millis() - timeout) < milliseconds) {
231  wdtFeed();
232  }
233 #else // !(ASYNCWEBSERVER + ARDUINO_ARCH_ESP32)
234  delay(milliseconds);
235 #endif // !ASYNCWEBSERVER & !ARDUINO_ARCH_ESP32
236 }
237 
238 uint16_t Hal::getChipID()
239 {
240 #ifdef ARDUINO_ARCH_ESP8266
241  return ESP.getChipId();
242 #endif //ARDUINO_ARCH_ESP8266
243 #ifdef ARDUINO_ARCH_ESP32
244  return (uint16_t) (ESP.getEfuseMac() >> 32);
245 #endif //ARDUINO_ARCH_ESP32
246 }
247 
249 {
250 #ifdef ARDUINO_ARCH_ESP8266
251  return false;
252 #endif //ARDUINO_ARCH_ESP8266
253 #ifdef ARDUINO_ARCH_ESP32
254  return true;
255 #endif //ARDUINO_ARCH_ESP32
256 }
257 
259 {
260 #ifdef ARDUINO_ARCH_ESP8266
261  return 0.0;
262 #endif //ARDUINO_ARCH_ESP8266
263 #ifdef ARDUINO_ARCH_ESP32
264  return temperatureRead();
265 #endif //ARDUINO_ARCH_ESP32
266 }
267 
268 bool Hal::is_pin_usable(uint pin)
269 {
270 #ifdef ARDUINO_ARCH_ESP8266
271  if ((pin <= 5) || ((pin >= 12) && (pin <= 16))) {
272  return true;
273  } else {
274  return false;
275  }
276 #endif //ARDUINO_ARCH_ESP8266
277 #ifdef ARDUINO_ARCH_ESP32
278  if ((pin <= 5) || ((pin >= 12) && (pin <= 39))) {
279  return true;
280  } else {
281  return false;
282  }
283 #endif //ARDUINO_ARCH_ESP32
284 }
Hal::wait
static void wait(uint32_t milliseconds)
Definition: hal.cpp:226
Hal::Hal
Hal()
Definition: hal.cpp:47
Hal::end
static void end()
Definition: hal.cpp:207
Hal::analogWriteRange
static void analogWriteRange(uint32_t range)
Definition: hal.cpp:186
Hal::is_pin_usable
static bool is_pin_usable(uint pin)
Definition: hal.cpp:268
Hal::temperature
static float temperature()
Definition: hal.cpp:258
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
Hal::getChipID
static uint16_t getChipID()
Definition: hal.cpp:238
Hal::begin
static bool begin()
Definition: hal.cpp:195
Hal::~Hal
~Hal()
Definition: hal.cpp:53
Hal::clearAnalogChannels
static void clearAnalogChannels()
Definition: hal.cpp:57
Hal::analogRead
static int analogRead(uint8_t pin)
Definition: hal.cpp:112
Hal::has_temperature_sensor
static bool has_temperature_sensor()
Definition: hal.cpp:248
Hal::pinMode
static void pinMode(uint8_t pin, uint8_t mode)
Definition: hal.cpp:69
Hal::toneESP
static void toneESP(uint8_t pin, unsigned int frequency, unsigned int duration, bool sync=true)
Definition: hal.cpp:80
esp3doutput.h
Hal::no_tone
static void no_tone(uint8_t pin)
Definition: hal.cpp:99