Tag Archives: programming

Tunebook generator

2023-01-11 Updated the script! More functions

I’m using below scripts to generate tunebooks.
These books I can print OR view on a tablet using my DIY bluetooth page turner. ( see other post )

I often work on tunes, add notes, text or write other versions.
So i needed a fast and simple way to re-generate a tunebook. ( hence the date on the title page and in the name, so i know whats the most recent version )
Now i have a separate tunebook for each instrument, with the same looks

What does this script?

  • Generates a title page using Latex
  • Generates a tune index, with page numbers ( works with multipage tunes )
  • Adds bookmarks to the tunes, so you can use the bookmark link in your reader.
  • Merges all pdf’s into one.
  • Sets title/author
  • Generic setup for multiple tunebooks
  • Generates ABC pdf and includes these

Todo: embed page numbers on every page?

BASH script:

#!/bin/bash
#sudo apt-get install texlive-latex-extra

### REMOVE OLD BOOKMARKS
ls *pdf | while read; do
rm -f "/tmp/$$.pdf"
cp "$REPLY" /tmp/$$.pdf
pdftk A=/tmp/$$.pdf cat A1-end output "$REPLY"
done


### GENERATE ABC 
if [ -d abc ] ; then
ls abc/*abc | while read abc ; do  abcm2ps -x -O - "$abc" | ps2pdf  -sPAPERSIZE=a4 - "$(echo  $abc | cut -f2 -d/ | sed 's/abc/pdf/g')" ;done
fi


nrpdf=$(ls *\.pdf | grep -vi index.pdf | grep -vi title.pdf | grep -v ^00  | wc -l)
echo "Tune PDFs in directory : $nrpdf"
pages=$(( $nrpdf / 126 ))
pages=$(( $pages + 1 ))
echo "Needed index pages : $pages"

# Extra pages : Number 002-999
extrapages=$( ls *pdf | egrep  ^00 | wc -l)
echo "Extra pages : $extrapages"

nr=$((  $extrapages  ))
echo "Total pages for tunes : $nr"
echo "create column page as text"
nr=$(( $nr +1 ))
echo $nr
ls *\.pdf | grep -vi index.pdf | grep -vi title.pdf | grep -v ^00 | sort | while read ; do 
echo "$nr $REPLY" 
next=$(exiftool "$REPLY" | awk -F": " '/Page Count/{print $2}')
nr=$(( $nr + $next ))
done | cut -f1,3- -d" " | cut -f1 -d. | sed s/\ a$//g | sed s/\ b$//g | pr -2 -t > /tmp/col
echo "Create Index pdf"
vim /tmp/col -c "set printfont=courier:h12"  -c":let &printheader = \" \""   -c "hardcopy > 001aIndex.ps | q"; ps2pdf 001aIndex.ps 

echo "Create title page pdf"
pdflatex "000 title.tex" 1>/dev/null

tempPDF=`mktemp`

echo -n "Add bookmarks : "
ls *\.pdf | grep -vi index.pdf | grep -vi title.pdf | grep -v ^00 | sort | while read i ; do
    bookmarkTitle=$( echo $i | cut -f2- -d" " | rev | cut -f2- -d. | rev)
    bookmarkInfo="BookmarkBegin\nBookmarkTitle: $bookmarkTitle\nBookmarkLevel: 1\nBookmarkPageNumber: 1"
    pdftk "$i" update_info_utf8 <(echo -en $bookmarkInfo) output $tempPDF >/dev/null
    mv $tempPDF "$i"
    echo -n "."
done
echo ""
set +x
#ls *\.pdf | grep ^[A-Z] | sort > /tmp/pdflist
name=$(pwd | rev | cut -f2 -d/ | rev)
echo $name
pdftk *pdf cat output "../${name}_$(date +%Y%m%d).pdf"
exiftool -Author="Henri Aanstoot" -Title="$name" "../${name}_$(date +%Y%m%d).pdf"

Needed tex file (named “000 title.tex”)

\documentclass[11pt]{report}
\usepackage[T1]{fontenc}
\usepackage{anyfontsize}
\usepackage[a4paper, total={6in, 8in}]{geometry}

\usepackage[T1]{fontenc}
\usepackage{tgbonum}

\newlength{\drop}

\begin{document}
  \begin{titlepage}
    \drop=0.1\textheight
    \centering
    \vspace*{\baselineskip}
    \rule{\textwidth}{1.6pt}\vspace*{-\baselineskip}\vspace*{2pt}
    \rule{\textwidth}{0.4pt}\\[\baselineskip]
    {\fontsize{50}{60}\selectfont Irish Tunes Test Tunebook}
    \rule{\textwidth}{0.4pt}\vspace*{-\baselineskip}\vspace{3.2pt}
    \rule{\textwidth}{1.6pt}\\[\baselineskip]
    \scshape
	{\large Tunebook}\par
    \vspace*{2\baselineskip}
    {\itshape Henri - Exampletunes\par}
    \vfill
    {\scshape generated:} \\
    \today
  \end{titlepage}
\end{document}

File naming:

000 title
001 index
002 – 099 xyz (extra pages, not in index)
100 – 999 Tunes (sorting)

000 title.pdf
001aIndex.pdf
002 tuneinfo.pdf
100 The Battle of Aughrim.pdf
101 I was born for sports.pdf
105 Cerlew Jig.pdf
110 Chanters Song.pdf
115 Gander at the Pratie Hole.pdf
120 honeymoon.pdf
125 Kitty Goes a-Milking.pdf
130 Terribus.pdf

Output from the script

 ./generatebook
Tune PDFs in directory : 8
Needed index pages : 1
Extra pages : 1
Total pages for tunes : 3
create column page as text
Create Index pdf
Create title page pdf
Add bookmarks : ........

Needed software:

pdftk, pdflatex, vim, exiftool, texlive-fonts-recommended

Assembly in Dosbox, and draw a line

Below my setup in Dosbox.
(As used 30 years ago)

First install dosbox (Linux/Windows)

download the package with used files and compilers from here:
https://media.henriaanstoot.nl/assembly.tgz

extract with tar xzvf /tmp/assembly.tgz to a directory

start dosbox and mount the directory as C

mount c /path/assembly

Run “a line”, this a batchfile which starts the editor (qedit)
When closing the file (esc – q menu)
It will compile the assembly and write out a executable

This is the batchfile

@echo off
q %1.asm
cls
masm %1.asm;
link %1.obj;
exe2bin %1.exe %1.com
echo READY!

line assembly code

    NAME lijnentrekroutine

.286

Code SEGMENT
    ASSUME CS:Code,DS:Code
org 100h
Start:
    mov ax,13h            ;set video mode
    int 10h

    mov bx,100
    mov cx,100
hiero:
    mov dx,0a000h
    mov es,dx
    mov ax,320
    mul cx
    add ax,bx
    mov di,ax
    mov al,2
    stosb
    inc bx
    inc bx
    inc cx
    cmp bx,150
    jnz hiero


    mov ah,8
    int 21h
    mov ax,3
    int 10h

    MOV AX,4C00h
    INT 21h

code ends
end start

Hercules to VGA

While playing with MuseScore….
(Typesetting some scores for Pipes and Flute)

This came in: WOOOT

Trident 8900C (1024 x768 max 512Kb)

This is a Trident VGA card. While having a 16bit ISA connector, it can work in a 8bits ISA slot.

A while ago i bought a Laser XT/3, that’s the one my parents had.
This is where i did a lot of assembly programming on.
It’s a 8086 cpu, 640K and has a Hercules/CGA graphics card.

I found loads of assembly files and i want to see if i can get it running again.
While some code was written for hercules, ( That’s the monochrome image you see in the example above ) and a few for EGA (4 colors).

Most of it was written for VGA. Probably on a later machine like a 80386?

But i know there are vga cards for 8 bit msdos computers, and i found one. ( This one is even autodetect, so no jumpers to figure out)

So i’ve put this card in the machine, turned it on, and it works!
I’ve got only 2 examples living on the harddisk of the machine, both black and white … 🙂
I have to search for interesting code in hundreds of files.

Some friends of mine, picture was taken from an amiga genlock digitizer
The intro pages of a “amiga emulator” WHERE is the rest??? (end is a cga starfield demo)

Hercules Card

There is not much info available about this card:

  • Max resolution (Hercules) : 720×348
  • 15 pin analog monitor port (CN1)
  • BIOS enabled JP1 Pins 1 & 2 closed
  • BIOS disabled JP1 Pins 2 & 3 closed
  • CGA selected SW1 On
  • MDA (Hercules) selected SW1 Off

Floppy drive boot

My friend EDK and I made some demo’s like

And a boot demo, which was able to start from a bootsector, went into a graphic mode and ran a demo with sound. Edk wrote a sector loader for this.
I have some 5.25 inch floppy disks, labelled boot demo. So i wanted to try this today …
I needed to change the boot order, so i went online to search for jumper settings.

I see a led when it tries to boot, but my disks are probably formatted 720Kb instead of 360Kb, which this drive is.

So …. TODO!

Find a 720Kb floppy drive (5.25 inch), and sort through my code!
There is a 8bit soundblaster compatible soundcard that i bidding on online, hopefully i’ll get it

Assembly and modes

I wasn’t sure how to sort the assembly code into Hercules and VGA compatible, but i used this table (There are also extended modes for higher resolutions)

mode 0x00text 40×25 gray
mode 0x01text 40×25 16 colors
mode 0x02text 80×25
mode 0x03text 80×25 16 color
mode 0x04graphics mode (CGA) 320×200
mode 0x05graphics mode (CGA) 320×200
mode 0x06graphics mode (CGA) 640×200 (B/W)
mode 0x07text 80×25 Hercules
mode 0x0Fgraphics mode 640×350? gray
mode 0x10graphics mode 640×350?
mode 0x11graphics vga 2 colors
mode 0x12graphics vga 16 colors
mode 0x13graphics 320×200 256 colors
# Set VGA mode
    mov ax,13h
    int 10h         ;screen 320x200 256 colours

# Exit VGA mode
    mov ax,3
    int 10h         ;screen  80x25 text
    mov ax,4c00h
    int 21h         ;back to DOS

Shelly Flood Sensor

I’ve had this Shelly sensor for a long time. But never posted anything about this.
Last weekend we had a -situation- in our kitchen, so what better time to test this device again!

This little disc shaped device has three metal points on its bottom side, those are the flood (water) sensors.
It stay’s in sleep mode when all’s good.
It does several things when it detects water.

  • Emits a alarm signal
  • Wakes-up wifi
    • Sends a MQTT message (when not connected to the cloud like i have)
      MQTT is a alarm message AND it wil send the temperature of the device!
  • After a while (when dry) goes back to sleep

There are connection point on the print you can use .. happy hacking!

My node-red configuration

Above is the part where the mqtt messages gets processed by Node-Red
Sending it to PushOver and my little MqttLcdNotifier

Above is the MqttLcdNotifer .. there are several parts to this

  • Top line is from shelly flood and other notifications
  • Text input puts text from the NR GUI on my TV and the LCDDisplay
    • same parts are being used by my 3D printer when the print tool is getting TO hot, or printing is finished
  • Trigger at work WAS a notification for work .. nonfunc
  • mqttlcd-button is the mqtt message send from the display (the one that i was pushing) to stop the beeping and clears the display
  • Bash notify, is as previously posted a flow which i can control from my linux machines notify “compiling complete” for example.
    This is also being broadcast from my livingroom using speakers.
    (See separate post about this)

Wellll, put this in place 2 years ago, never looked at it again .. still works

Volume is low, due to alarm sounds 🙂

Quick n easy media viewer in a browser

Februari 2021 i made a website to view images and movies in a browser to do some quick sorting. (borrowed some code from a codepen page i recall correctly)
At the time i didn´t have a good way to view webp webm media.
I wanted to view multiple files at the same time, and make it short and simple.

BTW no webserver needed, just open the file from a directory!
jpg’s png’s webm webp mp4 svg and animated gifs work. (maybe more, didn’t test more, whatever your browser supports)

With recent updates of the chrome browser the video attributes to mute is broken, i so made a workaround.
Also everything is in one file now.
Except for one issue .. i couldn´t create one file for images AND videos.

There is a piece of javascript i could not fix … yet
I have to do execute a document.createElement which is different for images and videos.
Also the attributes of video are mute,autoplay,loop,playinline

Examples:

index.html

<a href="video.html">Video version</a>
<div id="drop-area">
  <form class="my-form">
    <label class="button" for="fileElem">Drop some images</label>
  </form>
  <div id="gallery" /></div>
</div>
<style>
body {
  font-family: sans-serif;
}
#drop-area {
  border: 2px solid #ccc;
}
.my-form {
  margin-bottom: 10px;
}
#gallery {
  margin-top: 10px;
}
#gallery img {
  width: 300px;
}
}
#fileElem {
  display: none;
}
</style>
<script>
let dropArea = document.getElementById("drop-area")
;['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
  dropArea.addEventListener(eventName, preventDefaults, false)   
  document.body.addEventListener(eventName, preventDefaults, false)
})
;['dragenter', 'dragover'].forEach(eventName => {
  dropArea.addEventListener(eventName, highlight, false)
})
;['dragleave', 'drop'].forEach(eventName => {
  dropArea.addEventListener(eventName, unhighlight, false)
})
dropArea.addEventListener('drop', handleDrop, false)
function preventDefaults (e) {
  e.preventDefault()
  e.stopPropagation()
}
function highlight(e) {
  dropArea.classList.add('highlight')
}
function unhighlight(e) {
  dropArea.classList.remove('active')
}
function handleDrop(e) {
  var dt = e.dataTransfer
  var files = dt.files
  handleFiles(files)
}
function handleFiles(files) {
  files = [...files]
  files.forEach(previewFile)
}
function previewFile(file) {
  let reader = new FileReader()
  reader.readAsDataURL(file)
  reader.onloadend = function() {
    let img = document.createElement('img')
    img.src = reader.result
    document.getElementById('gallery').appendChild(img)
  }
}
</script>

video.html

<a href="index.php">Images version</a>
<div id="drop-area">
  <form class="my-form">
    <label class="button" for="fileElem">Drop some videos</label>
  </form>
  <div id="gallery" /></div>
</div>
<style>
body {
  font-family: sans-serif;
}
#drop-area {
  border: 2px solid #ccc;
}
.my-form {
  margin-bottom: 10px;
}
#gallery {
  margin-top: 10px;
}
#gallery video {
  width: 300px;
}
}
#fileElem {
  display: none;
}
</style>
<script>
let dropArea = document.getElementById("drop-area")
;['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
  dropArea.addEventListener(eventName, preventDefaults, false)   
  document.body.addEventListener(eventName, preventDefaults, false)
})
;['dragenter', 'dragover'].forEach(eventName => {
  dropArea.addEventListener(eventName, highlight, false)
})
;['dragleave', 'drop'].forEach(eventName => {
  dropArea.addEventListener(eventName, unhighlight, false)
})
dropArea.addEventListener('drop', handleDrop, false)
function preventDefaults (e) {
  e.preventDefault()
  e.stopPropagation()
}
function highlight(e) {
  dropArea.classList.add('highlight')
}
function unhighlight(e) {
  dropArea.classList.remove('active')
}
function handleDrop(e) {
  var dt = e.dataTransfer
  var files = dt.files
  handleFiles(files)
}
function handleFiles(files) {
  files = [...files]
  files.forEach(previewFile)
}
function previewFile(file) {
  let reader = new FileReader()
  reader.readAsDataURL(file)
  reader.onloadend = function() {
    let video = document.createElement('video')
    video.setAttribute('volume', '0');
    video.setAttribute('height', '300');
    video.setAttribute('autoplay', true);
    video.setAttribute('loop', true);
    video.setAttribute('playsinline', true);
    video.setAttribute('oncanplay', 'this.muted=true');
    video.src = reader.result
    document.getElementById('gallery').appendChild(video)
  }
}
</script>

Motion detection for stupid webcams

Last year i made a script for a friend who wanted to detect visually if his garden sprinkler was on or off. A few days ago i saw someone who wanted to see if things where moving in his house. (didn’t trust his landlord i think)
But he only had a dumb/simple/cheap camera .. so it had no motion detection.

I was thinking of my script, and could easily adapt it for this usage.

Most ipcams have somekind of URL/API you can use to capture a image.
Some examples below

# Reolink
wget "http://ipnumber/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=randomstring&user=user&password=password" -O image.jpg

# Foscam
wget "http://ipnumber:88/CGIProxy.fcgi?cmd=snapPicture2&usr=user&pwd=password&cnt=randomstring" -O image.jpg

# Edimax
wget "http://user:password@ipnumber/jpg/image.jpg" -O image.jpg

# Hikvision
wget "http://user:password@ipnumber/ISAPI/Streaming/channels/101/picture" -O image.jpg

So using below script i can capture a image, compare it to the previous, and when it’s above a certain threshold sends a email.

#!/bin/bash
# Only uses wget and image-magick
treshhold=500
fuzzyness=20%
# CHANGE WEBCAM THINGY TO OWN URL AND CREDENTIALS
wget -q "http://webcamip/cgi-bin/api.cgi?cmd=Snap&channel=0&user=user&password=password" -O previous.jpg
while true;  do
wget -q "http://webcamip/cgi-bin/api.cgi?cmd=Snap&channel=0&user=user&password=password" -O current.jpg
value=$(compare -fuzz $fuzzyness previous.jpg current.jpg -metric mae diff.jpg 2>&1 | cut -f1 -d.)
if [ $value -gt $treshhold ] ; then
echo "ping $treshhold"
echo "Something moved" | mail -s "Movement" user@example.com -A diff.jpg
fi
# Comment below if you want to compare against a base line .. not previous image
cat current.jpg > previous.jpg
sleep 60
done

Example previous picture

Example current picture

I got mailed with result

Hints tips:

Use crop to detect only a part.

copy current.jpg to a second file
Use painting black a part and compair with different treshhold fuzzyness to get different hotspots.

Below detects RED, use above ide with crop to detect red/green/blue leds

compare -verbose -metric mae 1.jpg 2.jpg /tmp/1.diff
1.jpg JPEG 2560x1920 2560x1920+0+0 8-bit sRGB 248819B 0.050u 0:00.057
2.jpg JPEG 2560x1920 2560x1920+0+0 8-bit sRGB 248949B 0.030u 0:00.137
Image: 1.jpg
  Channel distortion: MAE
  Channel distortion: MAE
    red: 12517.5 (0.191005)
    green: 11967.1 (0.182607)
    blue: 12492.8 (0.190628)
    all: 12325.8 (0.18808)
1.jpg=>/tmp/1.diff JPEG 2560x1920 2560x1920+0+0 8-bit sRGB 1.19495MiB 1.470u 0:00.197

Glade and python – network tester

THIS IS A WORK IN PROGRESS ! .. Updates follow

UPDATE: Found a glade project from 2002
https://www.henriaanstoot.nl/2002/02/20/reverse-engineering-a-alpha-ticker-led-scoller/

The goal of this project is to have a raspberry-pi with a screen wich shows network information.
It wil be using a battery, touchscreen .. maybe some status leds.
When debugging network issues we want to have information when/if/how a network port works on our switches.

It should show:

  • dhcp ip
  • gateway
  • can access internet?
  • speedtest
  • detect if vlan tagged network packets are present on the port?
  • icmp test
  • list of detected nearby hosts?

A long time ago i played with glade and C / Perl.

But i’d rather use python so i’m looking into glade/python combi for this little project.

Glade is a gnome/GTK user interface RAD tool. (Rapid Application Development)

i’ve used zenity and yad before to create simple gui’s for bash scripts, these where only for quick and dirty solutions. (See other posts)
Glade is a far better solution, but a little harder to use.

Below is a little framework i started with

Python script

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class Handler:
    def onDestroy(self, *args):
        Gtk.main_quit()

    def on_firstbutton_clicked(self, button):
        print("Ping test")

builder = Gtk.Builder()
builder.add_from_file("mytest.glade")
builder.connect_signals(Handler())

window = builder.get_object("Main")
window.show_all()

Gtk.main()

Glade file

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="Main">
    <property name="can-focus">False</property>
    <property name="title" translatable="yes">Networktool</property>
    <property name="default-width">440</property>
    <property name="default-height">250</property>
    <property name="icon-name">network-wired</property>
    <child>
      <object class="GtkFixed" id="fixed1">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <child>
          <object class="GtkButton" id="firstbutton">
            <property name="label" translatable="yes">Ping test</property>
            <property name="width-request">100</property>
            <property name="height-request">16</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <signal name="clicked" handler="on_firstbutton_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">56</property>
            <property name="y">40</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="speedtest">
            <property name="label" translatable="yes">Speed test</property>
            <property name="width-request">100</property>
            <property name="height-request">16</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
          </object>
          <packing>
            <property name="x">56</property>
            <property name="y">89</property>
          </packing>
        </child>
        <child>
          <object class="GtkTextView">
            <property name="width-request">179</property>
            <property name="height-request">166</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
          </object>
          <packing>
            <property name="x">222</property>
            <property name="y">36</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Which is generated using the Glade designer

When running you get below screen

Old glade program (2003)

Energy bill and ledserver

Having a lot of devices and running a Lab wil use a lot of energy. Now with the energy crisis in Europe, i had to take a closer look at whats using power in my house.

I notished some weird usage patterns while measuring.

I’m using a few shelly power plugs, to measure devices and powerstrips.

With these devices you can control devices connected to it.
On/Off/Timer etcetera.
It wil measure the power usage in watts, and it even got a temperature sensor.
I like the fact that it perfectly integrates into your home automation using an extensive API.
curl commands to controll, and even MQTT messaging. Intergrating in Home Assistant is a breeze.

So i was monitoring a bunch of stuff using Nodered/Grafana/Homeassistant and saw some recurring usage.
But being always late to check things, i made use of my ledserver i’ve build a long time ago.

This ledserver consists of a Raspberry Pi Zero, with a led string and a API written in python.

Below is autostarted on the Raspberry

( I made this ledserver for work, it showed the status of servers and services. Beside that every colleage had a range which he could use for his own scripts. I made some little bash script templates to have led funtions standard in your bash profile.

#!/usr/bin/python


# apt-get install python-flask
#

import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI
import struct

from flask import Flask, render_template, request
app = Flask(__name__)

PIXEL_COUNT = 32
SPI_PORT   = 0
SPI_DEVICE = 0

pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))

pixels.clear()
pixels.show()

@app.route("/led/<deviceName>/<color>")
def action(deviceName, color):
        if deviceName == 'reset':
                print ("reset")
                pixels.clear()
        print (deviceName)
        led = int(deviceName)
        s = color
        r = int(s[ :2], 16)
        b = int(s[2:4], 16)
        g = int(s[4: ], 16)

        pixels.set_pixel_rgb(led, r,g,b)
        pixels.show()

        templateData = {
                'rled'  : r,
                'bled'  : b,
                'gled'  : g,
                'deviceName'  : deviceName,
        }
        return render_template('index.html', **templateData)

@app.route("/control/<controlcommand>")
def actioncommand(controlcommand):
        if controlcommand == 'clear':

                print("clear")
                pixels.clear()
                pixels.show()

        templateData = {
                'controlcommand'  : controlcommand,
        }
        return render_template('index.html', **templateData)

@app.route("/range/<start>/<stop>/<color>")
def rangecommand(start,stop,color):
        s = color
        r = int(s[ :2], 16)
        b = int(s[2:4], 16)
        g = int(s[4: ], 16)
        startled = int(start)
        stopled = int(stop)
        while (startled < stopled):
          pixels.set_pixel_rgb(startled, r,g,b)
          startled=startled + 1
        pixels.show()

        templateData = {
                'rangecommand'  : rangecommand,
        }


        return render_template('index.html', **templateData)



if __name__ == "__main__":
   app.run(host='0.0.0.0', port=8080, debug=True)

Now you can control the leds with a simple curl command:

curl http://ledserver:8080/range/startled/endled/colorinrgb
curl http://ledserver:8080/led/lednumber/colorinrgb
curl http://ledserver:8080/control/clear

So today i made a little script to show power usage.

I’m reading the current power usage from a LS120 Youless

Youless LS120 device, which you can connect to your P1 connector.

With below bash script i’m reading the webinterface and update the ledstring.
I was using this ledserver for general notification usage. Below a 2 minute hack ..

#!/bin/bash
while true; do
number=$(echo $(curl -s http://youlessip/a | grep Watt | head -1 | awk '{ print $1 }') / 100  | bc)
curl -s http://ledserver:8080/control/clear
curl -s http://ledserver:8080/range/00/$number/010101
sleep 10
done
Using 9 leds = 9xx watt

IRQ redirect for other programs

I’ve got a ROM in my 6502 which can load programs at $0200.
When running own programs i want to use IRQ’s, but my rom is also using IRQ routines.
So i was wondering if i could ‘hijack’ this IRQ for own program purposes.

So i’ve altered the rom to use a vector in userspace.

  • CPU starts, getting vector from FFFC
  • Goto $8000 main ROM program
  • Setting a jmp routine on zero page $F0
    • 4C C9 FF
    • 4C C9 FF ; second time, first one will redirect
  • Running my program on $0200
  • Change first jmp C9 FF to my own IRQ part
    • Changed jmp vectors
    • 4C 6E 02 ; jmp $026E (myprg)
    • 4C C9 FF ; jmp $FFC9 (rom)
  • Run rest of program

All seems fine and dandy … buzzer is sounding, but no blinky leds.
When flashing the rom with only my program, everything works ..
So whats going on? .. anyone?

00000000  78 a9 6e 85 f1 a9 02 85  f2 58 a9 ff 8d 03 50 8d  |x.n......X....P.| ; sei , lda #$6e, store $f1, lda #$02, store $f2 - so address $026e
00000010  02 50 a9 00 8d 01 50 8d  00 50 85 04 20 45 02 a9  |.P....P..P.. E..|
00000020  77 8d 04 50 a9 07 8d 05  50 20 2f 02 4c 29 02 38  |w..P....P /.L).8|
00000030  a5 00 e5 04 c9 19 90 0c  a9 40 4d 00 50 8d 00 50  |.........@M.P..P|
00000040  a5 00 85 04 60 a9 00 85  00 85 01 85 02 85 03 a9  |....`...........|
00000050  c0 8d 0b 50 a9 a0 8d 0e  50 a9 0e 8d 08 50 a9 27  |...P....P....P.'|
00000060  8d 09 50 a9 00 8d 04 50  8d 05 50 58 60 40 48 a9  |..P....P..PX`@H.| ; at $6e opcode 48 (pha) 
00000070  0e 8d 08 50 a9 27 8d 09  50 e6 00 d0 0a e6 01 d0  |...P.'..P.......|
00000080  06 e6 02 d0 02 e6 03 68  4c f3 00                 |.......hL..|

ROM parts

MYIRQ = $F0                                     ; Own IRQ vector <=================== my additions/alterations

; Below definitions for VIA 1 my loadable program uses VIA 2
PORTB = $6000                                   ; VIA port B 
PORTA = $6001                                   ; VIA port A
DDRB = $6002                                    ; Data Direction Register B
DDRA = $6003                                    ; Data Direction Register A

-------------8<------- snip

PROGRAM_LOCATION = $0200                        ; memory location for user programs

    .org $8000

main:                                           ; boot routine, first thing loaded
    ldx #$ff                                    ; initialize the stackpointer with 0xff
    txs

; ISR redirect code  <=================== my additions/alterations
    sei
    lda #$4C
    sta MYIRQ
    sta MYIRQ + 3
    lda #<ISR
    sta MYIRQ + 1
    sta MYIRQ + 4
    lda #>ISR
    sta MYIRQ + 2
    sta MYIRQ + 5
    cli

; End ISR redirect <=================== till here

; below this the standard rom routines

-------------8<------- snip
    .org $FFC9                                  ; as close as possible to the ROM's end

ISR:                                                  <====================== Whole ISR not my code
CURRENT_RAM_ADDRESS = Z0                        ; a RAM address handle for indirect writing
    pha
    tya
    pha
    lda ISR_FIRST_RUN                           ; check whether we are called for the first time
    bne .write_data                             ; if not, just continue writing
    lda #1                                      ; otherwise set the first time marker
    sta ISR_FIRST_RUN                           ; and return from the interrupt
    jmp .doneisr
.write_data:
    lda #$01                                    ; progressing state of loading operation
    sta LOADING_STATE                           ; so program_ram routine knows, data's still flowing
    lda PORTB                                   ; load serial data byte
    ldy #0
    sta (CURRENT_RAM_ADDRESS),Y                 ; store byte at current RAM location
                                                ; increase the 16bit RAM location
    inc CURRENT_RAM_ADDRESS_L
    bne .doneisr
    inc CURRENT_RAM_ADDRESS_H
.doneisr
    pla                                         ; restore Y
    tay
    pla                                         ; restore A
    rti

    .org $fffc
    .word main                                  ; Main ROM program
    .word MYIRQ                                 ; Redirect to OWN irq vector <=================== my additions/alterations

RAM Program

; Second VIA stuff
PORTB = $5000
PORTA = $5001
DDRB = $5002
DDRA = $5003
;------------------8<-------------

; Vector pointer on zero page
MYIRQ = $F0 

ticks = $00 ; 4 Bytes
toggle_time = $04 ; 1 Byte

    .org $0200

start:
; IRQ REDIRECT
    sei               ; irq masked
    lda #<irq         ; get low byte IRQ routine address 
    sta MYIRQ + 1     ; store at $F1

    lda #>irq         ; get high part of address
    sta MYIRQ + 2     ; store at $F2
    cli               ; irq enabled
; IRQ END REDIRECT

; init of program part
    lda #%11111111
    sta DDRA
    sta DDRB
        lda #$00
    sta PORTA
    sta PORTB
    sta toggle_time
    jsr init_timer
    lda #$77
    sta T1CL
    lda #$07
    sta T1CH

loop:                       ; loop 
    jsr update_led          ; update led routine
    jmp loop

update_led:
    sec
    lda ticks
    sbc toggle_time
    cmp #25
    bcc exit_update_led
    ; Toggle led
    lda #%01000000
    eor PORTB
    sta PORTB
    lda ticks
    sta toggle_time
exit_update_led:
    rts

;-----------------------------8<------------- snip

irq:
    pha
    lda #$0e
    sta T2CL
    lda #$27
    sta T2CH
    inc ticks
    bne end_irq
    inc ticks + 1
    bne end_irq
    inc ticks + 2
    bne end_irq
    inc ticks + 3
end_irq:
    pla
    jmp MYIRQ + 3           ; jmp to vector which points to ROM routine ; should be $FFC9

6502 update

  • New amplifier part using a LM386
  • Buzzer and led on VIA 2, blinky and sound timed by the internal timers of the 6522
  • ACIA testing still going on, writing software
  • Mini matrix keyboard removed, and used the temporary cursor buttons for the test with a rom which allows for a 8bits upload method using a arduino and the 6522. (I’m working on the big keyboard)

Work in progress code

PORT2B = $5000 		; VIA PORTB
PORT2A = $5001 		; VIA PORTA
DDR2B = $5002  		; Data direction register
DDR2A = $5003  		; Data direction register

PORTB = $6000 		; display
PORTA = $6001 		; control display + matrix keyboard
DDRB = $6002  		; data direction register
DDRA = $6003  		; data direction register
SID = $7000   		; sid base address

E  = %10000000		; enable bit
RW = %01000000		; RW bit 
RS = %00100000		; Register Select bit 
HOME = %00000010 	; VIA PORTB HOME command
DADDR = %00010000 	; VIA DADDRESS

LINENO = $0200		; temp address linenumber (move to other location)
NEXTLINE = 40		; 2x16 Chars but internally 40


  .org $8000

reset:
  ldx #$ff
  txs		; reset stack

; ###################################################
; #                 DISPLAY CONTROL                 #
; ###################################################
; VIA Setup

  lda #%11111111 	; Set all pins on port B to output
  sta DDRB
  lda #%11100000 	; Set top 3 pins on port A to output
  sta DDRA

; DISPLAY Setup
  lda #%00111000 	; Set 8-bit mode; 2-line display; 5x8 font
  jsr lcd_instruction
  lda #%00001110 	; Display on; cursor on; blink off
  jsr lcd_instruction
  lda #%00000110 	; Increment and shift cursor; don't shift display
  jsr lcd_instruction
  lda #$00000001 	; Clear display
  jsr lcd_instruction

; ###################################################
; #             PRINT MESSAGE LINE NO 0             #
; ###################################################
  lda #0  		; set line number
  sta LINENO      	; store for subroutine
  jsr gotoline		; move cursor

  ldx #0		; message index pointer
print:
  lda message0,x 	; start of message
  beq nextprint      	; stop when null in message (asciiz <- Zero padded)
  jsr print_char	; print char
  inx			; incr index
  jmp print		; resume print
; ###################################################
; #             PRINT MESSAGE LINE NO 1             #
; ###################################################
nextprint:
  lda #1  		; set line number
  sta LINENO      	; store
  jsr gotoline
  ldx #0  		; index pointer                 
print2:
  lda message1,x  	; absolute address message + x in A
  beq sidsound        	; if x is 0, end of message     
  jsr print_char  	; jump subroutine
  inx             	; increment x
  jmp print2      	; loop print2



; ###################################################
; #             SID SOUND                           #
; ###################################################
sidsound:
  lda #0		
  sta SID+$5		; attack/decay duration
  	
  lda #250
  sta SID+$6		; sustain level/release duration
  	
  lda #$95		; frequency voice 1 low byte
  sta SID+$0
  	
  lda #$44		; frequency voice 1 high byte
  sta SID+$1
  
  lda #%00100001	; sawtooth + gate
  sta SID+$4		; control register voice 1
  
  lda #$0f		; filter mode and volume (bits 3-0 main volume)
  sta SID+$18		; filter mode and volume



; ###################################################
; #             2ND VIA                             #
; ###################################################
  lda #%11111111 	; set port A output
  sta DDR2A

  lda #%11111111	; all ones!
  sta PORT2A
; ###################################################
  lda #%11111111 	; set port A output
  sta DDR2A

  lda #%11111111	; all ones!
  sta PORT2A



; ###################################################
; #             MAIN PROGRAM LOOP                   #
; ###################################################
loop:
  jmp loop
;                   1234567812345678
message0: .asciiz  "VIA 1,2 SID TEST"
message1: .asciiz  "   FASH  2022   "

; ###################################################
; #             ONLY SUBROUTINES                    #
; ###################################################

; ###################################################
; #             Subroutine gotoline                 #
; # Moves character placement position on display   #
; # Needs : $LINENO ADDRESS                         #
; # Exit values : -                                 #
; # Destroys registers: -                           #
; ###################################################

gotoline:
  pha                             ; store a
  txa
  pha                             ; store x
  ldx LINENO
  lda #HOME                       ; cursor down
  jsr lcd_instruction
  lda #$80
nextline:
  ldx LINENO
  cpx #00
  beq endnextlines
loopline:
  adc #40
  jsr lcd_instruction
  dex
  stx LINENO
  jmp nextline
endnextlines:
  pla                             ; pop a
  tax                             ; a to x
  pla                             ; pop a
  rts




; ###################################################
; #             LCD SUBROUTINES                     #
; ###################################################
lcd_wait:
  pha
  lda #%00000000  ; Port B is input
  sta DDRB
lcdbusy:
  lda #RW
  sta PORTA
  lda #(RW | E)
  sta PORTA
  lda PORTB
  and #%10000000
  bne lcdbusy
  lda #RW
  sta PORTA
  lda #%11111111  ; Port B is output
  sta DDRB
  pla
  rts
lcd_instruction:
  jsr lcd_wait
  sta PORTB
  lda #0         ; Clear RS/RW/E bits
  sta PORTA
  lda #E         ; Set E bit to send instruction
  sta PORTA
  lda #0         ; Clear RS/RW/E bits
  sta PORTA
  rts
print_char:
  jsr lcd_wait
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA
  rts

nmi:
  rti

irq:
  rti

  .org $fffa
  .word nmi
  .word reset
  .word irq
;  .word $0000