Getting video out from your Adafruit Trinket or DigiSpark (or ANY ATTINY85)

11224347_913681192034970_90837413382715523_oWhile pouring through some videos and posts on just what the tiny little ATTiny85 chip can do, I ran across a HackADay post about someone generating NTSC video on VHF channel 3 (US channel 3.)  I was intrigued.  So, I tried it and got mixed results and, as I have only a couple of old analog sets, I thought it was kind of impractical to pursue, but cool anyway.  So, I wondered if just composite out would be better and, to my delight, the same person who posted the video also had done composite out with an ATTiny84.  It should work on the 85 as well, and, it does.

So, I found his code and downloaded it.  Mr. Lohr is a GENIUS, plain and simple.  Thank you, sir!

The code did not work well at first. I had to tweak it a bit to work with my hardware, especially the little monitor.  It still is not perfect, the video is not centered, but is fairly steady and bright…my first attempts were not very bright and not very steady. I am guessing it needs to be tweaked for the monitor it will be used on, but I have not tested on more than one.

All you need is something like a Trinket from Adafruit or a digispark. I used both, but destroyed the digispark when I attempted to use an external power source.  It does not like more than five volts.  Specs say 3.5 to 12volts, I used 9. My mistake. 12182942_913681798701576_4759339394040268204_o

The Trinket works great. I am using the 5volt version with USB.  Any ATTiny85 based controller should work, though.

Connecting it is simple: Pin 3 to video ground, Pin 4 to video center pin.  Download the code to your device, connect it to the monitor and reset it. You should see the video demo.

Now, there are some major caveats:

  • it, so far, only does text
  • you have 13 characters by 6 lines
  • character spacing is mono
  • there’s no video memory, each frame is drawn on the fly
  • the ‘video memory’ is simply a string array
  • there is no formatting
  • the character set is stored in flash

So, why bother with such limitations? Well, for starters, cheap composite monitors are easy to get WP_20151025_11_19_10_Rich_LIand use and the Trinket/digispark are both under ten bucks, so you can have an application with video out for very little money.  If nothing else, this is a great lesson in how video works. Charles Lohr did a fantastic job with the code.  He’s a genius. Did I mention that?

Any way, the code is posted below.  Let us know if you get it working and what you do with it.

 

 

/*
    Copyright (C) 2014 <>< Charles Lohr


    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "ntscfont.h"

 

void delay_ms(uint32_t time) {
  uint32_t i;
  for (i = 0; i < time; i++) {
    _delay_ms(1);
  }
}

#define NOOP asm volatile("nop" ::)

void NumToText( char * c, uint8_t a )
{
    c[0] = (a/100)+'0';
    c[1] = ((a/10)%10)+'0';
    c[2] = (a%10)+'0';
    c[3] = 0;
}
void NumToText4( char * c, uint16_t a )
{
    c[0] = (a/1000)+'0';
    c[1] = ((a/100)%10)+'0';
    c[2] = ((a/10)%10)+'0';
    c[3] = (a%10)+'0';
    c[4] = 0;
}


EMPTY_INTERRUPT(TIM0_OVF_vect );


int main( )
{
    cli();

    CLKPR = 0x80;    /*Setup CLKPCE to be receptive*/
    CLKPR = 0x00;    /*No scalar*/

    PLLCSR = _BV(PLLE) | _BV( PCKE );
//    PLLCSR |= _BV(LSM);

    DDRB = _BV(1);

    DDRB |= _BV(3);
    DDRB |= _BV(4);
    PORTB |= _BV(1);

    TCCR1 = _BV(CS10);// | _BV(CTC1); //Clear on trigger.
    GTCCR |= _BV(PWM1B) |  _BV(COM1B0);// | _BV(COM1B1);
    OCR1B = 2;
    OCR1C = 3;
    DTPS1 = 0;
    DT1B = _BV(0) | _BV(4);

    TCCR0A = 0;
    TCCR0B = _BV(CS01);
    TIMSK |= _BV(TOIE0);

OSCCAL = 215;

//    OSCCAL=186;


#define POWERSET

#ifdef POWERSET
#define NTSC_HI  {    DDRB=0;}
#define NTSC_LOW {    DDRB=_BV(4); }
#define NTSC_VH  {    DDRB=_BV(3); }
#elif defined( POWERSET2 )
#define NTSC_VH  {    DDRB=0;}
#define NTSC_LOW {    DDRB=_BV(4)|_BV(3); }
#define NTSC_HI  {    DDRB=_BV(3); }
#elif defined( POWERSET3 )
#define NTSC_VH  {    DDRB=0; }
#define NTSC_HI   { DDRB=_BV(3); }
#define NTSC_LOW   {    DDRB=_BV(4)|_BV(3); }
#else

//Experimental mechanisms for changing power. Don't work.
#define NTSC_VH  {    OCR1C = 3; TCNT1 = 0; }
#define NTSC_HI  {    OCR1C = 6; TCNT1 = 0;}
#define NTSC_LOW {    OCR1C = 0; TCNT1 = 0;}

