Archive for the ‘Uncategorized’ Category

Linear LED Fading

Sunday, September 5th, 2010
Linear LED Fading

Using PWM to fade a LED in and out is dead-simple. Except that the brightness does not seem to change linearly: It gets bright very fast, and then it nearly does not change anymore.

The fix: linearize it.

Since floating point operations are not that popular on a low power microcontroller as the Arduino, the easiest way to do this is via a lookup table to map 8 bit values for bridgeness into PWM values to drive the LED.

Perl to the rescue!
perl -e 'for(my $i=0;$i<256;$i++){ print int(exp($i/46)),",";}'

Manually change the first one to 0 (0 intensity is supposed to be off).
And now LED fading is easy. The example below shows also how to use TimedAction Library which I personally found very useful if you want to do 2 things independently on the Arduino (in this case: blink one LED and fade another one)

#include 

/* pwm9

Fade pin on PIN9 up and down and blink LED13

The circuit:
* LED attached from digital pin 9 to Vcc.

*/

#define ledPWMPin 9    // LED connected to digital pin 9
#define ledBlinkPin 13 // LED PWM

static unsigned char exp_map[256]={
  0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,
  3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,5,5,5,
  5,5,5,5,5,6,6,6,6,6,6,6,7,7,7,7,7,7,8,8,8,8,8,8,9,9,
  9,9,10,10,10,10,10,11,11,11,11,12,12,12,13,13,13,13,
  14,14,14,15,15,15,16,16,16,17,17,18,18,18,19,19,20,
  20,20,21,21,22,22,23,23,24,24,25,26,26,27,27,28,29,
  29,30,31,31,32,33,33,34,35,36,36,37,38,39,40,41,42,
  42,43,44,45,46,47,48,50,51,52,53,54,55,57,58,59,60,
  62,63,64,66,67,69,70,72,74,75,77,79,80,82,84,86,88,
  90,91,94,96,98,100,102,104,107,109,111,114,116,119,
  122,124,127,130,133,136,139,142,145,148,151,155,158,
  161,165,169,172,176,180,184,188,192,196,201,205,210,
  214,219,224,229,234,239,244,250,255
};

TimedAction timedBlink=TimedAction(1000,blink);
TimedAction timedPWM=TimedAction(100,fade);

void setup()  {
  pinMode(ledBlinkPin,OUTPUT);
  pinMode(ledPWMPin,OUTPUT);
}
// loop() is always called again when it exists
void loop()  {
  timedBlink.check();
  timedPWM.check();
}
// Blinking on PIN13
void blink() {
  static int ledState=LOW;

  ledState ? ledState=LOW : ledState=HIGH;
  digitalWrite(ledBlinkPin,ledState);
}

// Fading on PIN9
void fade() {
  static int currentPWM=0;
  static int dir=1;

  // 0 is supposed to be dark
  // Since LED connects to 5V, 0 is bright, so invert
  analogWrite(ledPWMPin,255-exp_map[currentPWM]);
  currentPWM+=dir;
  if (currentPWM > 255 || currentPWM < 0) {
  currentPWM-=2*dir;
  dir = -dir;
  }
}

4 Pin PWM Fan

Wednesday, August 25th, 2010
4 Pin PWM Fan

I was looking for something fan-like to move some air around, specifically from under my desk at work as it gets too warm there. It’s nice in winter, but unwanted in summer.

I was looking into this which needed a 50 Euro power control module and of course a propeller. And it would create about 100W output. Cool stuff, but overkill. Of course a simple small fan for 1000 Yen would do the trick, but where is the fun in that.

In the end I found this most simple method: a normal 4 pin PWM controlled fan as computers use them. Needs 12V DC, returns 2 count/revolution and can be controlled via a simple 5V signal. Can be easily controlled by an Arduino, and it can be programmed as I like (faster/slower, depending on time, temperature). Sounds like fun and useful at the same time!

Now let’s see if I can assemble it before it gets winter…

USL5P – It still lives

Friday, August 6th, 2010
USL5P - It still lives

I bought one of those some time ago (about 2005). It’s basically a small computer with a funny CPU (SH4, 266 MHz), RAM (64 MB) network (Fast Ethernet) and 5 USB 2.0 ports, 4 buttons, and some LEDs. Inside is a CF card for the OS, and originally you are supposed to connect USB storage devices which are then exported via Samba. Since it was basically a LANTANK with the internal IDE disk(s) replaced with a CF card, it did not take long until it was hacked. All you needed to do was to take out the CF card, modify some files (e.g. set a root password and enable telnet), and put it back. Suddenly you had a cut down accessible Debian. At first I used Gentoo (specifically from here) but when Gentoo broke on my desktop (trying to replace the shared glibc with another, newer and incompatible one on a running system is bad), I jumped to Ubuntu (Kubuntu) on my desktop and and plain Debian (or Ubuntu Server) on my servers.

