not logged in | [Login]

Brain barf

This section to be deleted/reformatted/made politically correct at a later date

  • I will not be teaching C or Python - that is completely off-topic and I ask that questions regarding C or Python questions not specific to micropython be taken elsewhere.
  • Basic knowledge of Make will probably help you out as well

Hello, world!

Introduction

Creating a pyboard module is a complicated task all by itself and I don't want to try and learn both C -> pyboard at the same time as ST's CAN hardware all while struggling to push my compiled code to the board (dfu-util is giving me trouble at the moment). Because of all this complexity, I will start by simply creating a "Hello, world!" pyboard module within the Unix implementation. I'm a Saab fan, so my module consists of the 6 different Saabs that I've owned. Originally it was going to be a generic module capable of storing any Saab between 1947 and 1993, but I realized this was more difficult and did not match up my example: the LED module. You see, the LEDs on the micropython board are all hard-coded in as static globals instead of being created on-the-fly as you need them - so I too have hard-coded my six Saabs instead of allowing a user to create any Saab (s)he wants. You will then be able to select which of my Saabs you want via an ID (just like selecting LED 1, 2 or 3).

Assumptions

  • Any content within angle brackets (that's the < and >) is a variable of your choosing
  • The path <root>/ refers to the top level directory of micropython. I am referring to the directory with README.md in it.

Getting started

  1. First, start by downloading the source code, reading the README, and familiarizing yourself with how to compile from source. https://github.com/micropython/micropython
  2. I've created two files - <root>/unix/saab.c and <root>/unix/saab.h. I can compile these into the module by adding the source file to line 73 of <root>/unix/Makefile.
  3. We will work from a top-down architectural approach (for the most part) but because we not are not forward declaring anything, this equates to a bottom-up approach when you think about line numbers! Be sure to use led.[c/h] and pin.[c/h] throughout this tutorial as reference material.

Writing Your First Lines of Code

  1. We start by declaring a base object with which we can build our new type: const mp_obj_type_t pyb_<name>_type = {...}; See the line 292 of the LED module for reference. This variable contains lots of metadata about our object: its name, a function that can be used for printing our object, a function to create a new instance as well as a dictionary with each of the name of each public method. Lots more metadata can be filled in - just take a look at the definition of mp_obj_type_t.
  2. The first line of this declaration is {&mp_type_type }. So, at a minimum, we have const mp_obj_type_t pyb_saab_type = { {&mp_type_type } };. This is essentially inheritance - giving a base type to our class. The output of Python's type function will depend on what we give this first field. I think it's a bit confusing and not very important for continuing on, so just type that line and move forward with life. :)
  3. Next we give our object a name - the name that will be used via Python. Micropython uses Q-Strings throughout, which simply means we can drop the name of our object in *** TODO: Finish this section ***
  4. Moving forward, we can create stubs for two common methods: those would be printing an instance of our object and creating a new object. We can do this by expanding the previous snippet to This code will be inserted when I get home
    • *** TODO: Finish this section ***