#endif
    uint8_t line, i;

    #define TIMEOFFSET .12 //.12
    #define CLKOFS .12

    uint8_t frame = 0, k, ctll;
    char stdsr[8*13];
    sprintf( stdsr, "Fr: " );
    sprintf( stdsr+8,  "HalfByte" );
    sprintf( stdsr+16, "  Blog  " );
    sprintf( stdsr+24, "        " );
    sprintf( stdsr+32, " Trinket" );
    sprintf( stdsr+40, "AdaFruit" );
    sprintf( stdsr+48, "        " );
    sprintf( stdsr+56, "        " );
    sprintf( stdsr+64, "        " );
    sprintf( stdsr+72, "        " );
    sprintf( stdsr+80, "        " );
    sprintf( stdsr+88, "        " );


    ADMUX =/* _BV(REFS1)  |  _BV(ADLAR) | */ 1; //1 = PB2
    ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADATE) | _BV(ADPS2) | _BV(ADPS1);

#define LINETIME 21 //Linetime of 7..20 is barely valid. So,
//#define WAITTCNT while(TCNT0);
#define RESETCNT {TCNT0 = LINETIME; TIFR|=_BV(TOV0); GTCCR|=PSR0;sei();}

//#define WAITTCNT while(!(TIFR&_BV(TOV0)));
#define WAITTCNT sleep_cpu();

//#define WAITTCNT fintcnt();

     sleep_enable();
    sei();

    uint16_t ovax; //0..1024 = 0...5v
    uint8_t  msd;
    uint8_t  lsd;

    while(1)
    {

        frame++;
        //H = 1./15734.264 = 63.555 / 2 = 31.7775
        for( line = 0; line < 6; line++ )
        { NTSC_LOW; _delay_us(2.3-TIMEOFFSET); NTSC_HI; _delay_us(29.5-TIMEOFFSET-CLKOFS); }
        for( line = 0; line < 6; line++ )
        { NTSC_LOW; _delay_us(27.1-TIMEOFFSET); NTSC_HI; _delay_us(4.7-TIMEOFFSET-CLKOFS); }
        for( line = 0; line < 6; line++ )
        { NTSC_LOW; _delay_us(2.3-TIMEOFFSET); NTSC_HI; _delay_us(29.5-TIMEOFFSET-CLKOFS); }

        for( line = 0; line < 39; line++ )
        {
            RESETCNT;
            NTSC_LOW;
            _delay_us(4.7-TIMEOFFSET);
            NTSC_HI;

            //Do whatever you want.
            //sprintf( stdsr, "%d", frame );
            switch (line)
            {
            case 0:
                NumToText( stdsr+4, frame );
                break;
            case 1:
                ovax = ADC;
                ovax = ovax * 49 + (ovax>>1);
                ovax/=10;
                break;
            case 2:
                NumToText( stdsr+24, ovax/1000 );
                stdsr[27] = '.';
                break;
            case 5:
                NumToText4( stdsr+27, ovax );
                stdsr[27] = '.';

                break;
            }

            WAITTCNT;
            //_delay_us(58.8-TIMEOFFSET-CLKOFS);
        }

        for( line = 0; line < 2; line++ )
        {
            RESETCNT;
            NTSC_LOW;
            _delay_us(4.7-TIMEOFFSET);
            NTSC_HI;
            WAITTCNT;
        }

        for( line = 0; line < 220; line++ )
        {
            RESETCNT;
            NTSC_LOW; _delay_us(4.7-TIMEOFFSET);
            NTSC_HI; _delay_us(8-TIMEOFFSET-CLKOFS);

//#define LINETEST
#ifdef LINETEST
            NTSC_VH; _delay_us(8-TIMEOFFSET-CLKOFS);
            NTSC_HI; _delay_us(44.5);
#else
            ctll = line>>2;
    
            for( k = 0; k < 8; k++ )
            {
      // draw the character, one pixel at a time
            uint8_t ch = pgm_read_byte( &font_8x8_data[(stdsr[k+((ctll>>3)<<3)]<<3)] + (ctll&0x07) );
            for( i = 0; i < 8; i++ )
            {
                if( (ch&1) ) //if pixel is dark...
                {
                    NTSC_VH;
                }
                else
                {
                    NTSC_HI;  // pixel is lit
                    NOOP;
                }
                ch>>=1;
                NOOP; NOOP; NOOP; NOOP;
            }
                    NTSC_HI;

            }

            NTSC_HI; //_delay_us(4.7-TIMEOFFSET-CLKOFS);
            WAITTCNT;
#endif

//        NTSC_HI; _delay_us(46-TIMEOFFSET-CLKOFS);

//            NTSC_VH; _delay_us(32-TIMEOFFSET-CLKOFS);
//            NTSC_HI; _delay_us(19.8-TIMEOFFSET-CLKOFS);
        }
    }
   
    return 0;
}

 

Download the original code and the font file here. You will need the font file to make this work.

Mr. Lohr’s YouTube Channel

A Spark of Life: More DigiSpark fun

WP_20151019_22_42_59_Pro__highresI recently wrote about the cool little DigiSpark ATTiny 85 controller board. This little USB wonder, like the AdaFruit Trinket, is based on the ATTiny85 Microcontroller and has limited I/O, memory and is cheap, very cheap.

One thing it is not short on, however, is uses. This little thing can be used for a variety of things, including games and control applications.  For any use, though, you will need some kind of input and some kind of output.  In a previous post, I postulated about porting my ‘Battlestar Galactica’ game to the DigiSpark from the Trinket.  So, here are the results of that endeavor and some other things as well.

For the backstory of the game and why I wrote it, check out this post. I’ll wait.

