Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Sunday, March 23, 2025

Raspberry Pi running BASIC

Earlier on this blog, I posted something about the BASIC language on Linux. 

It turns out that all the tools mentioned in this posting are available on Raspberry Pi computers too. In fact, BWBASIC, FreeBasic and Geany work in the exact same way.


Monday, February 22, 2021

Raspberry Pi and Arduino revisted

In my earlier post, locating a valid Arduino connection, I used exception handling by catching the error message created by attempting to open a non-existing device.
Of course there is an easier way of doing this in a POSIX compliant system, by looking at the existence of the respective device files. If you are confused about the ttyUSB0 device, this is how an inexpensive Arduino clone appears in my system.

Here is my new approach, which is bit more elegant and less brutish. This approach uses the "glob" module and the "set" data-type.


import glob

def listFiles(prefix):
    _=set()
    for file in glob.glob(prefix):
        _.add(file)
    return _

def findArduino():
    ser_dev={'/dev/ttyACM0','/dev/ttyACM1','/dev/ttyUSB0'}
    dev_list=listFiles('/dev/tty*')
    for _ in ser_dev:
        if _ in dev_list:
            return _

if __name__=='__main__':
    arduino=findArduino()
    if arduino:
        print('Arduino found at',arduino)
    else:
        print('Arduino not found')


Python Script Re-Import

Here is another study, pretty useless what it does actually, however, this might come in handy for some self-modifying projects.

This script writes a script called test01.py. It then imports the test01.py module, overwrites it, including data to a list and finally re-imports the modified test01.py module. At the very end, a function of the re-imported module is called. As a bonus the test01.py module is a fully self sufficient python program itself.

The study demonstrates how a python program can not only modify data but also modify its own functionality during run-time. Having a dictionary with python instruction could be an interesting way to write self-learning scripts.


from importlib import reload
import sys

a=[]

def modify(a,i):
    a.append(i)
    return a


# create a module to import
filename='test01.py'
f=open(filename,'w')
f.write(f'b=[]\n')
f.write(f'def test01(b):\n')
f.write(f'    return b\n')
f.write(f'if __name__=="__main__":\n')
f.write(f'    test01(b)\n')
f.write(f'    print(b)\n')
f.close()

# import the module
from test01 import *

for i in range(3):

    # play with data
    a=modify(a,i)

    # write modified module
    filename='test01.py'
    f=open(filename,'w')
    f.write(f'b='+str(a)+'\n')
    f.write(f'def test01(b):\n')
    f.write(f'    b.pop(1)\n')
    f.write(f'    return b\n')
    f.write(f'if __name__=="__main__":\n')
    f.write(f'    test01(b)\n')
    f.write(f'    print(b)\n')
    f.close()

    # re-import module
    reload(sys.modules['test01'])
    from test01 import b, test01    

    # show data from module
    print(b)

print('===')
print(a)

# run function from module
print(test01(a))


Thursday, February 18, 2021

Python Moving Average

In data analysis, moving averages are often used to "smooth" noisy data. In terms of signal processing, applying a moving average is a very crude low-pass filter. In fact, a moving average filter is a special case of an FIR (finite impulse response) filter.

Here is a study I wrote in Python using numpy and matplotlib.

import numpy as np
from matplotlib import pyplot as plt

# calculate moving average and add zero-padding
# the function takes two parameter, the data array and the
# number of bins for averaging, default 5 can be overwritten
def moving_average(a, n=5):

    ret = np.cumsum(a, dtype=float)
    ret[n:] = ret[n:] - ret[:-n]
    return (np.insert((ret[n-1:]/n),0,[0]*(n-1)))

# create some sort of noisy data
data = np.arange(100)
data = np.sin(data/5)+np.cos(data/20)+np.random.randn(100)/5

data_avg = moving_average(data,7)

print(data)
print(data_avg)

plt.plot(data,'.')
plt.plot(data_avg,'.')
plt.show()


Tuesday, February 16, 2021

Python Circular Buffer

Just to continue the series about watching me learning Python, here is one that might be useful for any  application that samples data in real time into a circular buffer, aka ring buffer. This examples runs two threads, one thread is "reading" (creating) data and depositing this data into the ring buffer, while the other thread reads a certain amount of said data independently.
Obviously, the ring buffer is in shared memory (global) and accessible by both threads.
Of course, putting random numbers into a buffer in a particular frequency and reading said buffer out with a different frequency is not solving any real problems, however, this is supposed to demonstrate how two threads could be used for sampling and analyzing data.

from time import sleep
from threading import Thread
import random

def read_val(var):
    length=len(var)
    global pos
    while True:
        for i in range(length):
            var[i]=random.random()
            pos=i
            print("generated ",i, var[i])
            sleep(0.5)

def pick_val(var):
    length=len(var)
    while True:
        sleep(0.1)
        print("most recent ",pos,var[pos])
        for i in range(length):
            lpos=(pos+i)%length
            print("=> ",i,var[i],lpos,var[lpos])

# initialize memory
data = []
for i in range(5):
    data.append(0)

t1 = Thread(target=read_val, args=(data,))
t2 = Thread(target=pick_val, args=(data,))

t1.start()
t2.start()

Sunday, May 17, 2020

Open Source FORTRAN for DOS

