Silicon ChipMicromite Plus Advanced Programming - November 2016 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: Renewable energy is seriously damaging the Australian economy
  4. Feature: Airbags: Your Car’s Explosive Guardian Angels by Dr David Maddison
  5. Feature: What Is The Internet Of Things (IoT)? by Ross Tester
  6. Project: 50A Battery Charger Controller by John Clarke
  7. Serviceman's Log: Odd happenings in my new house by Dave Thompson
  8. Project: Passive Line To Phono Input Converter by Nicholas Vinen & Bao Smith
  9. Product Showcase
  10. Feature: Micromite Plus Advanced Programming by Geoff Graham
  11. Project: Micromite Plus LCD BackPack by Nicholas Vinen
  12. Project: WiFi Controlled Switch Using A Raspberry Pi & Smartphone by Greg Swain & Nicholas Vinen
  13. PartShop
  14. Review: Siglent SDS2104 4x100MHz Mixed Signal Scope by Jim Rowe
  15. Subscriptions
  16. Vintage Radio: The incredible shrinking mantel set: GE’s T2105 by Ian Batty
  17. Market Centre
  18. Notes & Errata: 4-Input Automotive Fault Detector, Sept 16; Circuit Notebook, Sept 16; Vintage Radio, Sept 16; Appliance Energy Meter, Aug-Oct 16; Compact 8-Digit Auto-Ranging Frequency Meter, Aug 16; 5-Element DAB+ Antenna, Nov 15
  19. Advertising Index
  20. Outer Back Cover

This is only a preview of the November 2016 issue of Silicon Chip.

You can view 42 of the 104 pages in the full issue, including the advertisments.

For full access, purchase the issue for $10.00 or subscribe for access to the latest issues.

Items relevant to "50A Battery Charger Controller":
  • 50A Battery Charger Controller PCB [11111161] (AUD $10.00)
  • PIC12F675-I/P programmed for the 50A Battery Charger Controller [1111116A.HEX] (Programmed Microcontroller, AUD $10.00)
  • Firmware (ASM and HEX) files for the 50A Battery Charger Controller [1111116A.HEX] (Software, Free)
  • 50A Battery Charger Controller PCB pattern (PDF download) [11111161] (Free)
  • 50A Battery Charger Controller lid panel artwork (PDF download) (Free)
Items relevant to "Passive Line To Phono Input Converter":
  • Passive Phono To Line Input Converter PCB [01111161] (AUD $5.00)
  • Matte/Gloss Black UB5 Lid for Passive Phono To Line Input Converter (PCB, AUD $5.00)
  • Passive Phono To Line Input Converter PCB pattern (PDF download) [01111161] (Free)
  • Passive Phono To Line Input Converter panel artwork and drilling template (PDF download) (Free)
Articles in this series:
  • Micromite Plus Advanced Programming (November 2016)
  • Micromite Plus Advanced Programming (November 2016)
  • Micromite Plus Advanced Programming, Pt.2 (December 2016)
  • Micromite Plus Advanced Programming, Pt.2 (December 2016)
Items relevant to "Micromite Plus LCD BackPack":
  • Micromite Plus LCD BackPack PCB [07110161] (AUD $7.50)
  • PIC32MX470F512H-120/PT programmed for the Micromite Plus (Programmed Microcontroller, AUD $20.00)
  • Micromite Plus LCD BackPack complete kit (Component, AUD $65.00)
  • Matte/Gloss Black UB3 Lid for 2.8-inch Micromite LCD BackPack (PCB, AUD $5.00)
  • Clear UB3 Lid for 2.8-inch Micromite LCD BackPack (PCB, AUD $5.00)
  • Gloss Black UB3 Lid for 2.8-inch Micromite LCD BackPack (PCB, AUD $4.00)
  • Firmware (HEX) file and documents for the Micromite Mk.2 and Micromite Plus (Software, Free)
  • Micromite Plus LCD BackPack PCB pattern (PDF download) [07110161] (Free)
