Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use object-oriented method to control microbit display in MicroPython

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is a detailed introduction to "how to control microbit display with object-oriented method in MicroPython". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to control microbit display with object-oriented method in MicroPython" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.

Python (micropython) is very useful for physics calculations

Python is a very popular language for well-deserved physics computing applications.

Python is easy to learn, expressive and free to use on Linux, Mac OS and Microsoft Windows.

Python energizes the Raspberry Pi, and a lot of code for the Raspberry Pi is implemented in Python.

Recently, micropython has brought the language to several popular MCU platforms, including BBC microbit.

But...

Most published micropython scripts use Python programming style. Most programmers start out this way, and it's the most widely used style in tutorials.

When programmed programmatically, you can do the work that the computer wants you to do step by step, and the program is the process that specifies those steps.

It's worth exploring other programming styles, and in this article you'll see how to develop a simple Python application that can use an object-oriented style. In object-oriented programs, you program by creating objects, packages of Mini programs that combine data and code to perform simple tasks. Objects collaborate (interact) with each other to perform more complex tasks. Specify the types of objects required by your program by writing a class definition that specifies the type of a particular object (data) and what it can do (methods).

The idea is simple. The application uses microbit's LED dot matrix to display the value of the counter and can change it by using buttons. Later, you'll see how this simple idea can be applied to an actual project.

You can implement a demo with a short script, but the script components are not easily usable in other applications.

Here are some changes that might need to be implemented without having to start with scratch:

LED lamp post display

Display current light level or temperature

A clock pattern that jumps to 0 when the parameter rises to 9

Similar displays using different types of LEDs

Display with only one LED display value turned on at a time

A display that displays different values in different colors.

Of course, you can do this by creating multiple variations of the original script, but what happens if you find bugs in the original script? You must make the same changes on every copy!

This is one of the reasons why experienced programmers try to avoid duplicate copies of code. There's even an acronym to help you remember it: DRY (Don't repeat yourself), which stands for "Don't repeat yourself."

There is also a good reason to write code in simple components. Each component is easy to test on its own. This helps you avoid bugs, track down and remove any crawlers in your code.

Let's start coding.

You will need to look at the code for the buttons and tell the counters in the app to count up or down when one of the buttons is pressed.

There is no need for a class to implement it.

Create counters in your application code that are linked to SimpleDisplayer instances to achieve the desired results.

max_count = 10disp = SimpleDisplayer()counter = Counter(disp, max_count)while True: if button_a.is_pressed(): counter.up() if button_b.is_pressed(): counter.down() sleep(200)

The last part should be self-explanatory. It creates a code for SimpleDisplayer and counters. Let's start with the counter.

Counter class

In this application, a counter is a simple object that knows its current count and can count up and down between its initial value of zero and some maximum value. In our example, the maximum value is 10.

The counter knows about another object and is notified whenever the counter value changes. Initially, this will be a SimpleDisplayer object and displayed on microbit.

Here is the code for the counter class:

class Counter(): # count ranges from 0 to max. # when the count changes disp (the displayer) is notified def __init__(self, disp, max): self._ disp = disp self._ max = max self._ count = 0 def update(self): self._ disp.value(self._ count) def up(self): if self._ count

< self._max: self._count += 1 self.update() def down(self): if self._count >

0: self._ count -= 1 self.update()

The counter sends a message to the display, which is a SimpleDisplayer code that uses the built-in LED to display the value:

# SimpleDisplayer shows the latest value on the micro:bit's LEDsclass SimpleDisplayer(): def value(self, num): display.show(str(num))

Object-oriented programming is easy to change

This approach makes it easy for our application to adapt to a change in code. Suppose you want to use an LED array similar to Proto-pic EDGE to display the current state of counters.

Here is the code that drives the revision application:

from microbit import *import neopixel... max_count = 10leds = neopixel.NeoPixel(pin0, max_count)disp = BarGraph(leds, BarGraph.YELLOW)counter = Counter(disp, max_count)while True: if button_a.is_pressed(): counter.up() if button_b.is_pressed(): counter.down() sleep(200)

The last part remains unchanged. Two new classes of code are used: neopixel and BarGraph.

The neopixel class is part of the micropython version of microbit, so we only need to import the neopixel module. BarGraph is a new part of our application, and we need to write its definition and insert it into it.

Here is the code for the BarGraph class:

# BarGraph uses an array of Neopixels or other similar LEDs# to show the latest valueclass BarGraph(): # colo(u)r constants. # we use low values so the we don't strain the micro:bit's power supply WHITE = (50,50,50) RED = (50,0,0) BLUE = (0,0,50) GREEN = (0,50,0) YELLOW = (100,50,0) CLEAR = (0,0,0) def __init__(self, leds, on_colour=WHITE): self._ leds = leds self._ count = len(leds) self._ on_colour = on_colour def value(self, num): for i in range(self._ count): if i < num: self._ leds[i] = self._ on_colour else: self._ leds[i] = self.CLEAR self._ leds.show()

The application code (shown earlier) creates an instance and connects the and counters to the BarGraph to achieve the desired result.

Modular code is easier to change. In the next part of this article, we'll see changing the input to light-sensing LDR and adding more colors to the BarGraph display.

We will also see another major benefit of this approach: ease of testing.

Read here, this article "How to control microbit display with object-oriented method in MicroPython" article has been introduced, want to master the knowledge points of this article also need to be used by yourself to understand, if you want to know more about the content of the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report