diff --git a/docs/library/enum.rst b/docs/library/enum.rst new file mode 100644 index 0000000000000..40fbaae028724 --- /dev/null +++ b/docs/library/enum.rst @@ -0,0 +1,74 @@ +:mod:`enum` -- MicroPython Enum class like in Python. +======================================================== + +.. module:: enum + :synopsis: MicroPython Enum class like in Python. + +This module implements enumeration Enum class for MicroPython. +Class Enum is based in dictonary `dict` class and provides a way to create enumeration +objects with key-value pairs. + +Usage examples:: + + from enum import Enum + + class State(Enum): + Stop = 10 + Run = 20 + Ready = 30 + + + state = State() + print("state:", State()) + + current_state = state.Stop + print("current_state:", current_state, state.key_from_value(current_state)) + if current_state == state.Stop: + print(" Stop state") + if current_state != state.Ready: + print(" Not a Ready state") + print(" Run!") + current_state = state.Run + print("current_state:", current_state, state.key_from_value(current_state)) + # some process + i = -1 + while current_state != state.Ready: + i += 1 + if state.is_value(i): + if int(str(State(i))) == state.Ready: + current_state = state.Ready + print(".", end="") + print() + print("current_state:", current_state, state.key_from_value(current_state)) + print("Done!") + +Output:: + + state: State({'Run':20, 'Stop':10, 'Ready':30}) + current_state: 10 State.Stop + Stop state + Not a Ready state + Run! + current_state: 20 State.Run + ............................... + current_state: 30 State.Ready + Done! + +.. warning:: + + ``enum`` module allows XXX. + + >>> isinstance(State.Run, State) + False + >>> State(state.Ready) == state.Ready + False + >>> int(str(State(state.Ready))) == state.Ready + True + +.. note:: + + ``enum`` module ZZZ. + +.. seealso:: + + YYY. diff --git a/docs/library/index.rst b/docs/library/index.rst index 2919378ce13b2..083948c3b5db0 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -107,6 +107,7 @@ the following libraries. openamp.rst uctypes.rst vfs.rst + enum.rst The following libraries provide drivers for hardware components.