David Pottinger Design a journal blog

Linux Shell Scripting 1

Some things about living on a Linux laptop

Since the screen post, I’ve renged and put putty on this machine. It has a Linux version and I’m familiar with its GUI. Plus it looks presentable, IMHO.

But for this post, shell scripts are useful! I’ve used them for a number of things, including this blog. I can click on the jekyll serve script to preview changes that I make in blog posts. I had one in the last post about screen. It takes a little bit to load, but faster than typing commands myself in a terminal.

#!/bin/bash
echo "Testing the site locally, this may take a moment..."
# add chruby script
source /usr/local/share/chruby/chruby.sh
chruby 2
# overwrite the url so we can look at localhost:4000
jekyll serve --baseurl ''
# keep the shell open
$SHELL

Although now that I look at this, I’m surprised the “source” part doesn’t actually change the directory.

Jekyll prefers to use a newer version of Ruby than the Linux Mint distributions currently offer, hence chruby lets me use a self-installed version. That was a bit annoying to do but it works OK now. Probably I should go for Ubuntu packages instead of this workaround but I don’t use Ruby for all that much yet. This script handles the chruby part (which is itself a script, from what I read), and then serves the “test” site locally. I like leaving the shell open afterward to maybe push the site changes to github.

Sometime I should write out the github push and clone commands.

Shell scripts also work great for adding programs to the Cinnamon main menu. Right click on it to open its adjustment GUI and go from there. There’s another GUI for the menu programs themselves, which work with shell scripts if they’re working, more on that later. It handles the “desktop” files needed for things to show up there. You can add icons in the start menu, too. Can get some free icons intended for games from this site for that purpose.

I don’t have the script above in the main menu, but if I did, I’d probably want to add something to change directories to the blog folder.

Bug for the day

When using gedit or maybe even nano, you can reach a state where a Windows newline character is on your lines instead of a Linux character. Bash of course hates this. I had “/bin/bash^M unrecognized” errors and fixed them by saving a gedit *.sh file as a Unix/Linux line ending instead of a Windows one. Wouldn’t have thought of that, google saves me all the time.

I actually have seen this the other way too, with screen datalogging functions producing output that isn’t very readable on Windows machines at work. In that case I think it was carriage return and line feed or something. Maybe worth investigating later.

GNU Screen for USB Serial

I frequently connect microcontrollers to computers. There may be a smarter way to do this, but for now, the easiest way seems to be some kind of serial interface with some conversion to USB. The Bus Pirate does this too, or you can get modules or chips to take in UART serial and connect to your USB port.

For the software on the computer side, if it’s a Windows machine I tend to use putty. On this new Linux computer I’ve had success with Screen. It’s one of those GNU free software projects. Here is a link to their quick reference.

Typical Use

It works through my usual shell, some GNOME Terminal on Cinnamon.

I can type “screen /dev/ttyUSB0 115200” (for the Bus Pirate) and it changes the terminal to their input. I’ve read in a couple places that it could open in a separate window. For now this works. The control mechanism is either “ctrl + a” followed by characters or an escape key. A couple important commands are detach “d” and help “?”.

I also have a .screenrc file set up mostly to help with automatic data logging operations.

deflog on
shelltitle UART Log
logfile $HOME/screenlogs/uart.txt
logstamp off

Quirks

The output is raw, so it’s possible that some text editors will complain about unreadable characters. Newlines are a little annoying because the terminal needs carriage return and line feed, but the log may not process these correctly. I need to figure it out a little better for the logs.

Changed My Contact

Some kind of maintanence. Thought I shouldn’t just have my email address on “about”. Hence, a contact form. Added a new “contact” page for this. It was formatted in html, so I didn’t get to use a simple “page” set up in Lanyon. Oh well.

I’ll try this Cognito forms service first I suppose. There seem to be at least a dozen competing copies of approximately the same thing. Not about to spend much on any of them.

I guess next I should think about some kind of post index? Maybe I should be adding tags all along for people to sort these posts.

But if you have suggestions for how I should do things, now you can go to this contact page and tell me!

HTML on microcontrollers

The other project is delayed for maybe a few weeks, so I’m changing gears to work on a new one. I tend to have a few things on my mind at once. It’s easy to change gears when reaching a stalling point.

This has led to problems with nothing getting done in the past. I think I’ll be fine as long as there aren’t too many things going on at once.

Why HTML?

The idea is to use a USB port to connect a personal computer to a microcontroller and provide a commonly available interface for whatever program is running on that device.

The rationale is that browsers are on most PCs. If I want to plug something in to anyplace, I can expect to find some support for that. I’m thinking of local HTML that exists in some memory on the device though, not web pages.

Hope it works!

How could I do this?

I’m not sure yet, I haven’t tried it yet. I don’t know much about HTML or user interface. I’m learning as I go. Here are some notes:

HTML has a special tag for user input called a “form”. This is the kind of thing you see when entering an email address or shipping address. I bet there’s more UI things that can be done, but this is a good start for me. The Wikipedia page on them has been informative.

Some other web people have put together a tutorial for these here.

The device side should be more interesting. A program running on a microcontroller will provide the host PC with the files it will use to view an HTML page. I think some trickiness will be required to communicate the other way, to tell which buttons are pressed.

I could fall back on some USB trickiness by directing the PC to ask for a file which may or may not exist. Possibly I could execute some code from within HTML to communicate more directly? There are plenty of tutorials for doing this with a custom program (such as this one. Drivers may become an issue too if I go that route.

Would also be helpful to ask some friends, maybe the internet.

PIC32 Timer Doc

I usually work with the smaller 8 bit PICs. These might stick around in some way but it looks like the bigger processors are getting cheap enough to use more often, like the 5$ Raspberry Pi. Might as well dig into them. I’ll start with some 32 bit PIC projects maybe.

First I need a timer to handle a button input and to turn on some voltage supplies. That’s a good start for this one.

Complaining

The PIC32 Harmony stuff has some timer settings with lots of options, but I don’t see how to do all of it here. The new Configurator tool for 8 bit PICs allowed me to set it to 1mS more easily. I can still calculate this based on the hardware datasheet of course. It looks like I have to go that route.

There’s a big picture of the harmony tools on the Microchip Wiki. Don’t see the super easy way to set it up I was looking for. Documentation inside the code is detailed when looking at an individual function but the big picture level is lacking.

I like Harmony for some things, particularly keeping track of what pins the peripherals go on, but these timers aren’t there yet.

The Manual Solution

The internal FRC is 8MHz and I can use PLLs and dividers to change that. There’s another divider for all peripheral clocks. The timer hardware comes with nice period registers to avoid resetting the timer register every expiry. The math works like this:

interrupt flag period (seconds?) = 
    PRx register * divider / peripheral frequency (Hz?)

To use a polled interrupt, I had to enable the interrupt source for the timer, too. The flag for it was in the IFS0 register. Interrupts were set up nicely through Harmony though.