[Coco] Wico Command Control Trackball on eBay

Barry Nelson barry.nelson at amobiledevice.com
Wed Oct 14 21:38:49 EDT 2015


This is an updated Arduino sketch after some minimal testing with a mouse and a volt meter. I still have not connected this to a CoCo yet though. This version increases the pulse frequency of the PWM to the maximum so the output voltage is more stable and requires less filtering. It also corrects some incorrect assumptions about the values returned from the mouse.

#include <ps2.h>

/*
 * an Arduino sketch to interface a ps/2 mouse
 * to a Tandy Color Computer.
 * The Arduino should be able to get power from the joystick port
 */

/*
 * Pin 3 is the mouse data pin, pin 4 is the clock pin
 * Feel free to use whatever pins are convenient.
 */
PS2 mouse(3, 4);

#define CoCoXpin 5
#define CoCoYpin 6
#define CoCoButton1 7
#define CoCoButton2 8

#define XMax 1023
#define YMax 1023

/*
 * initialize the mouse. Reset it, and place it into remote
 * mode, so we can get the encoder data on demand.
 */
void mouse_init()
{
  mouse.write(0xff);  // reset
  mouse.read();  // ack byte
  mouse.read();  // blank */
  mouse.read();  // blank */
  mouse.write(0xf0);  // remote mode
  mouse.read();  // ack
  delayMicroseconds(100);
}

void setup()
{
  pinMode(CoCoXpin, OUTPUT);
  pinMode(CoCoYpin, OUTPUT);
  pinMode(CoCoButton1, OUTPUT);
  pinMode(CoCoButton2, OUTPUT);
  mouse_init();
  setPwmFrequency(CoCoXpin, 1);
  setPwmFrequency(CoCoYpin, 1);
  analogWrite(CoCoXpin,128); // start with CoCo x centered
  analogWrite(CoCoYpin,128); // start with CoCo y centered
  digitalWrite(CoCoButton1,HIGH); // start with CoCo button 1 not pressed
  digitalWrite(CoCoButton2,HIGH); // start with CoCo button 2 not pressed
}

/*
 * get a reading from the mouse and set the output lines
 * appropriately.
 */
void loop()
{
  char mstat;
  char mx;
  char my;
  int cocoxv=XMax/2; // start with CoCo x centered
  int cocoyv=YMax/2; // start with CoCo y centered

  /* get a reading from the mouse */
  mouse.write(0xeb);  // give me data!
  mouse.read();      // ignore ack
  mstat = mouse.read();
  mx = mouse.read();
  my = mouse.read();
  cocoxv += mx; // add mouse movement to CoCo x position
  if (cocoxv > XMax) { // limit to 0 - 5 volts
    cocoxv=XMax;
  }
  if (cocoxv < 0) {
    cocoxv=0;
  }
  cocoyv += my; // add mouse movement to CoCo y position
  if (cocoyv > YMax) { // limit to 0 - 5 volts
    cocoyv=YMax;
  }
  if (cocoyv < 0) {
    cocoyv=0;
  }
  analogWrite(CoCoXpin,map(cocoxv,0,XMax,0,255)); // Set CoCo x value
  analogWrite(CoCoYpin,map(cocoyv,0,YMax,0,255)); // Set CoCo y value
  if (mstat & 1) { // Mouse left button pressed?
    digitalWrite(CoCoButton1,LOW); // Press CoCo button 1
  } else {
    digitalWrite(CoCoButton1,HIGH);
  }
  if (mstat & 2) { // Mouse right button pressed?
    digitalWrite(CoCoButton2,LOW); // Press CoCo button 2
  } else {
    digitalWrite(CoCoButton2,HIGH);
  }
  // mstat & 4 or bit 2 is the mouse middle button
  // mstat & 8 or bit 3 is always on
  // mstat & 16 or bit 4 is x direction
  // mstat & 32 or bit 5 is y direction
  // mstat & 64 or bit 6 is X overflow
  // mstat & 128 or bit 7 is Y overflow
}

/**
 * Divides a given PWM pin frequency by a divisor.
 * 
 * The resulting frequency is equal to the base frequency divided by
 * the given divisor:
 *   - Base frequencies:
 *      o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
 *      o The base frequency for pins 5 and 6 is 62500 Hz.
 *   - Divisors:
 *      o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
 *        256, and 1024.
 *      o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
 *        128, 256, and 1024.
 * 
 * PWM frequencies are tied together in pairs of pins. If one in a
 * pair is changed, the other is also changed to match:
 *   - Pins 5 and 6 are paired on timer0
 *   - Pins 9 and 10 are paired on timer1
 *   - Pins 3 and 11 are paired on timer2
 * 
 * Note that this function will have side effects on anything else
 * that uses timers:
 *   - Changes on pins 3, 5, 6, or 11 may cause the delay() and
 *     millis() functions to stop working. Other timing-related
 *     functions may also be affected.
 *   - Changes on pins 9 or 10 will cause the Servo library to function
 *     incorrectly.
 * 
 * Thanks to macegr of the Arduino forums for his documentation of the
 * PWM frequency divisors. His post can be viewed at:
 *   http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
 */
void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  }
#if !defined(__AVR_ATmega32U4__)  
  else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x7; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
#endif
}


More information about the Coco mailing list