Merge remote-tracking branch 'upstream/master' into omega-dev

This commit is contained in:
Quentin Guidée
2019-12-06 19:34:28 +01:00
15 changed files with 115 additions and 33 deletions

View File

@@ -22,21 +22,22 @@ bool micropython_port_vm_hook_loop() {
return micropython_port_interrupt_if_needed();
}
bool micropython_port_interruptible_msleep(uint32_t delay) {
bool micropython_port_interruptible_msleep(int32_t delay) {
assert(delay >= 0);
/* We don't use millis because the systick drifts when changing the HCLK
* frequency. */
constexpr uint32_t interruptionCheckDelay = 100;
const uint32_t numberOfInterruptionChecks = delay / interruptionCheckDelay;
uint32_t remainingInterruptionChecks = numberOfInterruptionChecks;
while (remainingInterruptionChecks > 0) {
constexpr int32_t interruptionCheckDelay = 100;
const int32_t numberOfInterruptionChecks = delay / interruptionCheckDelay;
int32_t remainingDelay = delay - numberOfInterruptionChecks * interruptionCheckDelay;
int32_t currentRemainingInterruptionChecks = numberOfInterruptionChecks;
do {
// We assume the time taken by the interruption check is insignificant
if (micropython_port_interrupt_if_needed()) {
return true;
}
remainingInterruptionChecks--;
Ion::Timing::msleep(interruptionCheckDelay);
}
Ion::Timing::msleep(delay - numberOfInterruptionChecks * interruptionCheckDelay);
Ion::Timing::msleep(currentRemainingInterruptionChecks == numberOfInterruptionChecks ? remainingDelay : interruptionCheckDelay);
currentRemainingInterruptionChecks--;
} while (currentRemainingInterruptionChecks >= 0);
return false;
}

View File

@@ -10,7 +10,7 @@ extern "C" {
// These methods return true if they have been interrupted
bool micropython_port_vm_hook_loop();
bool micropython_port_interruptible_msleep(uint32_t delay);
bool micropython_port_interruptible_msleep(int32_t delay);
bool micropython_port_interrupt_if_needed();
int micropython_port_random();

View File

@@ -71,6 +71,10 @@ mp_obj_t modkandinsky_draw_string(size_t n_args, const mp_obj_t * args) {
KDColor backgroundColor = (n_args >= 5) ? ColorForTuple(args[4]) : KDColorWhite;
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
KDIonContext::sharedContext()->drawString(text, point, KDFont::LargeFont, textColor, backgroundColor);
/* drawString function might take some time to execute so we add an extra check
* for user interruption (to avoid freezing in an infinite loop calling
* 'drawString' for instance). */
micropython_port_interrupt_if_needed();
return mp_const_none;
}
@@ -84,6 +88,10 @@ mp_obj_t modkandinsky_fill_rect(size_t n_args, const mp_obj_t * args) {
KDColor color = ColorForTuple(args[4]);
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
KDIonContext::sharedContext()->fillRect(rect, color);
/* fillRect function might take some time to execute so we add an extra check
* for user interruption (to avoid freezing in an infinite loop calling
* 'fillRect' for instance). */
micropython_port_interrupt_if_needed();
return mp_const_none;
}

View File

@@ -335,6 +335,8 @@ bool Turtle::draw(bool force) {
m_drawn = true;
}
/* TODO: Maybe this threshold should be in time (mileage/speed) instead of
* mileage to interrupt with the same frequency whatever the speed is. */
if (m_mileage > k_mileageLimit) {
if (micropython_port_interruptible_msleep(1 + (m_speed == 0 ? 0 : 3 * (k_maxSpeed - m_speed)))) {
return true;