News:

We're live! Join us!

Main Menu

Python / Micro-Python

Started by Chris Savage, Mar 31, 2024, 12:36 AM

Previous topic - Next topic

Chris Savage

In talking to Jeff_T about Micro-Python, I thought I would start a discussion about the language, since I have been interested in learning it for some years, but never had a reason to explore it.

I'm also wondering what the differences are between Python and Micro-Python.

This is an open invitation for anyone to join this discussion. As far as Python goes, I'm a complete noob.

        I'm only responsible for what I say, not what you understand.

Jeff_T

Hi Chris, Micro-Python is just a stripped down version of Python with features more specific to microcontrollers. Python reminds me of Basic in some ways, it's simple and quick to create new designs, there are thousands of resources on the internet and Youtube even free code courses.

You dont need a microcontroller to start exploring Python, the same IDE I use for micro-python I also use for Python on the PC.

The IDE that I use mostly is Thonny from https://thonny.org/ . It has Python 3.10 built in and is a very compact app. so once you download it your ready to start programming.

You do have to set which interpreter you are using, for example if you want Python you would select it under Tools/Options/Interpreter under the heading "Which kind of interpreter should Thonny use for running your code?", for the desktop select LocalPython 3. If you were programming a microcontroller your selection might be Micropython (ESP32), this option would require you select a COM port.

The IDE has a large pane with line numbers which is the code editor and below that is the terminal pane for receiving output and taking input, the sidebar on the left is a file explorer for accessing files from the PC or Microcontroller.

You don't have to name the data type of variables but you do have initialize them, here is a simple example with methods I am sure will look familiar.

Just copy and paste the following into the code editor and press F5 or the small green run button. One thing to be aware of using Python is that it has to have the correct indentation or it will signal an error.


my_integer = 0
my_string_1 = "Hello "
my_string_2 = "World !"
my_string_3 = ""
my_list = []

def mutiply_function(a,b):
    val = a * b
    return val

   
print(my_string_1 + my_string_2)
print("Strings are immutable so my_string_1 is unchanged -> ",my_string_1)
print("The same with my_string_2 -> ",my_string_2)

my_string_3 = my_string_1 + my_string_2

print("To put the two into one string we need a third string -> ", my_string_3)
print("We can measure the length of a string -> ", len(my_string_3))

my_integer = mutiply_function(4,5)

print("Simple functions are standard -> 4 * 5 = ", my_integer)

my_list.append(my_string_3)
my_list.append(my_integer)

print("Lists can hold any data type -> ", my_list)
print("and can be accessed by index [1] -> ", my_list[1])
print("and [0] -> ", my_list[0])

The micro python discussion forum has some very knowledgeable people for answers to questions you can't find elsewhere https://github.com/orgs/micropython/discussions

One final thought, Python for the desktop, which has an in built GUI creator, can also be converted from a '.py' file to a '.exe' file , great for creating cross platform applications.

Chris Savage

Sounds good. Now that those two modules have arrived, I think I will mess around with some Micro-Python stuff before I get going on the project, which I still need to order parts for. But at least now I know what I am getting.

        I'm only responsible for what I say, not what you understand.

Jeff_T

Yes that is what I would do Chris, having two is nice.

ESP32 have their own wireless network protocol called "ESP NOW" which does 1 to 1 , 1 to many or many to 1 and it's stated 700 ft. range, there is something to try.

It also has BLE, I did pair one with a phone once but not really tried much else.

Micro Python has the relevant modules/libraries included in the MP firmware, probably the module used mostly is called "machine" , this module contains the methods for SDCard, SPI, Pin , I2c, I2S , UART etc.

All the docs. are online, here is a link to the pin control doc.   https://docs.micropython.org/en/latest/library/machine.Pin.html

That page even gives pin irq and interrupt handler a mention.

Chris Savage

This is a just a side-note...I promise, I'm not going to get distracted by it (no time). But, have you seen this thread on the Parallax Forums?

https://forums.parallax.com/discussion/169862/micropython-for-p2

        I'm only responsible for what I say, not what you understand.

Jeff_T

I had my own thread on Parallax re: micropython, I even bought three P2 development modules. I went with it for a couple of months and gave up through lack of progress and lack of official support. The thread in the link is going on six year old now and has been resurrected by Rayman, I will keep an eye on it though things could always get better.