[code] keep only CPython equivalent functions in utime module

This commit is contained in:
Damien Nicolet
2018-11-10 00:00:03 +01:00
parent 4640b67bc8
commit 24630011b1
24 changed files with 53 additions and 145 deletions

View File

@@ -257,7 +257,7 @@ void AppsContainer::displayExamModePopUp(bool activate) {
void AppsContainer::shutdownDueToLowBattery() {
while (Ion::Battery::level() == Ion::Battery::Charge::EMPTY) {
m_emptyBatteryWindow.redraw(true);
Ion::mssleep(3000);
Ion::msleep(3000);
Ion::Power::suspend();
}
window()->redraw(true);

View File

@@ -96,10 +96,5 @@ PythonUniform = "Floating point number in [a,b]"
PythonImportFromTime = "Import time module"
PythonImportTime = "Import time module"
PythonTimeFunction = "time module function prefix"
PythonTimeSleepMs = "Sleep for the given duration"
PythonTimeSleep = "Sleep for the given duration"
PythonTimeSleepUs = "Sleep for the given duration"
PythonTimeTicksAdd = "Offset ticks value"
PythonTimeTicksDiff = "Measure ticks difference"
PythonTimeTicksMs = "Increasing millisecond counter"
PythonTimeTicksUs = "Increasing microsecond counter"
PythonTimeSleep = "Sleep for the given seconds"
PythonTimeClock = "Increasing seconds counter"

View File

@@ -96,10 +96,5 @@ PythonUniform = "Floating point number in [a,b]"
PythonImportFromTime = "Import time module"
PythonImportTime = "Import time module"
PythonTimeFunction = "time module function prefix"
PythonTimeSleepMs = "Sleep for the given duration"
PythonTimeSleep = "Sleep for the given duration"
PythonTimeSleepUs = "Sleep for the given duration"
PythonTimeTicksAdd = "Offset ticks value"
PythonTimeTicksDiff = "Measure ticks difference"
PythonTimeTicksMs = "Increasing millisecond counter"
PythonTimeTicksUs = "Increasing microsecond counter"
PythonTimeSleep = "Sleep for the given seconds"
PythonTimeClock = "Increasing seconds counter"

View File

@@ -96,10 +96,5 @@ PythonUniform = "Floating point number in [a,b]"
PythonImportFromTime = "Import time module"
PythonImportTime = "Import time module"
PythonTimeFunction = "time module function prefix"
PythonTimeSleepMs = "Sleep for the given duration"
PythonTimeSleep = "Sleep for the given duration"
PythonTimeSleepUs = "Sleep for the given duration"
PythonTimeTicksAdd = "Offset ticks value"
PythonTimeTicksDiff = "Measure ticks difference"
PythonTimeTicksMs = "Increasing millisecond counter"
PythonTimeTicksUs = "Increasing microsecond counter"
PythonTimeSleep = "Sleep for the given seconds"
PythonTimeClock = "Increasing seconds counter"

View File

@@ -96,10 +96,5 @@ PythonUniform = "Nombre décimal dans [a,b]"
PythonImportFromTime = "Importation du module time"
PythonImportTime = "Importation du module time"
PythonTimeFunction = "Prefixe fonction du module time"
PythonTimeSleep = "Effectuer une pause"
PythonTimeSleepMs = "Effectuer une pause"
PythonTimeSleepUs = "Effectuer une pause"
PythonTimeTicksAdd = "Ajouter une valeur à un compteur"
PythonTimeTicksDiff = "Différence entre compteurs"
PythonTimeTicksMs = "Compteur croissant (millisecondes)"
PythonTimeTicksUs = "Compteur croissant (microsecondes)"
PythonTimeSleep = "Effectuer une pause (secondes)"
PythonTimeClock = "Compteur croissant (secondes)"

View File

@@ -96,10 +96,5 @@ PythonUniform = "Floating point number in [a,b]"
PythonImportFromTime = "Import time module"
PythonImportTime = "Import time module"
PythonTimeFunction = "time module function prefix"
PythonTimeSleepMs = "Sleep for the given duration"
PythonTimeSleep = "Sleep for the given duration"
PythonTimeSleepUs = "Sleep for the given duration"
PythonTimeTicksAdd = "Offset ticks value"
PythonTimeTicksDiff = "Measure ticks difference"
PythonTimeTicksMs = "Increasing millisecond counter"
PythonTimeTicksUs = "Increasing microsecond counter"
PythonTimeSleep = "Sleep for the given seconds"
PythonTimeClock = "Increasing seconds counter"

View File

@@ -108,11 +108,7 @@ PythonCommandImportFromTime = "from time import *"
PythonCommandImportTime = "import time"
PythonCommandTimeFunction = "time.function"
PythonCommandTimeFunctionWithoutArg = "time.ø"
PythonCommandTimeSleepMs = "sleep_ms(milliseconds)"
PythonCommandTimeSleep = "sleep(seconds)"
PythonCommandTimeSleepUs = "sleep_us(microseconds)"
PythonCommandTimeTicksAdd = "ticks_add(ticks,delta)"
PythonCommandTimeTicksDiff = "ticks_diff(end,start)"
PythonCommandTimeTicksMs = "ticks_ms()"
PythonCommandTimeTicksUs = "ticks_us()"
PythonCommandTimeClock = "clock()"
PythonCommandTimeTime = "time()"
TimeModule = "time"

View File

@@ -21,7 +21,7 @@ static constexpr int loopsAndTestsChildrenCount = 4;
static constexpr int menuChildrenCount = 4;
static constexpr int modulesChildrenCount = 5;
static constexpr int whileLoopChildrenCount = 1;
static constexpr int TimeModuleChildrenCount = 10;
static constexpr int TimeModuleChildrenCount = 6;
const ToolboxMessageTree forLoopChildren[forLoopChildrenCount] = {
@@ -129,12 +129,8 @@ const ToolboxMessageTree TimeModuleChildren[TimeModuleChildrenCount] = {
ToolboxMessageTree(I18n::Message::PythonCommandImportFromTime, I18n::Message::PythonImportFromTime, I18n::Message::PythonCommandImportFromTime),
ToolboxMessageTree(I18n::Message::PythonCommandTimeFunction, I18n::Message::PythonTimeFunction, I18n::Message::PythonCommandTimeFunctionWithoutArg),
ToolboxMessageTree(I18n::Message::PythonCommandTimeSleep, I18n::Message::PythonTimeSleep, I18n::Message::PythonCommandTimeSleep),
ToolboxMessageTree(I18n::Message::PythonCommandTimeSleepMs, I18n::Message::PythonTimeSleepMs, I18n::Message::PythonCommandTimeSleepMs),
ToolboxMessageTree(I18n::Message::PythonCommandTimeSleepUs, I18n::Message::PythonTimeSleepUs, I18n::Message::PythonCommandTimeSleepUs),
ToolboxMessageTree(I18n::Message::PythonCommandTimeTicksMs, I18n::Message::PythonTimeTicksMs, I18n::Message::PythonCommandTimeTicksMs),
ToolboxMessageTree(I18n::Message::PythonCommandTimeTicksUs, I18n::Message::PythonTimeTicksUs, I18n::Message::PythonCommandTimeTicksUs),
ToolboxMessageTree(I18n::Message::PythonCommandTimeTicksDiff, I18n::Message::PythonTimeTicksDiff, I18n::Message::PythonCommandTimeTicksDiff),
ToolboxMessageTree(I18n::Message::PythonCommandTimeTicksAdd, I18n::Message::PythonTimeTicksAdd, I18n::Message::PythonCommandTimeTicksAdd),
ToolboxMessageTree(I18n::Message::PythonCommandTimeClock, I18n::Message::PythonTimeClock, I18n::Message::PythonCommandTimeClock),
ToolboxMessageTree(I18n::Message::PythonCommandTimeTime, I18n::Message::PythonTimeClock, I18n::Message::PythonCommandTimeTime)
};
const ToolboxMessageTree CMathModuleChildren[CMathModuleChildrenCount] = {

View File

@@ -74,8 +74,7 @@ _mp_execute_bytecode \
_mp_hal_input \
_mp_import_name \
_mp_parse_compile_execute \
_mssleep \
_ussleep
_msleep
EMTERPRETIFY_WHITELIST = $(foreach sym,$(EMSCRIPTEN_ASYNC_SYMBOLS),"$(sym)",)END
EMFLAGS = -s PRECISE_F32=1 -s EMTERPRETIFY=1 -s EMTERPRETIFY_ASYNC=1 -s EMTERPRETIFY_WHITELIST='[$(EMTERPRETIFY_WHITELIST:,END=)]'

View File

@@ -4,6 +4,7 @@
#ifdef __cplusplus
#define EXTERNC extern "C"
namespace Ion {
void usleep(long us);
#else
#define EXTERNC
#endif
@@ -11,8 +12,7 @@ namespace Ion {
EXTERNC uint32_t millis();
EXTERNC uint32_t micros();
EXTERNC void mssleep(long ms);
EXTERNC void ussleep(long us);
EXTERNC void msleep(long ms);
#ifdef __cplusplus
}

View File

@@ -1,8 +1,8 @@
#include <stdint.h>
#include <ion.h>
void Ion::mssleep(long ms) {
void Ion::msleep(long ms) {
}
void Ion::ussleep(long us) {
void Ion::usleep(long us) {
}

View File

@@ -45,12 +45,12 @@ void shutdown() {
void suspend() {
GPIOC.ODR()->set(6, false);
mssleep(3); // Might not need to be blocking
msleep(3); // Might not need to be blocking
}
void resume() {
GPIOC.ODR()->set(6, true);
ussleep(50);
usleep(50);
uint8_t level = sLevel;
sLevel = 0xF;
setLevel(level);
@@ -74,9 +74,9 @@ uint8_t level() {
void sendPulses(int n) {
for (int i=0; i<n; i++) {
GPIOC.ODR()->set(6, false);
ussleep(20);
usleep(20);
GPIOC.ODR()->set(6, true);
ussleep(20);
usleep(20);
}
}

View File

@@ -12,7 +12,7 @@ void Suspend(const char * input) {
return;
}
reply(sOK);
Ion::mssleep(100);
Ion::msleep(100);
Ion::Power::suspend();
}

View File

@@ -63,7 +63,7 @@ void shutdown() {
bool peerConnected() {
RxPin.group().PUPDR()->setPull(RxPin.pin(), GPIO::PUPDR::Pull::Down);
RxPin.group().MODER()->setMode(RxPin.pin(), GPIO::MODER::Mode::Input);
mssleep(1);
msleep(1);
bool result = RxPin.group().IDR()->get(RxPin.pin());
RxPin.group().PUPDR()->setPull(RxPin.pin(), GPIO::PUPDR::Pull::None);
RxPin.group().MODER()->setMode(RxPin.pin(), GPIO::MODER::Mode::AlternateFunction);

View File

@@ -24,16 +24,16 @@ extern "C" {
// Public Ion methods
/* TODO: The delay methods 'mssleep' and 'ussleep' are currently dependent on the
/* TODO: The delay methods 'msleep' and 'usleep' are currently dependent on the
* optimizations chosen by the compiler. To prevent that and to gain in
* precision, we could use the controller cycle counter (Systick). */
void Ion::mssleep(long ms) {
void Ion::msleep(long ms) {
for (volatile long i=0; i<8852*ms; i++) {
__asm volatile("nop");
}
}
void Ion::ussleep(long us) {
void Ion::usleep(long us) {
for (volatile long i=0; i<9*us; i++) {
__asm volatile("nop");
}

View File

@@ -162,7 +162,7 @@ void initGPIO() {
TearingEffectPin.group().MODER()->setMode(TearingEffectPin.pin(), GPIO::MODER::Mode::Input);
TearingEffectPin.group().PUPDR()->setPull(TearingEffectPin.pin(), GPIO::PUPDR::Pull::None);
mssleep(120);
msleep(120);
}
@@ -247,10 +247,10 @@ void shutdownFSMC() {
void initPanel() {
send_command(Command::Reset);
mssleep(5);
msleep(5);
send_command(Command::SleepOut);
mssleep(5);
msleep(5);
send_command(Command::PixelFormatSet, 0x05);
send_command(Command::TearingEffectLineOn, 0x00);
@@ -262,7 +262,7 @@ void initPanel() {
void shutdownPanel() {
send_command(Command::DisplayOff);
send_command(Command::SleepIn);
mssleep(5);
msleep(5);
}
void setDrawingArea(KDRect r, Orientation o) {

View File

@@ -6,11 +6,11 @@ namespace Events {
static bool sleepWithTimeout(int duration, int * timeout) {
if (*timeout >= duration) {
mssleep(duration);
msleep(duration);
*timeout -= duration;
return false;
} else {
mssleep(*timeout);
msleep(*timeout);
*timeout = 0;
return true;
}

View File

@@ -56,7 +56,7 @@ inline void activateRow(uint8_t row) {
Device::RowGPIO.ODR()->setBitRange(9, 0, rowState);
// TODO: 100 us seems to work, but wasn't really calculated
ussleep(100);
usleep(100);
}
inline bool columnIsActive(uint8_t column) {

View File

@@ -29,7 +29,7 @@ void Ion::Power::suspend(bool checkIfPowerKeyReleased) {
/* Update LEDS
* if the standby mode was stopped due to a "stop charging" event, we wait
* a while to be sure that the plug state of the USB is up-to-date. */
mssleep(200);
msleep(200);
//Ion::LED::setCharging(Ion::USB::isPlugged(), Ion::Battery::isCharging());
#endif

View File

@@ -20,10 +20,10 @@ int main(int argc, char * argv[]) {
return 0;
}
void Ion::mssleep(long ms) {
void Ion::msleep(long ms) {
emscripten_sleep(ms);
}
void Ion::ussleep(long us) {
void Ion::usleep(long us) {
emscripten_sleep(us/1000);
}

View File

@@ -104,7 +104,7 @@ Ion::Events::Event Ion::Events::getEvent(int * timeout) {
#include <chrono>
void Ion::mssleep(long ms) {
void Ion::msleep(long ms) {
auto start = std::chrono::high_resolution_clock::now();
while (true) {
sDisplay->redraw();
@@ -117,7 +117,7 @@ void Ion::mssleep(long ms) {
}
}
void Ion::ussleep(long us) {
void Ion::usleep(long us) {
auto start = std::chrono::high_resolution_clock::now();
while (true) {
sDisplay->redraw();

View File

@@ -27,12 +27,8 @@ Q(set_pixel)
// utime QSTRs
Q(time)
Q(sleep)
Q(sleep_ms)
Q(sleep_us)
Q(ticks_ms)
Q(ticks_us)
Q(ticks_add)
Q(ticks_diff)
Q(time)
Q(clock)
// MicroPython QSTRs
Q()

View File

@@ -35,14 +35,7 @@
void delay_ms(mp_uint_t delay) {
uint32_t start = millis();
while (millis() - start < delay && !micropython_port_should_interrupt(true)) {
mssleep(1);
}
}
void delay_us(mp_uint_t delay) {
uint32_t start = micros();
while (micros() - start < delay && !micropython_port_should_interrupt(false)) {
ussleep(1);
msleep(1);
}
}
@@ -56,64 +49,17 @@ STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep);
STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) {
mp_int_t ms = mp_obj_get_int(arg);
if (ms > 0) {
delay_ms(ms);
}
return mp_const_none;
STATIC mp_obj_t time_clock(void) {
return mp_obj_new_float(millis() / 1000.0);
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_ms_obj, time_sleep_ms);
STATIC mp_obj_t time_sleep_us(mp_obj_t arg) {
mp_int_t us = mp_obj_get_int(arg);
if (us > 0) {
delay_us(us);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_us_obj, time_sleep_us);
STATIC mp_obj_t time_ticks_ms(void) {
return MP_OBJ_NEW_SMALL_INT(millis() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
}
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj, time_ticks_ms);
STATIC mp_obj_t time_ticks_us(void) {
return MP_OBJ_NEW_SMALL_INT(micros() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
}
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj, time_ticks_us);
STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) {
// we assume that the arguments come from ticks_xx so are small ints
mp_uint_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
mp_uint_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
// Optimized formula avoiding if conditions. We adjust difference "forward",
// wrap it around and adjust back.
mp_int_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1))
- MICROPY_PY_UTIME_TICKS_PERIOD / 2;
return MP_OBJ_NEW_SMALL_INT(diff);
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff);
STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) {
// we assume that first argument come from ticks_xx so is small int
mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in);
mp_uint_t delta = mp_obj_get_int(delta_in);
return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add);
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_time_obj, time_clock);
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_clock_obj, time_clock);
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
{ MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
{ MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_utime_time_obj) },
{ MP_ROM_QSTR(MP_QSTR_clock), MP_ROM_PTR(&mp_utime_clock_obj) },
};
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);

View File

@@ -39,7 +39,7 @@ static inline void ion_main_inner() {
quiz_print("ALL TESTS FINISHED");
#if !QUIZ_USE_CONSOLE
while (1) {
Ion::mssleep(1000);
Ion::msleep(1000);
}
#endif
}
@@ -59,7 +59,7 @@ void ion_main(int argc, char * argv[]) {
quiz_assert(false);
#if !QUIZ_USE_CONSOLE
while (1) {
Ion::mssleep(1000);
Ion::msleep(1000);
}
#endif
}