Items relevant to "WiFi Controlled Switch Using A Raspberry Pi & Smartphone":
  • Script for the Raspberry Pi WiFi Controlled Switch (Software, Free)

Purchase a printed copy of this issue for $10.00.

Micromite Plus Advanced Programming Pt.1: By Geoff Graham The Micromite Plus is not only faster than the Micromite but also boasts more RAM and storage space. It also has several new and important programming features such as SD card support and a graphical user interface (GUI) application library. This makes it easier than ever to develop an interactive touchscreen control panel; in fact, it will take you hours rather than months! T HE MICROMITE Plus has a number of new BASIC commands and features compared to the original Micromite, so even experienced Maxi­ mite/Micromite/BASIC programmers will have some new things to learn before they can take full advantage of its capabilities. So having introduced the Explore 64 module last month and the Explore 100 computer this month, it’s time to get into the nitty gritty of using these amazing new features. This month, we’ll look at the new SD card file reading and writing support and also start delving into advanced GUI programming. In Pt.2 next month, we’ll go over even more advanced techniques to make your GUIs slicker and easier to use, as well as to program. SD card socket Fig.1: this screen shot shows an example of a control panel created by a BASIC program. Each object on the screen was created using a single BASIC command which specified the parameters of the control such as its type, location, dimensions and colour. MMBasic then manage the controls in the background. For example, when a button is pressed, MMBasic will change its appearance accordingly. 58  Silicon Chip Both the Explore 64 and Explore 100 boards are equipped with a microSD card socket which is fully accessible from within a BASIC program. The Explore 100 board can also read and write to full-size SD cards if a display module is attached which has an onboard SD card socket, or if a socket is wired to pin header CON10. Regardless of which type of SD card you’re using, you can have up to five files open at the same time and you can access or write the data sequentially or via random access. This makes the siliconchip.com.au To write text to the file, it’s just a matter of using the PRINT command. For example:    PRINT #2, “This line is saved in the file” Micromite Plus perfect for logging data for later analysis. The files written are compatible with Windows, Mac and Linux systems and to access the data it is as simple as popping the card out of its socket and into another computer. Or you can set up your program to read data off the card later and off-load it to a computer over a USB or serial interface. The commands and functions related to the SD card are summarised in an accompanying panel. In addition, the “Micromite Plus User’s Manual” has further details, so we will just go over the basics here. Reading & writing to an SD card This is the same PRINT command that you use to display data on the console, the difference being that we have specified the file’s identifier as the first argument. As a result, MMBasic will direct the data to the file rather than to the console. The print command is very flexible and by using that one command, you can save any data, including numbers, strings, the contents of variables, etc. When you have finished with the file, you must tell MMBasic to close it. This will flush any buffered data and update the SD card’s file index. For example: CLOSE #2 Reading from a file is similar to writing. First you must open it: OPEN “file.txt” FOR INPUT AS #5 This instructs MMBasic to find the file “file.txt” and prepare it for reading. There are a number of commands that you can use to read data but the easiest to understand is the LINE INPUT command which will read a line (terminated with a carriage return character) from the file and save it in a variable. For example: LINE INPUT #5, D$ The first line from the file will be copied into string variable D$. You can see what is stored in the variable by printing it to the console. For example: PRINT D$ Subsequent reads will move through the file, returning one line each time. You can detect when you have reached the end of the file using the function EOF(#ref), which will return true if the end has been reached. When you are finished with the file, you close it using the CLOSE command described above. The previous examples were for sequential access, where you write new data to the end of the file and read through it sequentially from the start. However, the Micromite Plus also supports random access which allows you to jump around in the file and change or read from any part of it. This is useful if you need to create a simple database or read a file out of sequence – see the SEEK and LOC functions listed in the accompanying panel. Details on how to use these commands are in the Micromite Plus Addendum PDF. Advanced graphics The basic graphic commands and functions that are available on the standard 28 and 44-pin Micromites are also available on the Micromite Plus. In summary, these commands are: CLS – Clear the screen. PIXEL – Set the colour of an individual pixel. LINE – Draw a line on the screen. BOX – Draw a box on the screen. It To record data to the SD card, you first need to open the file for writing. For example: OPEN “file.txt” FOR OUTPUT AS #2 This instructs MMBasic to create the file “file.txt” and prepare it for writing. If the file already exists, it will be overwritten (ie, erased) by this command. If you do not want to overwrite the file, open it “FOR APPEND” which will leave the file as it is and ensure that any new data written will be added at the end. Note that file names must be in the 8.3 format – long file names are not currently supported. The opened file is identified by a number (a “file handle”), in this case #2. You can use any number in the range of #1 to #10 and this number is then used by all subsequent operations to identify the opened file. siliconchip.com.au Fig.2: this is an example of a screen that was constructed using the standard graphics commands of the Micromite family (LINE, CIRCLE, etc). Images like this look very good because they are drawn at high resolution with a wide range of colours. November 2016  59 Fig.3: it’s easy to show a lot of information on a single screen if using a large LCD panel. This is a simulation of an engine monitor and although you cannot see it in the photograph, the meters update in real time with a smooth response. can be optionally filled with another colour. RBOX – Draw a box with rounded corners. This can also be filled with a colour. CIRCLE – Draw a circle with a specified aspect ratio. As with boxes, this can be filled with a specified colour. TEXT – Display text in a specific font with a specified colour. GUI BITMAP – display a bitmap. Using these commands, you can create reasonably advanced graphical displays such as that shown in Fig.2 but it does take a lot of effort. However, the Micromite Plus offers an additional selection of commands that make it much easier to create control/management displays. A good example is shown in Fig.1 which is a demonstration of a pump controller. The on-screen graphic elements (check boxes, switches, etc) are created and managed by MMBasic, which makes writing this type of program much easier. These are known within MMBasic as GUI controls. A control is Fig.4: the SPINBOX control consists of a box displaying a number and up and down arrows at each end. It acts like a potentiometer; when either the up or down arrow is pressed, the number will increment or decrement by a set amount. If the touch is held down, the increment or decrement process will repeat at a fast rate. 60  Silicon Chip an on-screen graphical element which is created by the program but is managed by MMBasic. Spin box An example of a typical control is the “spin box”, as shown in Fig.4. When the user touches the up or down icons, the number in the box will increment or decrement. Holding down either will cause the action to continuously repeat. This control is handy for setting the level of something and is the digital equivalent of a potentiometer. To create this control, the BASIC program uses the following command: GUI SPINBOX #ref, StartX, StartY, Width, Height, FColour, BColour, Step, Minimum, Maximum This takes a number of parameters: (1) #ref: this is a unique reference number in the range of 1-100 that identifies the control. (2 & 3) StartX and StartY: these are the screen coordinates of the top lefthand corner of the control (including the up/down icons). (4 & 5) Width and Height: the dimensions of the control (including the up/ down icons). (6 & 7) FColour and BColour: the colours used for the foreground and background when the control is drawn. (8) Step: this is the amount by which the value in the spin box will change when the up and down icons are touched. It can be a fraction such as 0.1 or a whole number like 5. (9 & 10) Minimum and Maximum: these are the limits for the value in the spin box. When they are reached, the up/down icons will not change the value beyond these limits. They are analogous to the end stops on a potentiometer. When the GUI SPINBOX command is executed, MMBasic will draw the control on the LCD panel and the user can immediately start using it by touching the up/down icons. MMBasic will animate the control by illuminating the touched icons to provide visual feedback, updating the number displayed in the box and making a clicking sound (more on this later). The animation is completely managed in the background by MMBasic. This allows the main BASIC program to be doing something completely different, eg, responding to changes in external sensor inputs. Whenever the BASIC program needs to know the current value in the spin box, it can get this number using the CTRLVAL(#ref) function, where #ref is the reference number given to the control when it was created. For example PRINT CTRLVAL(#40) will display the current value of control number #40 on the console. Often a program will also need to set the number in the spin box to some default value when the program is first run. This can be done with the following command, which can be executed at any time: CTRLVAL(#ref) = number This brief tutorial demonstrates all that is needed to create and use a GUI control within a BASIC program. MMBasic will do all the hard work while your program can be doing something more useful. More controls MMBasic provides 11 different controls, including the spin box. The other controls are: Check Box – this is a check box with a caption. When touched, an X will be drawn inside the box to indicate that this option has been selected and the control’s value will be set to 1. When touched a second time, the check mark will be removed and the control’s value will be zero. Push Button – a momentary button which is a square switch with a caption siliconchip.com.au on its face. When touched, the visual image of the button will appear to be pressed (on) and the control’s value will be 1. When the touch is removed, the image will return to the off state and the value will revert to zero. Switch – the switch control will draw a latching switch with a caption on its face. When touched, the visual image of the button will appear to be pressed and the control’s value will be 1. When touched a second time, the switch will be released and the value will revert to zero. Radio Button – this will draw a radio button with a caption beside it. When touched, the centre of the button will be illuminated to indicate that this option has been selected and the control’s value will be 1. Radio buttons are grouped together when surrounded by a frame (see below) and when one button in the group is selected, all the others in the group will be deselected. If a frame is not used, all buttons on the screen will be grouped together. Frame – a frame is a box with round corners and a caption. It does not respond to touch but is useful when a group of controls need to be brought together. It can also used to surround a group of radio buttons and MMBasic will arrange for the radio buttons surrounded by the frame to be exclusive, as described previously. LED – this is an indicator light (it looks like a panel-mounted LED) with a caption. When its value is set to non-zero it will be illuminated and when it is set to zero, it will be off (a dull version of its colour). If needed, the colour of the LED can be changed on the fly. The LED graphic does not respond to touch. Display box – a box with rounded corners containing some text. Any text can be displayed in the box by using the CTRLVAL(#ref) = command. It does not respond to touch and is useful for displaying text, numbers and messages. Caption – this will draw a text string on the screen. It is similar to the basic drawing command TEXT, the difference being that MMBasic will automatically manage this control by dimming or hiding it when needed. Text Box – an advanced control that allows the user to enter text via an onscreen QWERTY keyboard. Normally, this control is just a rounded box containing some text but when touched, siliconchip.com.au SD Card Functions MMBasic on the Micromite Plus supports the standard BASIC commands for working with storage systems. This is a brief summary; the “Micromite Plus Addendum” PDF goes into more detail: • • OPEN fname$ FOR mode AS #fnbr – open a file for reading or writing. • INPUT #fnbr, list of variables – read a list of comma separated data into the variables specified from the file previously opened as #fnbr. • LINE INPUT #fnbr, variable$ – read a complete line into the string variable specified from the file previously opened as #fnbr. • CLOSE #fnbr [,#fnbr] . . . – close the file(s) previously opened with the file number ‘#fnbr’. PRINT #fnbr, expression [[,; ]expression] . . . etc – output text to the file opened as #fnbr. Programs and images can be loaded from the SD card while programs can also be saved: • LOAD fname$ [, R] – load a BASIC program from the SD Card. “,R” will cause the program to also be run. • • SAVE fname$ – save the current program to the SD card. LOAD IMAGE filename$ [, StartX, StartY] – loads a BMP image from the SD card and displays it on the attached LCD display. Basic file and directory manipulation can be done from within a BASIC program: • FILES [wildcard] – search the current directory and list the files/directories found. • • • • • KILL fname$ – delete a file in the current directory. MKDIR dname$ – make a sub-directory in the current directory. CHDIR dname$ – change into to the directory dname$. RMDIR dir$ – remove or delete the directory “dir$” on the SD card. SEEK #fnbr, pos – will position the read/write pointer in a file that has been opened for RANDOM access. There are also a number of functions that support the above commands: • INPUT$( nbr, #fnbr ) – will return a string composed of “nbr” characters read from a file previously opened for INPUT. • DIR$( fspec, type ) – will search an SD card for files and return the names of entries found. • EOF( #fnbr ) – will return true if the file with the file number #fnbr is positioned at the end of the file. • LOC( #fnbr ) – for a file opened as RANDOM, this will return the current position of the read/write pointer in the file. • LOF( #fnbr ) – will return the current length of the file in bytes. a full QWERTY keyboard will appear and all other controls will be dimmed and disabled – see Fig.5. Using this virtual keyboard, any text can be entered into the box, including upper/lower case letters, numbers and any other characters in the ASCII character set. Number Box – a number box is similar to the text box described above except that when touched, it will display a numeric keypad on the screen. Using this virtual keypad, any number can be entered into the box including a floating point number in exponential format. The new number will replace the number previously in the box. Click sound When a control is touched, it is animated by MMBasic to provide visual feedback to the person touching it. To add to the impression that this is a November 2016  61 Fig.5: the Text Box control displays a full on-screen QWERTY keyboard when touched. This allows the user to enter any text using the full ASCII character set, including numbers and punctuation. The up arrow will shift lower/upper case and the “&12” key will change the keyboard to a number and punctuation layout. Note that the other objects on the screen are automatically dimmed to indicate that they cannot be used while the keyboard is on the screen. physical object, MMBasic can also generate a click sound at the same time. This is done by adding a standard piezo buzzer to an I/O pin and telling the Micromite Plus the pin number in the OPTION TOUCH command. Then, whenever a touch-sensitive control is touched, MMBasic will generate a short pulse on that pin to produce a simulated click sound. Transistor driver The I/O pins on the Micromite Plus do not have sufficient drive capability for most piezo buzzers, so you should use a transistor as the driver. The Explore 100 does this and it provides a good example of how to implement this feature. Reference numbers All controls are identified with a reference number when first created. This number is then used whenever you want to do something associated with the control. The number must be in the range of 1-100 which caters for up to 100 simultaneously active controls in a program. For example, you might create a switch with a reference number of 41 and then later hide it: GUI SWITCH 41, c$, x, y, etc GUI HIDE 41 In a program with a lot of controls, using simple numbers can be confusing. For example, what do controls that have been designated 87 and 41 do? For this reason, it is good practice to define the control reference numbers as a constant with a meaningful name. 62  Silicon Chip You can then use the name throughout your program and it will be obvious to the casual reader what the control does. For example: CONST PwrSwitch = 41 GUI SWITCH PwrSwitch, c$, x, y, etc GUI HIDE PwrSwitch Interacting with controls Most controls have a value which can be read and set. For example, you can read the value of a check box with the CTRLVAL(#ref) function. You can also set the value by assigning a value to the function (ie, using it as a command). For example: CTRLVAL(#ref) = 1 will set the value of the check box to true and cause the visual image of the check box to be checked – just as if the user had touched the on-screen check box. This is useful when setting defaults and interacting with other controls. The value returned by this function depends on the control; for some it is a number and for others it is a string. MMBasic will automatically return the correct type of data and will also expect the correct type of data when YouTube Video The author has produced a video which describes and demonstrates the capabilities of the Micromite Plus. You’ll find it at: https://youtu.be/j12LidkzG2A you are setting a value. For example, setting the value of a frame will change the caption of the frame (which is a string) and therefore you must supply a string. Modifying a control There are a range of commands and functions that you can use to modify a control after it has been created. They include: GUI FCOLOUR – changes the foreground colour of the control. This is especially useful for the LED control. GUI BCOLOUR – changes the background colour of a control. GUI DISABLE – disables one or more control(s). Disabled controls do not respond to touch and will be dimmed on the screen. GUI ENABLE – undoes the effects of GUI DISABLE and restore the control(s) to normal operation. GUI HIDE – hides one or more control(s). Hidden controls will not respond to touch and will be replaced on the screen with the current background colour (ie, they are erased). GUI SHOW – undoes the effects of GUI HIDE and restores the controls to full visibility and normal operation. GUI DELETE – deletes one or more controls. This includes removing the image of the control from the screen and freeing the memory used by the control. Next month That’s it for now. Next month, we’ll get into more advanced topics such as touch interrupts, screen pages and SC message boxes. siliconchip.com.au