Read it?  Cool.WP_20151019_22_44_34_Pro__highres

Ok, so there are few differences between the Trinket and the DigiSpark, other than size.  The way you program it is very similar and the pins are nearly identical.  The way you talk to things, is, however, a little different.

The Trinket needed SoftwareSerial. We don’t here, we just use DigitalRead and DigitalWrite and Serial.write to read the button and write to the screen.

The display is a 16×2 Serial LCD from Parallax.  The button is actually a ‘controller’ I built a couple of years ago for my game console. I did not use it for that, opting for the Wii Nunchuck instead.  So, the controller has three buttons that would be used for left, right and ‘fire’ or action.  Here, they just return a 0 if pressed and a 1 when ‘open’.  The code periodically polls the controller for a press and then acts on the press to evaluate a hit or miss.

WP_20151019_22_44_06_Pro__highres

Now, one of the problems I had was the lack of GROUND pins. So, I made a ground bus, a four pin strip with a wire connecting all four pins. I then connected this to ground on the DS and the button and the LCD as well are connected to this bus. I have one free ground pin.  Well, what I did not realize was that you can use any pin for ground.  While I did not change my connections, I find this pretty cool.

TIP! To use a free pin as ground:

(From the wiki)

You can use an I/O pin as a ground/+5V if you have some left and the current that flow in it is low (a few milliamps if you want the pin to stay around 0V/+5V. If you don’t care about the exact voltage you can go up to 40mA per pin according to the datasheet but it’s good to keep a safety margin).

