weixin_39703468
weixin_39703468
2020-12-30 00:35

Consider allowing microbit.sleep to take a float

microbit.sleep currently only accepts an integer, since the underlying DAL only accepts an integer argument.

Maybe it makes sense to allow a float and implicitly truncate it to an int?

what is your opinion?

该提问来源于开源项目:bbcmicrobit/micropython

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

11条回答

  • weixin_39719127 weixin_39719127 4月前

    Guys,

    The underlying DAL implementation of sleep() is around a scheduler context (so performs a power efficient sleep). 6ms is the current scheduling quantum, hence the integer argument. I do have a few feature requests to improve the granularity though...

    Whilst I'm sure we'll never have a full RTOS scheduler, there will likely be some changes in the future to provide a better 'best effort' sleep(). If I do, it'll probably be exposed as sleep_ms() and sleep_us(). The reason being that the processor on the micro:bit (Cortex M0 core) has no FPU, so avoiding floats where possible improves efficiency.

    点赞 评论 复制链接分享
  • weixin_39703468 weixin_39703468 4月前

    thanks for the info.

    The background to this issue: we had a demo at the BBC and needed to use the Python int function to convert a float to be passed to microbit.sleep. This required explanation, an explanation that complicates the teaching. Python has floats builtin and so any operation with a division will make a float, so passing these floats to the API will require casting.

    If I were writing a Python API without any need to conform to some existing functions, then I'd make microbit.sleep() take as its argument a floating point value in seconds. This is the easiest to explain to a kid: they know what seconds are (they probably don't know what ms/us are) and it just makes sense that you can sleep for 0.5 seconds (or any fraction).

    We could implement our Python microbit.sleep() to take seconds, and call the best underlying function (could be sleep_ms or sleep_us). The problem is that this will confuse kids going from using MicroPython to another micro:bit language (one that uses ms for sleep).

    Maybe you can rename the current MicroBit::sleep method to MicroBit::sleep_ms, to make it more self-documenting, provide a path for the future addition of MicroBit::sleep_us, and then reserve the word "sleep" to take seconds?

    点赞 评论 复制链接分享
  • weixin_39719127 weixin_39719127 4月前

    This is a good argument for providing a float interface. I agree that the ideal set would be:

    sleep(float) sleep_ms(int) sleep_us(int)

    This might catch a few other folks out that are targeting the DAL layer, but if we patch the Touch Develop glue layer this will catch about 95% of them.

    Better to do this sooner rather than later. I'll work it into the next release of the runtime.

    点赞 评论 复制链接分享
  • weixin_39703468 weixin_39703468 4月前

    that would be great if Touch Develop et al used the same interface... but there are some things to think about: other methods (like display print) take milliseconds to specify delays, and you probably don't want to convert all of them to seconds. So kids do need to learn about ms to some extent.

    Also note that this issue here is about the MicroPython interface to the DAL, and the MicroPython function microbit.sleep. The issue is whether to accept a float and convert it automatically to an int for the DAL, or to force the user to do it themselves. So the issue should remain open until that idea is resolved.

    点赞 评论 复制链接分享
  • weixin_39719127 weixin_39719127 4月前

    Apologies - used to seeing these on the micro:bit github. :-D

    In that case I'll leave the DAL interface as specified and let you guys talk over your interface layer. Let me know if you need any support from under the hood.

    点赞 评论 复制链接分享
  • weixin_39703468 weixin_39703468 4月前

    Ok!

    点赞 评论 复制链接分享
  • weixin_39703468 weixin_39703468 4月前

    What about this? I still think microbit.sleep should be allowed to take a float (representing a fraction of a microsecond). That way people can use float maths to compute the sleep time and not need to cast to an integer.

    点赞 评论 复制链接分享
  • weixin_39704314 weixin_39704314 4月前

    +1

    点赞 评论 复制链接分享
  • weixin_39614322 weixin_39614322 4月前

    +1 as well

    点赞 评论 复制链接分享
  • weixin_39704314 weixin_39704314 4月前

    It appears the "idea is resolved" (to quote ). :-)

    点赞 评论 复制链接分享
  • weixin_39703468 weixin_39703468 4月前

    Implemented, see above.

    点赞 评论 复制链接分享

相关推荐