Friday, 9 November 2018

SQL delimited string to table function

Sometimes data suppliers do not make their data easy to separate. Other times you might want to split some other data up.

This SQL Table function will take a string comprising a list of substrings with a specified specified delimiter and returns a table containing the substrings

-- =============================================
-- Author: Technology Is Not Dull
-- Create date: 19/01/2016
-- Description: Takes a delimited string list and returns a table
-- =============================================
CREATE FUNCTION [dbo].[stringToTable]
(
@string VARCHAR(200),
@delimiter char(1)
)
RETURNS
@rettable TABLE
(
stringbit varchar(50)
)
AS
BEGIN
--***# select * from dbo.stringToTable(POLYETHYLENE GLYCOL 3350; POTASSIUM CHLORIDE; SODIUM BICARBONATE; SODIUM CHLORIDE; SODIUM SULFATE ANHYDROUS',';')
DECLARE @start INT, @end INT
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
    WHILE @start < LEN(@string) + 1 BEGIN
        IF @end = 0
            SET @end = LEN(@string) + 1
     
        INSERT INTO @rettable (stringbit)
                  VALUES(SUBSTRING(@string, @start, @end - @start))
        SET @start = @end + 1
        SET @end = CHARINDEX(@delimiter, @string, @start)
     
    END
    RETURN
END
GO

Tuesday, 6 November 2018

Circuitverse - online digital logic simulator

Circuitverse is a free online digital logic simulator developed by students at IIIT Bangalore.

It allows the building of digital logic systems in the comfort of your own browser.

The user can select various input and output devices plus logic gates, wire them up and then test them.

The circuits can be saved, and images of the circuits produced. The circuits can be Public, which allows them to shared, useful for distance learning.

Previously saved circuits can be retrieved, but you need to click on the user name to get to the menu with the "My Circuits" option.

The developers say that though you could undertake a full CPU implementation, it is really designed for educational work.

It is certainly an interesting piece of software, and is happy running on a Raspberry Pi 3.

Monday, 5 November 2018

Circuitverse - image for something else

Circuitverse.org is an an online digital logic simulator that allows you to build digital circuits in your browser.

I will be coming back to this later, but I needed somewhere to host an image.
There are two circuits, the top right one is showing a plain NAND gate, the bottom one builds an OR gate from three NAND gates.

This is for the FutureLearn How Computers Work course, from the Raspberry Pi Foundation.

Monday, 22 October 2018

Browser size - Javascript

Sometimes when you are building a web page it is useful to know what the browser size is, so you check it will display correctly if it is on a desk top, lap top, tablet or mobile.

The following Javascript will put the browser client size as the title.
<html>
<head>
    <script>
        function titleIsSize() {
            document.title = "" + document.body.clientWidth + " pixels.";           
        }
    </script>
</head>
<body  onresize="titleIsSize()"  >
</body>
</html>

Monday, 15 October 2018

Raspberry Pi sound not working on HDMI

By default the Raspberry Pi will channel sound through the HDMI connection to a suitable television/monitor.

Most of the time that will work automatically (if it thinks the television/monitor is not equipped with a speaker or speakers, it routes it through the headphone socket).

The key thing is the "sometime".

Some television/monitors do not appear to tell the Raspberry Pi they have a speaker. This may be due to the monitor, or due to the HDMI cable in use.

The easiest way of forcing the sound to be output via one of the channels is to right click on the loudspeaker icon. The drop down will show available audio output devices, the tick indicating the selected one. Make your choice there.

You can also use the command line:
amixer cset numid=3 2

The parameter 2 sets output to HDMI, 1 sets it to the headphone socket and 0 sets it to default.

It is also possible using the configuration screen and by editing the /boot/config.txt file. See the reference for details.

References:
https://www.raspberrypi.org/documentation/configuration/audio-config.md

Wednesday, 19 September 2018

CircuitPython: writing to the file system.

Circuitpython has a very easy way of uploading code and data to the microcontroller, the microcontroller's storage appears as a USB storage device on the host's operating system. To move code and data to the microcontroller, you simply copy everything to the volume.

Unfortunately this does mean that, by default, Circuitpython cannot write data to its own storage (it is read only) and so it cannot write data that will visible to the host.

However it is possible to change that in the optional boot.py code file.

The boot.py file is only run on the first boot of the device (power up) and not when REPL is restarted or when you save a file. The microcontroller needs to be Ejected as a USB device and the reset button pressed.

Note: When you change it from read only, it is not possible to update the code on the microcontroller - including the boot.py.

The boot.py file needs to contain the following code:
import storage
storage.remount("/", False)

The first line imports the storage module, allowing access to OS functions.
The second line remounts the storage with readonly set to false.

The boot.py file can be "removed" via REPL (Read - Evaluate - Print - Loop). This is very like a command line interface combined with an (xPython) interpretor.

To access REPL on Mu:
Click on the Serial button on the ribbon. This will open a serial connection to the connected microcontroller.
Press CTRL-C (keyboard interrupt) to stop CircuitPython from continuing with whatever it is doing.
Press any key (as instructed).
Use the REPL.

To remove the boot.py, you need to rename the file via REPL:
import os
os.listdir("/")
os.rename("/boot.py", "/boot.bak")

The first line imports the storage module, allowing access to OS functions.
The second lists the contents of the root of the microcontoller's storage.
The third renames the boot.py file to boot.bak.

The microcontroller needs to be ejected (as a USB device) and the reset button pressed (physically unplugging it after ejecting it will also work).

References

https://codewith.mu/en/tutorials/1.0/repl
https://learn.adafruit.com/cpu-temperature-logging-with-circuit-python/writing-to-the-filesystem
https://circuitpython.readthedocs.io/en/2.x/shared-bindings/storage/__init__.html

MU Python, Micropython and CircuitPython editor

The Micro:bit can be programmed offline using the Mu editor. Until recently the editor available on the Raspbian repository has been an earlier version without support for the Radio module.

For a Raspberry Pi, Mu is now included in the Recommended Software option.

Instructions for installation are listed in the references.

References:

https://codewith.mu/
https://www.raspberrypi.org/blog/mu-python-ide/
https://projects.raspberrypi.org/en/projects/getting-started-with-mu