Skip to content

Commit fab5104

Browse files
committed
use User-defined literals for kHz, MHz, GHz, kBit, MBit, GBit, kB, MB and GB
see #145
1 parent cf9da93 commit fab5104

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

cores/esp8266/Arduino.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,48 @@ long map(long, long, long, long, long);
186186

187187
#include "pins_arduino.h"
188188

189+
/**
190+
* User-defined Literals
191+
* usage:
192+
*
193+
* uint32_t = test = 10_MHz; // --> 10000000
194+
*/
195+
#ifdef __cplusplus
196+
unsigned long long operator"" _kHz(unsigned long long x) {
197+
return x * 1000;
198+
}
199+
200+
unsigned long long operator"" _MHz(unsigned long long x) {
201+
return x * 1000 * 1000;
202+
}
203+
204+
unsigned long long operator"" _GHz(unsigned long long x) {
205+
return x * 1000 * 1000 * 1000;
206+
}
207+
208+
unsigned long long operator"" _kBit(unsigned long long x) {
209+
return x * 1024;
210+
}
211+
212+
unsigned long long operator"" _MBit(unsigned long long x) {
213+
return x * 1024 * 1024;
214+
}
215+
216+
unsigned long long operator"" _GBit(unsigned long long x) {
217+
return x * 1024 * 1024 * 1024;
218+
}
219+
220+
unsigned long long operator"" _kB(unsigned long long x) {
221+
return x * 1024;
222+
}
223+
224+
unsigned long long operator"" _MB(unsigned long long x) {
225+
return x * 1024 * 1024;
226+
}
227+
228+
unsigned long long operator"" _GB(unsigned long long x) {
229+
return x * 1024 * 1024 * 1024;
230+
}
231+
#endif
232+
189233
#endif

cores/esp8266/Esp.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ extern "C" {
2424
#include "user_interface.h"
2525
}
2626

27-
#define kHz (1000L)
28-
#define MHz (1000L*kHz)
29-
#define GHz (1000L*MHz)
30-
31-
#define kBit (1024L)
32-
#define MBit (1024L*kBit)
33-
#define GBit (1024L*MBit)
34-
35-
#define kB (1024L)
36-
#define MB (1024L*kB)
37-
#define GB (1024L*MB)
38-
3927
//extern "C" void ets_wdt_init(uint32_t val);
4028
extern "C" void ets_wdt_enable(void);
4129
extern "C" void ets_wdt_disable(void);
@@ -134,15 +122,15 @@ uint32_t EspClass::getFlashChipSize(void)
134122
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
135123
switch((bytes[3] & 0xf0) >> 4) {
136124
case 0x0: // 4 Mbit (512KB)
137-
return (512 * kB);
125+
return (512_kB);
138126
case 0x1: // 2 MBit (256KB)
139-
return (256 * kB);
127+
return (256_kB);
140128
case 0x2: // 8 MBit (1MB)
141-
return (1 * MB);
129+
return (1_MB);
142130
case 0x3: // 16 MBit (2MB)
143-
return (2 * MB);
131+
return (2_MB);
144132
case 0x4: // 32 MBit (4MB)
145-
return (4 * MB);
133+
return (4_MB);
146134
default: // fail?
147135
return 0;
148136
}
@@ -158,13 +146,13 @@ uint32_t EspClass::getFlashChipSpeed(void)
158146
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
159147
switch(bytes[3] & 0x0F) {
160148
case 0x0: // 40 MHz
161-
return (40 * MHz);
149+
return (40_MHz);
162150
case 0x1: // 26 MHz
163-
return (26 * MHz);
151+
return (26_MHz);
164152
case 0x2: // 20 MHz
165-
return (20 * MHz);
153+
return (20_MHz);
166154
case 0xf: // 80 MHz
167-
return (80 * MHz);
155+
return (80_MHz);
168156
default: // fail?
169157
return 0;
170158
}

0 commit comments

Comments
 (0)