Controlling the display: Difference between revisions

Copyright © 2017–2023 J. M. Spivey
Jump to navigation Jump to search
Line 4: Line 4:
The @IMAGE@ macro can be used to create a static image, with the macro call written the same way for both V1 and V2, but expanding into an array initialiser that has length 3 words in V1 and 10 words on V2.
The @IMAGE@ macro can be used to create a static image, with the macro call written the same way for both V1 and V2, but expanding into an array initialiser that has length 3 words in V1 and 10 words on V2.
<pre>
<pre>
static const unsigned heart[] =
static const image heart =
     IMAGE(0,1,0,1,0,
     IMAGE(0,1,0,1,0,
           1,1,1,1,1,
           1,1,1,1,1,
Line 15: Line 15:
{ 0x28f0, 0x5e00, 0x8060 }
{ 0x28f0, 0x5e00, 0x8060 }
</pre>
</pre>
The expansion and simplification process relies both on the macro expansion provided by the C preprocessor, and the fact that the C compiler itself can take complex expressions involving shifts and bitwise-{{sc|or}} operations and reduce them to simple constants, suitable for defining a read-only array.
The expansion and simplification process relies both on the macro expansion provided by the C preprocessor, and the fact that the C compiler itself can take complex expressions involving shifts and bitwise-{{sc|or}} operations and reduce them to simple constants, suitable for defining a read-only array like @heart@, whose type is given by the typedef,
<pre>
#define NIMG 3
 
typedef unsigned image[NIMG];
</pre>
on V1, with @NIMG@ defined as 10 on V2.
 
This expansion of the macro into an array initialiser means that the macro is suitable for use only in declaring a named image constant, as shown above.  It can't be used, for example, on the right hand side of an assignment statement.  The same effect can be obtained with an explicit call of @memcpy()@: first define a constant array for the image, then write something like
<pre>
image myimage;
 
memcpy(myimage, heart, sizeof(image));
</pre>


==Computing images dynamically==
==Computing images dynamically==


==The {{microbian}} display server==
==The {{microbian}} display server==

Revision as of 12:34, 11 March 2022

One of the attractions of the micro:bit for exploring low-level programming is that it has the 25-LED display, but that controlling this display is not quite trivial, giving a series of non-trivial but nevertheless manageable challenges. Our early attempts to program the display will depend on controlling the hardware directly, and will need different code on the V1 and V2 {microbit}}s, whose displays are wired differently. However, once we start to use the display as a tool for investigating other aspects of the hardware (for example, when we use it to implement a 2-D spirit level based on the accelerometer), it's good if client code can be written in a way that is the same for V1 and V2. The micro:bian operating system provides a server process – implemented differently on V1 and V2 – that provides just this abstraction.

Static images

The IMAGE macro can be used to create a static image, with the macro call written the same way for both V1 and V2, but expanding into an array initialiser that has length 3 words in V1 and 10 words on V2.

static const image heart =
    IMAGE(0,1,0,1,0,
          1,1,1,1,1,
          1,1,1,1,1,
          0,1,1,1,0,
          0,0,1,0,0);

That call of the IMAGE macro expands and simplifies (on V1) to something equivalent to the text

{ 0x28f0, 0x5e00, 0x8060 }

The expansion and simplification process relies both on the macro expansion provided by the C preprocessor, and the fact that the C compiler itself can take complex expressions involving shifts and bitwise-or operations and reduce them to simple constants, suitable for defining a read-only array like heart, whose type is given by the typedef,

#define NIMG 3

typedef unsigned image[NIMG];

on V1, with NIMG defined as 10 on V2.

This expansion of the macro into an array initialiser means that the macro is suitable for use only in declaring a named image constant, as shown above. It can't be used, for example, on the right hand side of an assignment statement. The same effect can be obtained with an explicit call of memcpy(): first define a constant array for the image, then write something like

image myimage;

memcpy(myimage, heart, sizeof(image));

Computing images dynamically

The micro:bian display server