So some month ago I updated it to the latest Debian release (unstable/unreleased) as the previous installation was from 2007 and updates were no longer available.

Here some pointers where to find useful information about this device and how to configure it to be on Debian sid/squeeze:

/etc/apt/sources.list should contain:

deb http://ftp.debian-ports.org/debian/ unstable main
deb http://ftp.debian-ports.org/debian/ unreleased main
And if anyone wonders why I would use such a slow machine: It runs on 5V, using max 2.2A (and that’s mostly for the USB ports which can draw 0.5A each). It also has no moving parts, it’s really small, and it just works (mostly thanks to Debian’s SH4 port). And it can do anything Linux can do when it comes to networking and USB.
To put some numbers to the “slow”: compiling a recent Linux kernel takes 8h.
A more modern similar machine would be this.

AVCHD and YouTube

Sunday, July 11th, 2010
AVCHD and YouTube

More a memo for me than anything else:

To convert an MTS file which is what I get from my video camera (Canon HF S10) to something else which is playable on a normal machine or which can be send to someone or uploaded to YouTube, ffpmeg is your friend:

ffmpeg -i MOVIE.MTS -vcodec libxvid -vb 800k -aspect 16:9 -acodec libmp3lame -ab 128k -s 720x540 MOVIE.AVI

This AVI can then be uploaded to YouTube. I could not seem to make sound able to work with AC3 when uploading. All I got was deadly silence. However mplayer had no issues playing a movie encoded via
ffmpeg -i MOVIE.MTS -vcodec mpeg4 -vb 800k -aspect 16:9 -acodec copy -s 720x540 MOVIE.AVI

I will have to see what the PS3 via ps3mediaserver can play.

Shiny New Notebook

Sunday, April 25th, 2010
Shiny New Notebook

Main old main PC is getting oldish. Time for an update! So I got myself a Dell Vostro 3700. “Business Notebook” it’s called, but I have no idea why.

My requirements were simple:

  1. Non-glaring display
  2. Lots of pixel (specifically more than 768 lines)
  3. Non-japanese keyboard (I hate the small space bar)

Other contestants were Lenovo (flunked the display category as the otherwise attractive SL series would be more than sufficient) and HP (no English keyboard). Smaller companies (like Mouse Computer) were not really competitive.

So far what I know about this notebook:

  • It has a 17″ screen, but the notebook itself is not as big as I feared.
  • The keyboard is not too bad. Not as good as the usual Thinkpad ones, but better than many other notebooks I tested.
  • 4 years on-site support included. No worries about potentially shoddy Dell quality.
  • Pre-installed is Japanese Windows 7. Not possibly to fix that to use English instead. But re-installing works.
  • Windows 7 has the same odd behaviour when the network breaks unexpectedly: it hangs and makes the computer unresponsive for seconds, then responsive for some milliseconds, and repeating this pattern. Note to self: do not meddle with the network settings too much.
  • Installing and updating drivers is unchanged since good old Win2k times. Only later I found out that Windows update would have found most updated automatically. So things do have improved in the last 10 years.
  • My Windows knowledge is still enough to get a Windows machine working well.

And here some noteworthy Linux items:

  • Kubuntu 9.10 (via PXE) is easier to set up than Windows 7 from CD
  • Dell uses 3 primary partitions. That leaves one primary left or only logical partitions. Luckily the BIOS can boot from logical partitions, so putting /boot in a small logical partition (and the rest under LVM control) is working fine.
  • Nearly everything worked out of the box. I only had to install the nVidia driver and the Broadcom driver as both are closed-source.
  • The only things not working is the screen brightness. While the Fn-keys work, the brightness does not change. It’s awfully bright. The current workaround is to change the brightness using the nVidia setting tool. Otherwise I’ll turn blind.

This is a serious upgrade in about every shape: CPU, memory, display pixels and I am quite happy with it. I wish I could turn off the fan though. While it’s not as noisy as my previous desktop machine, it’s much closer to me now and any fan is annoying.

Update:  Kubuntu 10.04 fixes the screen brightness problem mentioned above. Now the only outstanding item is how to disable the touch pad.

Update 2: The touch pad can be disabled (and generally) adjusted using the KDE Control Panel.

Fixing Emobile S21HT

Sunday, February 14th, 2010
Fixing Emobile S21HT

I have a H11T from Emobile. Simple phone. Works well for connecting computers via Bluetooth or USB to the Internet. The phone itself is medicore. Web browsing is a pain as the phone constantly runs out of memory and reflowing web pages is a big challenge for the phone. Unfortunately there is no Android phone available from Emobile. Otherwise I would have bought one.

