Create an Extension for HaloCode in Three Minutes

Background

This section uses the WS2812 extension as an example to describe how to create an extension for HaloCode in three munites. It focuses on the key points and does not cover every detail. You can download the attached WS2812 extension to find more details.

Introduction to WS2812

Main features

● The control circuit and RGB chip are integrated in a 5050 package component to form a complete external control pixel.

● It is equipped with a built-in signal shaping circuit, shaping signals received by each pixel before outputting waveforms, and thus preventing line waveforms from distortion accumulation.

● It is equipped with built-in power-on and power-off reset circuits.

● The three primary colors of each pixel can achieve 256 levels of luminance, implementing full true color display of 16,777,216 colors with a scan frequency of no lower than 400Hz.

● The serial cascade interface enables the receiving and decoding of data through one signal line.

● Any two-point transmission within five meters does not require any additional circuit.

● At a refresh rate of 30 frames per second, the number of cascades in low-speed mode is no less than 512, and that in high-speed mode is no less than 1024.

● The maximum data transmission rate is 800 Kbps.

● It is cost-effective with highly consistent light color.

Application

It can be applied to LED full-color light strings, full-color LED modules, LED Pixel screens, various electronic products, electronic equipment, and so on.

图片

Preparation for Creating the WS2812 Extension

Goal: Create a HaloCode extension that drives WS2812

Knowledge points:

  1. The hardware core of the HaloCode is ESP32.
  2. The software core of the HaloCode is MicroPython.
  3. WS2812 are LED strips.

Keywords: MicroPython, NeoPixels, WS2812, mext

  1. MicroPython:

The "upload mode" of mBlock 5 is to convert the graphics blocks into Python statements and upload them to HaloCode for execution.

For more information about the syntax and ESP32, see the official MicroPython documentation

Link: http://docs.micropython.org/en/latest/esp32/quickref.html

  1. NeoPixels library: Used to drive WS2812.

https://github.com/micropython/micropython/blob/master/ports/esp32/modules/neopixel.py

  1. WS2812 driving example program:

http://docs.micropython.org/en/latest/esp8266/tutorial/neopixel.html?highlight=neopixel

  1. WS2812 manual:

https://wenku.baidu.com/view/b18fbd6bddccda38376bafbd.html

  1. "mext" is the suffix of the extension file generated by the extension builder. The extension file demonstrated in this tutorial is attached at the end of this article.

To get started, drive WS2812 with Python code in mBlock 5. The Python Code is provided at the end of this article.

Steps for Creating the WS2812 Extension

Step 1: Create a new extension (first minute)

Choose My Plugin > My Extension, click Add extension, select Universal template in the dialog box that appears, and click OK.

图片

The basic information area is displayed.

Step 2: Fill in the basic information

图片

The information in the red rectangle boxes is mandatory.

Enter the basic information and click OK.

The homepage is displayed.

Step 3: Add categories and blocks

Click the editing icon of the extension.

图片

The editing page is displayed.

图片

Six setting tabs are provided. For the WS2812 extension, you need to set the basic information (which is set in step 2), blocks settings, and transcode settings.

Let's set the transcode first.

Set Transcoding language support to Python, and click Save.

图片

Click the Blocks settings tab and click Add category.

The Extension category area is displayed.

图片

Set the ID and category name, and click OK.

One minute is enough, isn't it?

Let's go to the next step.

Step 4: Design the Statement Block (second minute)

Click on the building block in the "building block list" to start configuring the building block details.

Click the icon at the bottom of the Blocks list area to add a block, and setting it in the Blocks settings area on the right.

图片

Tips:

To learn parameter setting, visit https://www.mBlock 5.cc/doc/en/developer-documentation/design-blocks-4.html.

For block types, refer to the block design standards at https://www.mBlock 5.cc/doc/en/developer-documentation/design-blocks-5.html.

Refer to the official blocks when designing blocks, so that other users can easily understand how to use the your blocks.

You can add all the blocks you need, and then set the code for them.

This is the second minute.

Let's go to the next step.

Step 5: Fill in transcode (third minute)

Click the Upload transcode tab in the Blocks settings area.

图片

Let's start with the following block.

图片

Click the Upload transcode tab.

图片

You do not need to set the import, lib, and loop items.

Double-click the code area to edit it.

Enter the following code and click OK.

图片

Tips:

The following figure shows the mapping between Upload transcode and Python in mBlock 5. The reference variable syntax is {lednumber}.

图片

The loop item is not set in this example.

This is the third munite, and the extension has been created.

Now, let's test the extension we have just created.

Verify the WS2812 Extension

Step 1: Download the extension and drag and drop it to mBlock 5 on your PC.

1. Click Download in the upper right corner.

图片

The package box is displayed.

2. Wait for the packaging to complete, and click Download now.

图片

3. Open mBlock 5, find the halo_ws2812.mext file, and drag and drop it to mBlock 5.

图片

The installation is successful if the message indicating that WS2812 is successfully updated.

Tips:

If you have dragged the same extension to mBlock 5 multiple times, wait until the message indicating successful update is displayed and close mBlock 5. Open mBlock 5 again to allow it to load the latest .mext file.

图片

Step 2: Verify the blocks

  1. Drag and drop blocks to the Scripts area.

Before drag the blocks, you need to:

(1) Set the mode to Upload, and drag an event block to the Scripts area first.

(2) Click the </> Button at the right sidebar to display the code interface.

图片

Check the code generated by the blocks carefully.

图片

Tips:

You can copy the code and paste it to the Python editing interface. Upload the progam to HaloCode, and you can see the error information returned by HaloCode.

图片

Summary

Extension preview screenshot

图片

Another Way to Learn Extension Block DesignIn addition to the step-by-step learning by following the tutorial, you can also find an extension, import it to the extension builder, and refer to it.

What you need is to import it to the extension builder.

图片

图片

After importing an extension, you can see in detail how it is designed.

For details about how to import an extension, see How to import extension into IDE (PC).

Download extension package

Download Extension Package

Python Code ready to Use

#generated by mBlock 55 for HaloCode
#codes make you happy

import time, machine, event, halo

from esp import neopixel_write

RED = (255, 0, 0)

YELLOW = (255, 255, 0)

GREEN = (0, 255, 0)

AQUA = (0, 255, 255)

BLUE = (0, 0, 255)

PURPLE = (148, 0, 211)

INDIGO = (75, 0, 130)

ORANGE = (255, 127, 0)

COLORS = (RED, YELLOW, GREEN, AQUA, BLUE, PURPLE)

RAINBOWS = (PURPLE, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED)

class NeoPixel:

ORDER = (1, 0, 2, 3)

def __init__(self, pin, n, bpp=3, timing=1):

    self.pin = pin

    self.n = n

    self.bpp = bpp

    self.buf = bytearray(n * bpp)

    self.pin.init(pin.OUT)

    self.timing = timing

def __setitem__(self, index, val):

    offset = index * self.bpp

    for i in range(self.bpp):

        self.buf[offset + self.ORDER[i]] = val[i]

def __getitem__(self, index):

    offset = index * self.bpp

    return tuple(self.buf[offset + self.ORDER[i]]

                 for i in range(self.bpp))

def fill(self, color):

    for i in range(self.n):

        self[i] = color

def fillone(self, ni, color):

    self[ni] = color

def write(self):

    neopixel_write(self.pin, self.buf, self.timing)

global neop

neop = NeoPixel(machine.Pin(23), 10)

@event.start

def on_start():

for color in RAINBOWS:

    neop.fill(color)

    neop.write()

    time.sleep(5*0.1)

Reference example

Create Extensions for micro:bit

results matching ""

    No results matching ""