screen for normal linux user not working, but…

While playing with a development server at work, we just came across an error message like this:

Cannot open your terminal '/dev/pts/0' - please check.

So, there seemed to be rights-related problem, and searching the web brings up quite a lot of forum entries about this topic, with maily this two solutions:

  1. open screen as root and then sudo to user
  2. make /dev/pts/0 writable for the user

I didn’t want to try the first one, because that might allow the user to fall back to root, so I tried the second one. At first it worked, but when I completely closed the user’s session, /dev/pts/0 seemed to be gone, and the next try failed again, so it’s not a permanent solution.

The solution (that worked for me):

But finally I came across another solution that worked like a charme: you just need to SSH onto the machine directly with the desired user, and then it’s no problem to open a screen.

In our previous tries we had logged in as root and then sudoed to the user, which caused this problem. Maybe I’ll investigate some alternatives or eventually more in-depth information about why this works this way, but for now I’m quite happy that we have a working solution.

nHangman – a tiny game engine (prototype)

Inspired by a question on StackOverflow (which was actually about using regex to replace strings used for a Hangman game) I hacked together a file in C# that contains a minimal implementation of the Hangman game.

The code is up on Github: https://github.com/jcoder/nHangman

It’s actually more of a protoype, but maybe it’s helpful to somebody.

Moving the extension methods project to github

Some time ago I started to write a class with some C# extension methods, adding more methods over time. I first uploaded the file to the webspace of my blog, but I think this is getting too complicated to mantain, which makes it harder than required to post new versions.

So I’ve put the code on github: https://github.com/jcoder/jCsharpExtensions

Feel free to stay updated there, fork it or just download it. If you have any comments or suggestions, please leave them in the comments below.

Reading and learning instead of blinking and beeping

Yes, I know, all those “I’ve been too busy to blog” postings are boring, so I’ll skip that part and just give you a short status update how things are going.

First of all, I haven’t been building and coding much lately, at least not for the .NET Microframework environment. Sure, I think this kind of technology is great, and I really like to have some peaces of it laying around, waiting for the next idea to become a prototype.

But the more I’ve been playing with the NETMF things, the more I learn about related technology: electronic components, sensors, chips and microcontrollers. Well, I’m exaggregating when I speak of “learning”, but I’m getting a better idea of how all those things are working internally.

On the one hand this makes it easier to understand what others are building and how their projects solved a specific problem, often in a very awesome way, on the other hand you have to get more into detail when working on own project ideas, because having an overview of available components doesn’t mean that the technological solution appears out of nothing.

To cut a long story short: electronics in general and (.NET powered) microcontrollers are just awesome and I’ll keep on playing around with it and occasionally sharing my results with you, promised.

Leaving the FEZ comfort zone – say hello to the breadboard

I’ve been very busy over the last weeks, but now I’ve taken some time to get deeper into the “core electronics part”, which simply means that I got a breadboard and some electronic components like resistors, transistors, LEDs and a lot of wires.

The first step was to get a normal LED on the breadboard blinking by connecting it directly to the Digital IO ports on the FEZ Panda board. Sounds easy but I did not want to break anything so after carefully reading some postings about the topic I connected die Digital IO port to a 220 Ohm resistor which was connected to the LED and then back to Ground. Using the OutputPort class on the given IO port the LED was blinking. That easy :)

As it was not as complicated as I thought it would be, I remembered the 2×24 LCD panel that I got from a friend. I wasn’t sure how it exactly works, but as I found out it has a HD44780 controller on-board that accepts 4-bit commands using additional wires for signals and power. And there is even a very useful C# class at the GHI Electronics website, nearly ready to use.

And guess what? After connecting the wires and including the pre-defined class I got the LCD working! Awesome!

I actually had to de-wire everything to put it back in the boxes for storage, but it’s not as hard as I thought to put everything together, so manybe I’ll use the LCD in some of the next projects.

And the next part of equipment is waiting: I just soldered the needed connectors to a 4×7 segment LED element, so stay tuned ;)

Netduino Scripted Lights

Playing around with a FEZ Panda II is a lot of fun, especially when it comes to LED lights (and when it’s getting dark outside). Sure, you can make them simply blink, or maybe progam a more complex light-switching pattern, but you always end up with a lot of code to address the LEDs and wait some time between changing their state.

To make it easier to work with an LED, I’ve written a small wrapper class some time ago as described in this posting, but I really wanted to come around having to write a lot of (non-reusable) code just to make some LEDs blink.

My current solution: a very simple, char-based scripting module to control the LEDs. This is how it works:

  • You define some simple script like "R2 r2" or "GH4 YZ4 a2"
  • Based on the ILights interface, you create an instance of LEDLights
  • An outer loop defines how often the sequence should be executed
  • The inner loop iterates over the sequence and passes each char to the mini engine
  • The lights start blinking, or whatever you told them to do

