             analogWrite(PIN_PWM, val);
     }
 }
+
+// we need CMD0 CMD1 <cmd> <value> sequence from serial port
+// so implementing a simple state machine here
+byte read_cmd(byte *value)
+{
+    byte c;
+
+    while (1)
+    {
+        // seek for start sequence
+        while (read_byte() != CMD_START1)
+            ;
+
+        // next in sequence
+        if (read_byte() != CMD_START2)
+            continue;
+
+        // read cmd byte
+        c = read_byte();
+        // read value
+        *value = read_byte();
+        return c;
+    }
+}
+
+byte read_byte()
+{
+    // Brutal: stay in busyloop until we have something
+    while (1)
+    {
+        if (Serial.available())
+        {
+            byte c = Serial.read();
+            return c;
+        }
+    }
+}
 // EOF

#include <Arduino.h>

#define PIN_PWM 4

// we define a dead simple protocol: 0x00 0xFF followed with command byte and value.
// Right now we only define command byte 0x01 (:


void setup()
{
    Serial.begin(SERIAL_SPEED);
    Serial.println("Starting...");

    pinMode(PIN_PWM, OUTPUT);
    analogWrite(PIN_PWM, 0);

    Serial.println("Setup complete.");
}

void loop()