Configure the pin as an output and set its value to either LOW or HIGH (for ground or +5V

#define button 0
#define gnd 2
 
void setup() {                
  pinMode (button, INPUT);
  digitalWrite (button, HIGH); // enable pullup
  pinMode(gnd, OUTPUT);
  digitalWrite (gnd, LOW); // use this pin as a ground
}

For much more information and tips, go to the wiki, located here.

The game code is below.

Come back for more on this cool little device.  Since it is capable of serial i/o, I’m thinking of using the Bluetooth Serial module and connecting the DS to one of my HalfByte Consoles running the graphical serial software for video display from the DS.

WP_20151019_22_45_35_Pro__highresWP_20151019_22_43_27_Pro__highres

Game Code:

#include <TinyPinChange.h>

 // globals
 int  cylonpos;
 int  oldcylonpos;
 int  cyDirection;
 int  button;
 int  posStart;
 int  lin2Start;
 int  bsgpos;
 
 // special characters
 int  bsg;
 int  cylon;
 
 // total number of torpedos the ship has left
 int  tShots=10;
 
 // the setup routine runs once when you press reset:
 void waitforbutton();
 void moveCylon();
 
 void setup() {  // initialize the digital pin as an output.  
   Serial.begin(9600); 
   Serial.write(12);
   Serial.write(17);
   
   //define the ship character
   Serial.write(249);
   Serial.write(0x4);
   Serial.write(0xE);
   Serial.write(0xE);
   Serial.write(0x15);
   Serial.write(0x1F);
   Serial.write(0x15);
   Serial.write(0xE);
   Serial.write(0xE);
   
   // baddies
   Serial.write(251);

   Serial.write(uint8_t(0x0));//to handle a goofy issue with '0'
   Serial.write(uint8_t(0x0));
   Serial.write(0xE);
   Serial.write(0x15);
   Serial.write(0x1F);
   Serial.write(uint8_t(0x0));
   Serial.write(uint8_t(0x0));
   Serial.write(uint8_t(0x0));
 
 // the button is on pin 0  
   pinMode(0,INPUT);
   
 // the initial direction is right
   cyDirection=1;
   
 } // the loop routine runs over and over again forever:
 void loop() {
  int score;
  score=0;
  
  Serial.write(12); // clear the screen and show the instructions
  Serial.println("Shoot the Cylon\n");
  Serial.println("press btn strt");
  
  // wait for the button
  waitforbutton();
  
  // define somethings before we start the game loop
  tShots=10;
  cylon=3;
  bsg=1;
  lin2Start=148; // the baddies appear on line 1
  posStart=128;  // the good guys on line 2
  randomSeed(analogRead(0)); // gen a random seed
  
  bsgpos = 8;  // our ship starts in the middle of the sector
  cylonpos=random(0,15); // baddies warp in at a random spot
  oldcylonpos=cylonpos;
  
  Serial.write(12);  // clear screen
  Serial.write(17);  // turn on backlight so we can see
  Serial.write(posStart + cylonpos);  // uh oh...the Cylon appeared!
 
  Serial.write(cylon);
  Serial.write(lin2Start+bsgpos);
  Serial.write(bsg); // and here we are
  
  // begin the game 
  while(1){
    /* Game loop */
    int button;
    Serial.write(22); // kill the cursor
    moveCylon();
    delay(200);
    button=digitalRead(0);  // check the button
    
    if(button==0){ // torpedo fired?
      if(cylonpos==bsgpos){ // Hit?
        Serial.write(132); // center 'Hit' on screen
        Serial.print("** HIT **");
        score ++;  // update score
        Serial.write(213); // noise (C at half note, 3rd scale)
        Serial.write(215); // play the note
        Serial.write(224);
        Serial.write(225);
        if(score==10){  // did we win?
           Serial.write(12); // clear screen
           Serial.write(128);// start at beginning
           Serial.print("You have saved\nthe Galactica.");
           delay(2000);
           loop();  // start game again
        }
        delay(3000); // did not win yet
        Serial.write(12); // clear screen
        Serial.write(lin2Start); // move cursor to beginning of line
        Serial.write("Score:");  // put up score
        Serial.print(score);
        Serial.write(lin2Start+bsgpos); // Since we shot the Cylon, we have to leave the sector, so...
        Serial.write(0x20); // go into hyperdrive and erase the BSG
        bsgpos=random(9,15);  // calculate new position
        Serial.write(lin2Start+bsgpos); //...and, we are here
        Serial.write(bsg);
        Serial.write(posStart+cylonpos);
        Serial.write(0x20);
        cylonpos=random(0,15); // Damn, they found us!
        oldcylonpos=cylonpos;
        moveCylon();
      } 
      
      tShots --; // decrement our torpedo inventory
      if(tShots<1){ // out of ammo?
        Serial.write(12);  // clear screen
        Serial.write(posStart); // start at beginning of line
        Serial.print("The Cylons win.\n");
        int count=1;
        // show animation of our ship exploding
        while (count<10){
          Serial.write(lin2Start+bsgpos);
          Serial.write(bsg);
          delay(500);
          Serial.write(lin2Start+bsgpos);
          Serial.write('*');
          delay(500);
          count++;
        }        
        loop(); // Play again
      }
    }  
  }
}
void moveCylon(){
  // sneaky Cylons...they show up anywhere!
  Serial.write(posStart+oldcylonpos);
  Serial.write(0x20);
  if (random(0,6)>3){
      //Cylon warped
      cylonpos=random(0,15);
      oldcylonpos=cylonpos;
  }
  
  cylonpos += cyDirection; // figure out which way to move
  if (cylonpos>15){
   cylonpos=15;
   cyDirection=-1;
  }
  
  if (cylonpos<0){
    cylonpos=0;
    cyDirection=1;
  }
  // erase from old spot
  Serial.write(posStart+oldcylonpos);
  Serial.write(0x20);
  Serial.write(posStart+cylonpos);
  Serial.write(cylon);
  oldcylonpos=cylonpos;
} 

void waitforbutton(){
   
  int button;
  button=digitalRead(0);
  while(button==1){     //wait for button press
    button=digitalRead(0);
    delay(1000);
  }  
  Serial.write(1);  
  delay(1000);
  Serial.write(12); 
}

The Mighty ATTiny85 and DigiSpark

Microcontrollers are lots of fun to play with as well as to build useful things.  They come in all sizes, shapes and varieties.  I am always, however, intrigued with the tiny controllers, like AdaFruit’s Trinket.  Well, the latest one I’ve started to tackle is the DigiSpark.  The DigiSpark is small and comes in several types. I am using the USB flavor. This little guy is on a very small board, about the size of a nickel, and is, itself, a USB dongle.

WP_20150928_23_59_09_Pro (2)

It features six I/O lines, plus power and ground pins.  This thing is a little bit more versatile than the original Trinket, but not much more. The USB version has the added advantage of being a USB dongle itself, meaning you can build a project that plugs into a USB port and act as an external controller. You don’t need USB, once you have programmed it, the power and ground pins can be used to power the device.

The nice thing about the DigiSpark is that you can use the Arduino IDE and SOME of the libraries to program the device.  There is a nice tutorial on setting up a newer release of the IDE and add DigiSpark support.

In a nutshell:

  1. Start the IDE (Install 1.6.x if you don’t already have it installed)
  2. Click FILE, then PREFERENCES
  3. in the Additional Boards Manager URL’s box, enter: http://digistump.com/package_digistump_index.json
  4. Click OK
  5. Click Tools, then BOARDS
  6. Select Board Manager and then Contributed
  7. In the dialog box that pops up, select DIGISTUMP AVR BOARDS
  8. Click Install
  9. Close the Board Manager
  10. You should now see the DigiSpark boards in the IDE

The thing that tripped me up was uploading a sketch.  I didn’t realize that you don’t need to have the device plugged in prior to uploading (you are told this, but it didn’t sink in.)  Once I unplugged the device, clicked upload and THEN plugged it in, it worked like a charm.

So, now I have to figure out what I want to do with this cool little device. 

But, wait…I still have not really said anything about the device, what it has on it, etc.

It is based on the ATTiny 85 and features six I/O pins:

Pin outs:

    • All pins can be used as Digital I/O

    • Pin 0 → I2C SDA, PWM (LED on Model B)
    • Pin 1 → PWM (LED on Model A)
    • Pin 2 → I2C SCK, Analog
    • Pin 3 → Analog In (also used for USB+ when USB is in use)
    • Pin 4 → PWM, Analog (also used for USB- when USB is in use)
    • Pin 5 → Analog In

The ATTiny 85 specs:

  • 8 Bit Data Bus
  • 20 MHz Max Clock Frequency ( w/ external crystal )
  • 8 kB Program Memory Size
  • 2.7 V to 5.5 V Operating Supply Voltage
  • 6 I/O Pins
  • 512 bytes of RAM

Limitations:

Two things you cannot do from the IDE: burn the bootloader and use the serial monitor. Some libraries will work, most will not.  There is no short circuit or reverse polarity protection, so be careful or you will certainly destroy the board, and, if connected via USB (which it really should not be if using external power) the computer could be damaged as well.

SAMPLE IDE OUTPUT

The IDE, as with other Arduino’s, displays the compiler and upload progress in the text area below the coding window.  Several rather bothersome messages can show, but, not to worry, it is normal. See my sample output below.

Sketch uses 668 bytes (11%) of program storage space. Maximum is 6,012 bytes.
Global variables use 9 bytes of dynamic memory.
Running Digispark Uploader…
Plug in device now… (will timeout in 60 seconds)
> Please plug in the device …
> Press CTRL+C to terminate the program.
> Device is found!
connecting: 16% complete
connecting: 22% complete
connecting: 28% complete
connecting: 33% complete
> Device has firmware version 1.6
> Available space for user applications: 6012 bytes
> Suggested sleep time between sending pages: 8ms
> Whole page count: 94  page size: 64
> Erase function sleep duration: 752ms
parsing: 50% complete
> Erasing the memory …
erasing: 55% complete
erasing: 60% complete
erasing: 65% complete
>> Eep! Connection to device lost during erase! Not to worry
>> This happens on some computers – reconnecting…
>> Reconnected! Continuing upload sequence…
> Starting to upload …
writing: 70% complete
writing: 75% complete
writing: 80% complete
> Starting the user app …
running: 100% complete
>> Micronucleus done. Thank you!

Micronucleus is the name of the bootloader.  The disconnect message was a little disturbing, but not a problem.

Maybe I’ll adapt my Battlestar Galactica LCD game to this thing…

Here is a link to the Trinket posts on the blog. It is very similar to the DigiSpark.

digiStump is the maker of the device and they have other products as well. Take a gander around their site. There is a nice Wiki page and forums.

Finally, you can purchase one from Amazon for $4.88 each.  Of course, you can support digiStump by purchasing the device from them, they sell it for $8.95, a little higher than Amazon, but you will be supporting the makers and encourage further development of the product.

A Video Game on my Trinket, take 2

Several posts ago, I wrote about writing a video game for the Trinket from Adafruit. Well, since then, I have added scoring, sound and a bit more animation (very crude, mind you, I can only do so much in the small memory and limited screen.)

The setup uses the Trinket, Parallax’s 2×16 LCD panel and a switch connected to Pin 0. The LCD panel is connected to Pin 2.  You will need to import the SoftwareSerial library for the LCD.

The premise of the game is based on the Battlestar Galactica TV show from 2003-2010.  The Galactica was engaged in heavy battle and barely managed to escape before certain destruction. All of her gun turrets, save the forward torpedo launcher, are damaged and non-functional, meaning she can only fight head on.  The Viper squads are still engaging the enemy.  A group of Cylon raiders have found the mighty Battlestar and are taking her on, one by one. Oh, you only have ten torpedo’s left, so use them wisely.

The game ends when you use up your ten torpedo’s.

Below is the revised listing for the game.  Apologies to Glen Larsen.

 

Listing 1.

#include <SoftwareSerial.h>

SoftwareSerial s(0,2);
 
// globals
int  cylonpos;
int  oldcylonpos;
int  cyDirection;
int  button;
int  posStart;
int  lin2Start;
int  bsgpos;
 
// special characters
int  bsg;
int  cylon;
 
// total number of torpedos the ship has left
int  tShots=10;
 
// the setup routine runs once when you press reset:
void waitforbutton();
void moveCylon();
 
void setup() {  // initialize the digital pin as an output. 
   s.begin(9600);
   s.write(12);
   s.write(17);
  
   //define the ship character
   s.write(249);
   s.write(0x4);
   s.write(0xE);
   s.write(0xE);
   s.write(0x15);
   s.write(0x1F);
   s.write(0x15);
   s.write(0xE);
   s.write(0xE);
  
   // baddies
   s.write(251);

   s.write(uint8_t(0x0));//to handle a goofy issue with ‘0’
   s.write(uint8_t(0x0));
   s.write(0xE);
   s.write(0x15);
   s.write(0x1F);
   s.write(uint8_t(0x0));
   s.write(uint8_t(0x0));
   s.write(uint8_t(0x0));
 
// the button is on pin 0 
   pinMode(0,INPUT);
  
// the initial direction is right
   cyDirection=1;
  
} // the loop routine runs over and over again forever:
void loop() {
  int score;
  score=0;
 
  s.write(12); // clear the screen and show the instructions
  s.println("Shoot the Cylon\n");
  s.println("press btn 2 strt");
 
  // wait for the button
  waitforbutton();
 
  // define somethings before we start the game loop
  tShots=10;
  cylon=3;
  bsg=1;
  lin2Start=148; // the baddies appear on line 1
  posStart=128;  // the good guys on line 2
  randomSeed(analogRead(0)); // gen a random seed
 
  bsgpos = 8;  // our ship starts in the middle of the sector
  cylonpos=random(0,15); // baddies warp in at a random spot
  oldcylonpos=cylonpos;
 
  s.write(12);  // clear screen
  s.write(17);  // turn on backlight so we can see
  s.write(posStart + cylonpos);  // uh oh…the Cylon appeared!
 
  s.write(cylon);
  s.write(lin2Start+bsgpos);
  s.write(bsg); // and here we are
 
  // begin the game
  while(1){
    /* Game loop */
    int button;
    s.write(22); // kill the cursor
    moveCylon();
    delay(200);
    button=digitalRead(0);  // check the button
    if(button==HIGH){ // torpedo fired?
      if(cylonpos==bsgpos){ // Hit?
        s.write(132); // center ‘Hit’ on screen
        s.print("** HIT **");
        score ++;  // update score
        s.write(213); // noise (C at half note, 3rd scale)
        s.write(215); // play the note
        s.write(224);
        s.write(225);
        if(score==10){  // did we win?
           s.write(12); // clear screen
           s.write(128);// start at beginning
           s.print("You have saved\nthe Galactica.");
           delay(2000);
           loop();  // start game again
        }
        delay(3000); // did not win yet
        s.write(12); // clear screen
        s.write(lin2Start); // move cursor to beginning of line
        s.write("Score:");  // put up score
        s.print(score);
        s.write(lin2Start+bsgpos); // Since we shot the Cylon, we have to leave the sector, so…
        s.write(0x20); // go into hyperdrive and erase the BSG
        bsgpos=random(9,15);  // calculate new position
        s.write(lin2Start+bsgpos); //…and, we are here
        s.write(bsg);
        s.write(posStart+cylonpos);
        s.write(0x20);
        cylonpos=random(0,15); // Damn, they found us!
        oldcylonpos=cylonpos;
        moveCylon();
      }
     
      tShots –; // decrement our torpedo inventory
      if(tShots<1){ // out of ammo?
        s.write(12);  // clear screen
        s.write(posStart); // start at beginning of line
        s.print("The Cylons win.\n");
        int count=1;
        // show animation of our ship exploding
        while (count<10){
          s.write(lin2Start+bsgpos);
          s.write(bsg);
          delay(500);
          s.write(lin2Start+bsgpos);
          s.write(‘*’);
          delay(500);
          count++;
        }       
        loop(); // Play again
      }
    } 
  }
}
void moveCylon(){
  // sneaky Cylons…they show up anywhere!
  s.write(posStart+oldcylonpos);
  s.write(0x20);
  if (random(0,6)>3){
      //Cylon warped
      cylonpos=random(0,15);
      oldcylonpos=cylonpos;
  }
 
  cylonpos += cyDirection; // figure out which way to move
  if (cylonpos>15){
   cylonpos=15;
   cyDirection=-1;
  }
 
  if (cylonpos<0){
    cylonpos=0;
    cyDirection=1;
  }
  // erase from old spot
  s.write(posStart+oldcylonpos);
  s.write(0x20);
  s.write(posStart+cylonpos);
  s.write(cylon);
  oldcylonpos=cylonpos;
}

void waitforbutton(){
  SoftwareSerial s(0,1);
 
  int button;
  button=digitalRead(0);
  while(button==LOW){
    button=digitalRead(0);
    delay(1);
  } 
  s.write(1); 
  delay(1000);
  s.write(12);
}

Coding for small platforms is nearly a lost art OR why I love tiny computers

There was a time when a computer with 4K of memory was something to behold. Indeed, 4K was enough for a BASIC interpreter AND have room left over for user programs. Most I/O was serial, so ALL of that space was for code. The ‘operating system’ was something called a ‘monitor’ or the BASIC itself. Either way, the I/O code was ‘built’ into either the BASIC or the monitor. And, if you had a video terminal, well, you were THE stuff.

Just a few years later, that 4K grew to 16K then 64K and, suddenly, we are talking MEGAbytes. Wow. Well, funny thing happened, code went from small, tight and mostly efficient to BIG, heavy and bloated.  Code in that 4K system that put a character on the screen took, maybe six bytes suddenly took a DLL, which was probably ten times that or more.  Programmers got complacent, including this one.

I recall one job I had, I was maybe 17, that required me to write an accounting screen for a paging system (remember the pocket pager?) I had 2k for the data entry screen AND the billing code. Plus, the datastream to/from the paging system was like 32 bytes.  In that 32 bytes, I had to squeeze in the client number (10 digits), number of pages, and about two dozen status bits and more. I did it. The software went into a dozen or ‘head end’ systems (hey, it was a SMALL market) but it was my first ‘professional’ gig.

My career took me down the Microsoft/Lotus path. I had all kinds of wonders to code with, Visual Studio, Visual Basic, Lotus Notes, web.  My code, while functional and often clever, was sloppy. I forgot what it was to REALLY code.

Well, I had a shot a programming for the Palm Pilot, which reminded me of my old days, but it didn’t last long and I was back to programming for machines with memory measured in the gigabytes.

Imagine my delight when I discovered the world of Microcontrollers.  These things have big capability, but tiny (and I mean tiny) memory spaces. My current microcontroller project, the Adafruit Trinket, based on the ATMel ATtiny85, has 5,2k of available programming memory and 512 BYTES of RAM (as well as 512 bytes of EEPROM.)

Having found some cool 2×16 LCD panels for five bucks each at Radio Shack, these Trinkets and a bunch of parts, I set out to make a game for my step son.  I’m still working on a game concept that will keep his interest, but I did a proof of concept game based on Space Invaders (my all time favorite game.) So, how can one make a Space Invaders on a TWO line by SIXTEEN character display? Well…you need to be creative. AND…you have to fit it, along with a necessary library, into under 5.1K.

So…what did I do? Well, found an old notebook I kept from my TRS-80 days. Looked at some old Tiny Basic game code and…coded.

BSG001First, I needed an idea. The display I am using, from Parallax, allows for eight special characters.  So, I played around with a few designs and one of them looked like the Battlestar Galactica. After some thought, I had the idea: The Galactica’s Viper squadrons are out on patrol. The Galactica was in a fierce battle that has rendered it nearly immovable, it can only hyperjump. It’s turrets are all damaged, save the forward bank, she can only shoot from her forward cannons.  A Cylon has discovered the Galactica and is taking runs at it.  Problem is, the Cylon is jumping randomly, making a direct hit nearly impossible.  If, however, the Galactica does manage to destroy the Cylon, the Galactica then hyperjumps too.  Then, it all happens again.

Cool, huh?IMG_2758

So, I set out to code it. I put code together to read a switch, move a character back and forth and come up with an interrupt mechanism for the button.

Turns out, it was simpler than I thought.  I had some hiccups with the Arduino IDE (which involved upgrading the toolchain, upgrading part of it again, some choice words…) but got them ironed out and, now, the Colonials are fighting the nasty Cylons again.

First, the source code:

#include <SoftwareSerial.h>

SoftwareSerial s(0,2);
 
// globals
int  cylonpos;
int  oldcylonpos;
int  cyDirection;
int  button;
int  posStart;
int  lin2Start;
int  bsgpos;
// special characters
int  bsg;
int  cylon;
 
// the setup routine runs once when you press reset:
void waitforbutton();
void moveCylon();
 
void setup() {  // initialize the digital pin as an output. 
   s.begin(9600);
   s.write(12);
   s.write(17);
   //define the character
   s.write(249);
   s.write(0x4);
   s.write(0xE);
   s.write(0xE);
   s.write(0x15);
   s.write(0x1F);
   s.write(0x15);
   s.write(0xE);
   s.write(0xE);
  
   // baddies
   s.write(251);

   s.write(uint8_t(0x0));//to handle a goofy issue with ‘0’
   s.write(uint8_t(0x0));
   s.write(0xE);
   s.write(0x15);
   s.write(0x1F);
   s.write(uint8_t(0x0));
   s.write(uint8_t(0x0));
   s.write(uint8_t(0x0));
  
   pinMode(0,INPUT);
   cyDirection=1;
  
} // the loop routine runs over and over again forever:
void loop() {
  int score;
  score=0;
 
  s.write(12);
  s.println("Shoot the Cylon\n");
  s.println("press btn 2 strt");
 
  waitforbutton();
 
  cylon=3;
  bsg=1;
  lin2Start=148;
  posStart=128;
  randomSeed(analogRead(0)); // gen a random seed
 
  bsgpos = 8;
  cylonpos=random(0,15);
  oldcylonpos=cylonpos;
 
  s.write(12);
  s.write(17);
  s.write(posStart + cylonpos);
 
  s.write(cylon);
  s.write(lin2Start+bsgpos);
  s.write(bsg);
 
  while(1){
    /* Game loop */
    int button;
    s.write(22); // kill the cursor
    moveCylon();
    delay(200);
    button=digitalRead(0);
    if(button==HIGH){
      if(cylonpos==bsgpos){
        s.write(132); // center ‘Hit’ on screen
        s.print("** HIT **");
        score ++;  // update score
        delay(3000);
        s.write(12); // clear screen
        s.write(lin2Start); // move cursor to beginning of line
        s.write("Score:");  // put up score
        s.print(score);
        s.write(lin2Start+bsgpos); // Since we shot the Cylon, we have to leave the sector, so…
        s.write(0x20); // go into hyperdrive and erase the BSG
        bsgpos=random(9,15);  // calculate new position
        s.write(lin2Start+bsgpos); //…and, we are here
        s.write(bsg);
        s.write(posStart+cylonpos);
        s.write(0x20);
        cylonpos=random(0,15); // Damn, they found us!
        oldcylonpos=cylonpos;
        moveCylon();
      }
    } 
  }
}
void moveCylon(){
  // sneaky Cylons…they show up anywhere!
  s.write(posStart+oldcylonpos);
  s.write(0x20);
  if (random(0,6)>3){
      //Cylon warped
      cylonpos=random(0,15);
      oldcylonpos=cylonpos;
  }
 
  cylonpos += cyDirection; // figure out which way to move
  if (cylonpos>15){
   cylonpos=15;
   cyDirection=-1;
  }
 
  if (cylonpos<0){
    cylonpos=0;
    cyDirection=1;
  }
  // erase from old spot
  s.write(posStart+oldcylonpos);
  s.write(0x20);
  s.write(posStart+cylonpos);
  s.write(cylon);
  oldcylonpos=cylonpos;
}

void waitforbutton(){
  SoftwareSerial s(0,1);
 
  int button;
  button=digitalRead(0);
  while(button==LOW){
    button=digitalRead(0);
    delay(1);
  } 
  s.write(1); 
  delay(1000);
  s.write(12);
}

SInce the Trinket is limited, I/O wise, I have only five pins, two of which are shared with the USB programmer and one is shared with the red programming LED, it really limits what you can add. At least, for those of us who are electronically challenged. I have my LCD on pin 2 and the switch on pin 0.  The LCD is controlled with a single line, it also has a five volt line (which goes to the USB power) and ground. The switch is connected to power and a 10k ohm resistor. It connects to pin 0. In the LOOP code, there is a While loop that handles the game logic. Inside the loop, it checks the pin for HIGH, which means the button was pressed. It then checks the position of the BSG and the Cylon. If there is a hit, it tells the user that they hit the Cylon and then increments the score, recalculates the positions and does it all again.  Simple, but kind of hard since the Cylon jumps so much.

While the game is not sophisticated, it does show what you can do in 5K of memory, a limited input mechanism and very limited display.  Now, about the game for my step son…

 

Adafruit, USBTinyISP, Digital Signature and Windows 8: hoop jumping time

I do development work on a variety of PC’s in my home, Windows 7, Windows 8 and, yes, even on a Windows Vista laptop.  So far, it’s not been a problem. Oh, sure, my ancient copy of Visual Studio 2005 tells me it is not compatible with Windows 8, but ignore the warning and keep on going…no issues yet and I’ve been running it for awhile now.  However, drivers for things like USB programmers have not been an issue (well, OK, there was the one Radio Shack Serial to USB cable that Windows 8 hates) until now.

IMG_2603I go to setup one of my Windows 8 desktops with the Arduino IDE so I can do some work with the Adafruit Trinket. The IDE installs just fine. I make all of the changes that Adafruit says I need to make to the IDE so it can support the Trinket.  Everything is great…until I try to install USBTinyISP. It’s drivers are not digitally signed.  Windows 8 does not allow unsigned drivers.  Well, damn. So, off to the ‘net I go.

I BING ‘USBTinyISP and Windows 8’ and get this site, Next of Windows. Here, they have a short tutorial on how to enable the ability to install unsigned drivers. Now, I understand why Microsoft did this…it was designed to protect consumers and help ensure only good stuff gets on your computer.

Rather than retread what Next of Windows has already done, just go there and follow the instructions. There are a few restarts involved as you are accessing settings that are only mean to be accessed if your PC is having issues with Windows.

Now, while Microsoft may be partly to blame, I also wonder why Adafruit does not supply a signed driver or, at least, tell you how to install it under Windows 8. It is, after all, on the LEARNING System.

Once I got the driver installed, Arduino IDE was able to program the Trinket and all is well with the universe.

UPDATE: It seems that Adafruit DID, in fact, post instructions for installing on Windows 8. Apologies to Adafruit are in order [wipes egg from face.] Here’s the link to the page that actually says ‘Don’t forget, for Windows 8, you will have to turn off driver signing checking..’. I don’t know how I missed that.

The Trinket: small, versatile and cute

IMG_2603I’ve become a fan of microcontrollers. These little marvels can do a lot: control motors, lights, LED’s,LCD’s, servo mechanisms, keyboards, mice…you name it, it probably has one of these things in it.  Some of them, such as the PIC24, are as powerful as yesterday’s microprocessor and can even run real operating systems or video games (the PIC24 is the heart of the XGS PIC 16 bit board I’ve been porting BASIC-the same BASIC will also run on the Arduino line of microcontroller based development boards.)

Adafruit Industries is one of the leaders in the burgeoning hobby that has sprung up around IMG_2631these little beasts.  They recently introduced the Trinket. The Trinket, based on the ATtiny85 controller, is about an inch long by half inch wide (approx.) and contains a sole USB connection and five I/O lines.

The Trinket is easy to use and program. Using Arduino IDE (free from the Arduino web site) you write using C++ and then upload the resulting code to the device. This is the only thing about the device that I have issue with: You have a very short time in which the device is programmable. The IDE is rather slow, and the interval with which the device will program is about ten seconds. You have to repeatedly press the reset button on the device and hope that you have time to program it, otherwise, you have to start over and the IDE does not appear to be smart enough to know that the source has already been compiled. 

IMG_2637The upload process aside, everything else is rather straight forward. There are a number of guides on the Adafruit site to explain how to set to set up the IDE.  Basically, download and install the IDE, then USBTiny, the driver, then there are two config files you need to download and copy into directories under Arduino IDE and in the Documents directory. Complete instructions are here. I suggest you read through the entire article and then do the install.

Once installed, you would code as if it were an Arduino.  Keep in mind, there is no Serial port, but there is a library that ‘maps’ one to one of the I/O pins. It works, great. I used it to talk to one of the Parallax 2×16 LCD panels I picked up from Radio Shack. See my source code snippet below for more.

One of the really nice features is that you can power this thing using three 1.5 volt AAA batteries. Input voltage can be from 3.3 to 16 volts.  The device comes in 3v and 5v flavors, meaning you have to be careful about what you connect to the device. I got two 3v boards as the 5v boards were sold out. I tried with both a 9v battery and three 1.5 volt AAA cells. Of course, you can also power it from the USB connections. And anything connected will, likely, need its own power source.

At $7.95, you cannot really go wrong.  Check out Adafruit’s site for project ideas. I have a couple in mind, including that Star Trek game I wrote of, several posts ago.  In a future post, I will share what I have done with these as well as how my homebrew PIC16 project turns out. And look for more on that Tiny Basic port to the XGS.

#include <SoftwareSerial.h>

SoftwareSerial s(0,1); //receive on "0", and transmit on "1" aka "PB1" aka pin 6

void setup() {   s.begin(9600);// set baud rate to 9600 baud   }    void loop() {   s.write(12);// clear screen   s.write(17);// turn on backlight   s.println("Adafruit Trinket");   s.println("2x16LCD Parallax");      }