As a physicist of trade, I mainly worked with FORTRAN during my science days. One of the most important factors for me in selecting an OS is therefore the availability of a FORTRAN compiler.
With DOS, or FreeDOS, we got lucky. The Waterloo Compilers are now available as Open Source under the Open Watcom Project.
On the download page, you will find that a C/C++ compiler, an ASSEMBLER and a FORTRAN compiler are now available as open source.
The FreeDOS project included the C/C++ compiler in the distribution. I figure, the ASSEMBLE is included too. However, the FORTRAN compiler is not. I got in contact with Jim Hall asking him to include Watcom FORTRAN in the next distribution of FreeDOS.
In the meanwhile, I installed Watcom FORTRAN to the same tree as Watcom C/C++ in FreeDOS.

Here is how to do it:
Download open-watcom-f77-dos-1.9.exe as provided by the Open Watcom webpage.
Run the installer and select "\DEVEL\OW\" as install directory. This will install the FORTRAN compiler into the same environment as the C/C++ compiler provided in the FreeDOS distribution.

To use the compiler, first run the BATCH-scrip "\DEVEL\OW\OWSETENC.BAT", which will set the correct PATH and LIBRARY variables. This script is part of the FreeDOS installation.
In order to compile a simple FORTRAN program, call "wfl".

When I was taught FORTRAN in University, a system called WATFOR-77 was used in the classroom. This was an integrated system having an editor and a compiler. Same origin as Open Watcom, however, WATFOR-77 is not available as open source by today. If the authors are reading, it would be great if you would turn the DOS version of WATFOR-77 including the editor into open source!

Saturday, May 9, 2020

Hardware to run FreeDOS

In my earlier post, I stated that I intended to run FreeDOS directly on bare hardware, meaning, without virtualization.

Using the FreeDOS 1.3 Live CD, I tried a different systems at my disposal. Most modern ones failed, one way or the other.

An IBM 380Z laptop booted the CD fine, however, only the center of the screen was used. The system would work, however, the experience would be disappointing since the real estate of the screen is severely underused. The mouse-nipple, or however the red thing in the middle of the keyboard is called, worked find under FreeDOS.

My HP/Compaq nx6110 booted the live CD fine, screen fully used. The touchpad did what it was supposed to do, right away. This could be the perfect candidate for running FreeDOS on bare silicon. However, this laptop is my goto-PC for Debian/LINUX. So, for now. FreeDOS will live in qemu on this one.

Actually, the first hardware I tried FreeDOS on was one of my Acer ASPIRE ONE 110 netbooks. One that I had given an HDD years ago. Here is a link to a post on my RF related blog, concerning the HDD in the Aspire ONE netbook. To the time, I had not thought about an IT related blog, so, it was called "off-topic", seen the radio-frequency electronics content of my other blog.

Here is the netbook during the install of OpenWatcom FORTRAN on FreeDOS.
Acer ASPIRE ONE 110
The install files are on the USB thumb-drive on the right hand side of the netbook.
While the device is equipped with Ethernet, I had not set up or tested networking by the time of writing this post.
The touchpad on the Acer does not work with FreeDOS yet. I tried various options with the USB-mouse-driver, all of the attempts ended in crashes.

As a pure DOS computer, the netbook does a great job.
Several programming languages are available, through the FreeDOS-CD and also by installation using the USB thumb-drive and a USB floppy drive.
My old DOS version of Maple V runs just fine and so does a MathCAD student edition I happen to own.

Unfortunately, the Aspire ONE does is not fully supported in FreeDOS what APM is concerned. However, dictating processor speed does work fine.
I created the following aliases in fdauto.bat

alias slow=fdapm speed1
alias fast=fdapm speed8

Those commands set the processor speed to 1/8 and 8/8 (full) processor speed. In the case of the Aspire ONE, the native processor speed is 1.6GHz. Running the netbook at 1/8th of that, will still deliver a very decent 200MHz. This is plenty of fast for a DOS computer.

In fact, when editing code, I set my netbook to run at lowest speed. Only when I compile and run my programs, I will ramp the CPU up to max.

In conclusion, the Acer ASPIRE ONE 110 is the perfect DOS machine, I never was able to dream about.
In my nest post, I will compare the ASPIRE ONE to machines of that past, which I still own today.


Thursday, May 7, 2020

Taking every day computing a step back

Due to the present situation, a lot of my own professional use of computer is vastly reduced. Presently, there is not need and apparently no funds left for my clients to order polished illustrated reports nicely presented in PDF files.
So, that gives me the opportunity to reflect on how I did my personal computing in the eighties and nineties. At that time, computer mice were around, however, nobody really knew what to do with them, even CAD was done with console input. Many of the structures and devices designed that way are still standing and/or in operation.

Back in the day, I used an 8088 with two 5in floppy drives.
Later, I was able to buy a no-name 286/287 based PC with a 20MB hard-drive. OS of the time was MS-DOS, I believe it was version 4.01, not sure about that.
Remember those time? You switched the PC on and seconds later, you could start with your work.

Latest advances in the FreeDOS project got me back to those times. Jim Hall started the project in 1994 and still keeps improving this opensource MS-DOS compatible OS.
Some hardware support of modern hardware is present already.
While FreeDOS can run in a virtual machine, I am personally more interested in running a native DOS PC.

Why DOS, you might ask yourself. One reason I mentioned above already. But, there are additional reasons such as

  • easy administration and programming
  • tons of old freeware and shareware available
  • abandonware
  • some commercial software being turned into freeware, e.g. OpenWatcom and Borland compilers
  • one or the other software one has bought back in the day
In the next post, I will write about my first steps using mondern-ish hardware with FreeDOS.