I got my fingers on an Emobile S21HT (AKA HTC Touch Diamond). Not the most modern phone, but not bad either. And it’s surprisingly small with a nice display. But it has one severe flaw: It runs Windows Mobile 6.1. For phones it is the most horrible OS possible in my opinion. Sluggish. Non-intuitive usage. It is Japanese only, which does not help here.

Luckily there is fix available for those shortcomings: Android On HTC which allows you to install Android (currently 2.01) on several HTC Windows phones. Like the HTC Touch Diamond.

Initially it failed for me. haret displayed an error message which did not make much sense. What fixed it was a full reset of the phone including formatting the internal flash.

For those who want to fix their S21HT too: here is the link for the download. Phone works. Internet works. WiFi works. And using it is far more intuitive now.

Small problem: the buttons at the bottom are swapped. The very bottom one and the top one are swapped. The neat thing is, that when you start to touch one, the other one starts to glow. Neat.

Not everything works though: This table shows what works and what does not. Mostly the cameras and GPS does not work. Luckily my real phone has both, so I don’t care.

So far I am happy. It is a bit slower than I expected (but snappier than on Windows Mobile), but it is great to do my first steps on Android and I can figure out whether this is something I want or not.

Update: 3G data transmission has to be set up. I found the settings here. In short: APN=emb.ne.jp,  Username=Password=em, MCC=440, MNC=00

I have to admit I have no clue what those things means, but once set up, browsing the web via 3G works like a charm.

KVM and Kubuntu

Monday, February 8th, 2010
KVM and Kubuntu

I used to run VMware products since they exist (well, shortly later). Back when there was VMware Workstation 1.0 in 1999. We use it at work in shape of ESX 3.5. Great stuff. Solid. Useful at home too with VMware Server (free) or for Mac, use Fusion (cheap).

That is, all was well until I updated to Kubuntu 9.10. Then things broke. Badly. VMware server kernel modules could not compile (see here and here and here). Despite the helpful fixes (not from VMware if I may comment), it was simply unstable. The web console would suddenly stop working (the VMs were fine), and maybe the console could be restarted, or maybe not. And if not, then there was nothing in the world I could do to fix this beside rebooting. Very annoying.

I waited some months(!) for a fix from VMware, but I guess when it comes to support, the free VMware Server cannot compete with the (quite expensive) ESX.

Fair enough. The kernel of (K)Ubuntu 9.10 is not supported in VMware Server 2.02 after all. Time to move on as I did not want to move back to an older kernel. I rather move to a more recent kernel.

Enter KVM. I used qemu some time ago for fun. Mostly for emulating non-x86 architectures (e.g. ARM) to run their binaries for no other reason than because I could. It’s fun to use an arm cross compiler (my NAS runs on a Marvell 88F5182 AKA Orion), and run the very same binary on an AMD Athlon Linux box via qemu. KVM, the native Linux virtualization has a lot in common with qemu. E.g. it uses the same network infrastructure, same file format, same BIOS from Bochs. KVM expects a CPU to offer hardware assisted virtualization. I had that, so I tried to make KVM work.

And what shall I say: It’s working as expected! Even has a graphical interface to setup VMs (but all can be scripted too) via libvirt. Great for the first initial creation of a VM. The graphical interface is not even close to the polished interface the Virtual Infrastructure client software ESX offers, but good enough. And it works. USB pass-through works too after a bit of Google (AppArmor, which I did not even know was enabled until then, was in my way but the fix was here). PCI pass-through would probably work, as well as live migration (AKA VMWare’s vmotion or CitrixXen live motion). I lack the equipment to do this though.

I have to admit I like virtualization in general. It’s great for consolidations, not only at work. At home it’s very useful to be able to have USB and PCI pass-through (see here for a Xen virtual server to capture video signals for MythTV). You can have one computer instead of one Linux, one Windows, or one computer booting either Linux or Windows.

With KVM I’m a happy user again. And I can even think about upgrading to the latest Linux kernel…

http://libvirt.org/

XBee – API Mode

Friday, January 22nd, 2010
XBee - API Mode

After playing a bit in the transparent (AKA AT) mode, it became apparent that it’s quite limited if you plan to do more than just using 2 XBee’s as a wireless serial connection. E.g. it’s not possible to receive the state of the digital and analog inputs from a remote XBee endpoint. The biggest drawback of the API mode is that it’s quite difficult to interactively use it. It’s made for programs to use, not for humans.

After looking for a usable API library, I found this blog which not only pointed me to the right direction, but also to a Python library which is easy enough to use. Easy enough for me who’s not used to Python.