Actually, some of these steps could be encapsulated and the pin configuration for the LEDs is currently hard-coded, but for the beginning I wanted to keep the things as easy as possible.

See it in action

How does the script work?

In short: an uppercase letter switches the LED on, a lowercase letter switches it off, and a numerical letter pauses between two actions. I recently got some additional LEDs so I added support for 6 LEDs (please look up the pin configuration in the source code).

  • r / R : red LED no. 1
  • s / S : red LED no. 2
  • y / Y : yellow LED no. 1
  • z / Z : yellow LED no. 2
  • g / G : green LED no. 1
  • h / H : green LED no. 2
  • a / A : all LEDs at once
  • 1, 2, 3, …, 9 : pause for 500ms, 1000ms, 1500ms, …, 4500ms

All other characters are currently ignored, so you can add spaces to separate the parts of the script which makes it easier to read the script sequence.

Some examples

R2 r2 – the red LEDs is switched on, then waits for 1000ms, then  the LED is switched off and the script waits again for 1000ms

R1Y1G4 r1y1g4 – the red, yellow and green LED is switched with short delay, then switched off again

zY2 yZ2 – the two yellow LEDs blink alternately

Tipps, tricks and ideas

When defining a sequence make sure that you add pauses, because otherwise the LEDs fill flicker based on the Netduino’s maximum speed.
It’s easy to forget the pause at the end of the script, which makes the sequence look strange when it gets repeated.
Of course, you can switch off an LED at the beginning of the script, even if it was not switched off before. This is very useful if the sequence has a complex overall pattern and an LED must stay switched on at the end of the script.

Some ideas to extend this project:

  • Store more than one sequence in the program and add an IR-receiver or simply a button to switch the sequences
  • Read the sequences from an SD card
  • Connect the Netduino to the network and add either a webserver to the program to enter the sequence or fetch it from a website (or even from Twitter)

Source Code

For the LED class please look at this posting.

public interface ILights
{
    void LightCommand(char c);
}

public class LEDLights : ILights
{
    private int baseDelay = 500;

    private LED led_R = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di1);
    private LED led_Y = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di0);
    private LED led_G = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di8);
    private LED led_S = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di5);
    private LED led_Z = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di3);
    private LED led_H = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di2);

    public void LightCommand(char c)
    {
        switch (c)
        {
            case 'r': led_R.Off(); break;
            case 'R': led_R.On(); break;
            case 'y': led_Y.Off(); break;
            case 'Y': led_Y.On(); break;
            case 'g': led_G.Off(); break;
            case 'G': led_G.On(); break;
            case 's': led_S.Off(); break;
            case 'S': led_S.On(); break;
            case 'z': led_Z.Off(); break;
            case 'Z': led_Z.On(); break;
            case 'h': led_H.Off(); break;
            case 'H': led_H.On(); break;
            case 'a': led_R.Off(); led_Y.Off(); led_G.Off(); led_S.Off(); led_Z.Off(); led_H.Off(); break;
            case 'A': led_R.On(); led_Y.On(); led_G.On(); led_S.On(); led_Z.On(); led_H.On(); break;
            case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': Sleep(c); break;
        }
    }

    protected void Sleep(char c)
    {
        int num = c - '1' + 1;
        int delay = num * baseDelay;
        Thread.Sleep(delay);
    }
}

public class Program
{
    public static void Main()
    {
        string sequence = "R2 r2;
        // process sequence
        ILights lights = new LEDLights();
        // switch off all lights at the beginning
        lights.LightCommand('a');
        // outer loop
        bool looping = true;
        while (looping)
        {
            for (int i = 0; i < sequence.Length; i++)
            {
                char c = sequence[i];
                lights.LightCommand(c);
            }
        }
    }
}

C# Extension Methods, Part 2

I’ve just made another tiny addition to my extension method class / project, adding a small, but maybe useful method:

/// <summary>
/// Repeats this IEnumerable a given number of times
/// </summary>
/// <param name="enumerable">this IEnumerable</param>
/// <param name="times">number of times to repeat this IEnumerable</param>
/// <returns>IEnumerable</returns>
public static IEnumerable<T> Repeated<T>(this IEnumerable<T> enumerable, int times)
{
    if (times < 1)
    {
        yield break;
    }
    for (int i = 0; i < times; i++)
    {
        foreach (T oneObject in enumerable)
        {
            yield return oneObject;
        }
    }
}

This is how to use it to consume an IEnumerable<T> multiple times:

var range = Enumerable.Range(1, 5);
foreach (var i in range.Repeated(5))
{
    // doing something with i, which will be the sequence
    // from 1 to 5, repeated 5 times
}

It’s not a big deal, but maybe it’s helpful anyway. Btw, the source code is updated, too.