Here a short program included in the library and meant as a sample, which I I slightly modified to make D4 of the endpoint blink, and it sends back the status of D1 and D2 once in 0xf000ms. That part does not work though. I always seem to get the same values back regardless of the I/O status.

from xbee_api import *

class myXbeeApi(XbeeApi):
def onData(self,pkg):
#get header for IO data
if pkg["code"]==0x92:
  print "Samples %s-> D:%s/%s" % (pkg["data"]["mac"],pkg["data"]["dmask"],pkg["data"]["dsamples"])

api=myXbeeApi("/dev/ttyUSB1",9600)

device={
 "CO3":[0x00,0x13,0xa2,0x00,0x40,0x52,0x8d,0x8a],
  "EP1":[0x00,0x13,0xa2,0x00,0x40,0x4a,0x61,0x84]
}

led=False

api.sendRemoteAT(device["EP1"],"D2",[0x02])
api.sendRemoteAT(device["EP1"],"D1",[0x03])
api.sendRemoteAT(device["EP1"],"IR",[0xf0,0x0])
api.sendRemoteAT(device["EP1"],"IC",[0x00])

while 1:

 #set led status
 led=not led
 if led:
  api.sendRemoteAT(device["EP1"],"D4",[0x04])
 else:
  api.sendRemoteAT(device["EP1"],"D4",[0x05])
 time.sleep(0.5)

Several things I learned while using those XBee radios:

  1. When flashing new firmware on the XBee using the X-CTU program, keep 9600 baud. It makes your life easier as it will always default back to 9600 which is when you will lose your connection to it again and again. When everything is set and working, change baud rate to what you like.
  2. Digi’s documentation is not bad, but more examples would make life easier. Luckily those XBees are popular enough that there are lots of examples in the wild.
  3. I still have no idea what the firmwares for digital and analog endpoints do. The endpoint with API interface seems to be able to handle everything.
  4. Watch out for sleep mode. For endpoints, you cannot turn off sleep mode. Instead use Pin 9 and set it to GND to disable sleep (and SM=1).
  5. Connect the RSSI and Associate pin to LEDs. Helps a lot while debugging.
  6. Have the Commission pin connected. It turns the radio on for 30s so sleep mode is not an issue for 30 seconds.

XBee – Working

Sunday, January 17th, 2010
XBee - Working

I’ve got those XBee Series 2 modules for quite some time, and I finally managed to make two talk to each other. That was more complicated than I expected, but in the end it was not difficult at all, if you know what to change and how to configure them.

I use the XBee Series 2.5 (AKA ZB) models. There is a lot of documentation about the older Series 1 models. They are easier to use and need no configuration to make 2 talk to each other. The ZB models are a bit more complicated.

  1. Program one via X-CTU as a ZIGBEE COORDINATOR AT (that’ll allow transparent use)
  2. Configure a PAN ID (actually this is optional), configure a Node Identifier (“CO3″ in my case), and the Destination High and Low address is the serial number of the end device XBee. Unless you do this, no characters will go around.
  3. Program the other XBee as ZIGBEE END DEVICE AT”
  4. Configure PAN ID to the same one as before, configure a Node Identifier (“EP1″). Leave Destination High and Low to 0, which is the Coordinator.

That’s it.

What you will see then is:

  1. On the Funnel I/O board there are 3 LEDs. CHG for Charge (when USB is connected and the LiPo battery is charged), ON which blinks about twice per second, and RSSI which is green when you send characters via the Coordinator via the X-CTU Terminal.
  2. On the X-CTU Terminal connected to the Coordinator, when you enter command mode (+++), and ATDB, you’ll get a number back which is the direct dB value in hex. The more, the weaker the signal. 0 means that no character ever was received.

Here the link which gave me the “Ah! Got it!” effect. It’s one of the few ones which write about the 2.5 firmware (which is similar to the ZB one I use).

LED Lamps

Friday, January 8th, 2010
LED Lamps

Sharp built a LED based lamp with a remote control named DL-L60AV in Japan. As the name implies, it’s supposed to be comparable bright to a 60W incandescent light bulb. That alone is quite impressive for a LED lamp.

I bought one of those for my wife. Since  you cannot only control the brightness but you can also control the color temperature it did look really nice to have.

It did not take long that it occurred to me that the IR remote control ability means that by using an computer connected IR sender, any computer can control a 100V lamp. No need to twiddle with 100V electronics, no need to use any power electronics like triacs. Being hit by a charged elko once (that was back in Germany, so it was probably 325V)  I have a lot of respect for 100V.

Kuroutoshikou has such an IR controller/sender named KURO-RS. I am tempted to buy one, but I unlikely will have time to play with this.