Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 41f16ac6c9
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 293 lines (239 sloc) 7.227 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

#define A6281_CLOCK_PIN PORTD0
#define A6281_ENABLE_PIN PORTD1
#define A6281_LATCH_PIN PORTD2
#define A6281_DATA_PIN PORTD3
#define LED_OUTPUT_REGISTER DDRD
#define LED_PORT PORTD


#include "tpad4-mega.h"
#include "led.h"
#include <stdlib.h>


uint8_t bootloader_force;

/** LUFA CDC Class driver interface configuration and state information. This structure is
* passed to all CDC Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another.
*/
USB_ClassInfo_CDC_Device_t tpad_CDC_Interface =
  {
    .Config =
      {
        .ControlInterfaceNumber = 0,
        .DataINEndpoint =
          {
            .Address = CDC_TX_EPADDR,
            .Size = CDC_TXRX_EPSIZE,
            .Banks = 1,
          },
        .DataOUTEndpoint =
          {
            .Address = CDC_RX_EPADDR,
            .Size = CDC_TXRX_EPSIZE,
            .Banks = 1,
          },
        .NotificationEndpoint =
          {
            .Address = CDC_NOTIFICATION_EPADDR,
            .Size = CDC_NOTIFICATION_EPSIZE,
            .Banks = 1,
          },
      },
  };

int ParserState[4];

/** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
* used like any regular character stream in the C APIs
*/
static FILE USBSerialStream;
static bool connected = false, wasConnected = false;

int button = 0;
int buttonChannels[4] = { ADC_CHANNEL0, ADC_CHANNEL1, ADC_CHANNEL4, ADC_CHANNEL5 };
uint16_t results[4];
int buttonMapping[4] = { 2, 3, 1, 0 };
bool reading = false;

uint16_t colors[4][3] = {
  { 0, 0, 0},
  { 0, 0, 0},
  { 0, 0, 0},
  { 0, 0, 0}
};

void updateLeds(void) {
  for (int i = 0; i<4; i++) {
    led_send(
      led_createColor(
        colors[buttonMapping[i]][0],
        colors[buttonMapping[i]][1],
        colors[buttonMapping[i]][2]
      )
    );
  }

  led_latch();
}


int parserPos = 0, bufferPos;
char colorBuffer[8];
void parseChunk(uint16_t byte) {
  char current = (char)byte;

  if (current == '!') {
    bootloader_force = 0xbb;
    wdt_enable(WDTO_250MS);
    for (;;);
  } else if (current == ',' || current == '\n') {
    ParserState[parserPos] = atoi(colorBuffer);

    memset(colorBuffer, 0, sizeof(colorBuffer));
    bufferPos = 0;
    parserPos++;
  } else {
    colorBuffer[bufferPos] = current;
    bufferPos++;

    // avoid buffer overruns
    if (bufferPos > 7) {
      bufferPos = 0;
      memset(colorBuffer, 0, sizeof(colorBuffer));
      // bail on this parse
      parserPos = 0;
    }
  }

  if (parserPos > 3) {
    parserPos = 0;
    bufferPos = 0;

    colors[ParserState[0]][0] = ParserState[1];
    colors[ParserState[0]][1] = ParserState[2];
    colors[ParserState[0]][2] = ParserState[3];
    memset(ParserState, 0, sizeof(ParserState));
  }
}

/*

Allow a reset into the (adafruit CDC) bootloader

+ CDC bootloader source https://github.com/adafruit/lufa-lib/blob/master/trunk/Bootloaders/CDC/BootloaderCDC.c#L139-142
+ source of this code https://groups.google.com/d/topic/lufa-support/-w1pP0K3Elk/discussion

*/
void __bootloader_test(void)
        __attribute__ ((naked))
        __attribute__ ((section (".init0")));
void __bootloader_test(void)
{
        __asm volatile (" lds r24, bootloader_force\n"
                        " cpi r24, 0xbb\n"
                        " brne 1f\n"
                        " ldi r25, 0x00\n"
                        " sts bootloader_force, r25\n"
                        " ret\n"
                        "1:\n"
                        : : : "memory");
}


/** Main program entry point. This routine contains the overall program flow, including initial
* setup of all components and the main program loop.
*/
int main(void)
{

  SetupHardware();
  uint16_t bytesAvailable;

  CDC_Device_CreateStream(&tpad_CDC_Interface, &USBSerialStream);
  sei();

  for (;;)
  {

    //
    // Read pressures from 16 buttons
    //
    ButtonStates();

    //
    // Apply values to the buttons
    //
    updateLeds();

    if (connected && !wasConnected) {
      wasConnected = true;
      fputs("tpad\n", &USBSerialStream);
      fputs("{\"name\": \"tpad4-mega\", \"leds\":true, \"pads\" : 4, \"version\" : \"0.0.3\" }\n", &USBSerialStream);
    } else if (!connected && wasConnected) {

      // reset colors
      for (int i = 0; i<4; i++) {
        colors[i][0] = 0;
        colors[i][1] = 0;
        colors[i][2] = 0;
      }

      wasConnected = false;
      parserPos = 0;
    } else if ((bytesAvailable = CDC_Device_BytesReceived(&tpad_CDC_Interface))) {
      while (bytesAvailable--) {
        parseChunk(CDC_Device_ReceiveByte(&tpad_CDC_Interface));
      }
    }

    CDC_Device_USBTask(&tpad_CDC_Interface);
    USB_USBTask();
  }
}

/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{
  /* Disable watchdog if enabled by bootloader/fuses */
  MCUSR &= ~(1 << WDRF);
  wdt_disable();

  /* Disable clock division */
  clock_prescale_set(clock_div_1);
  USB_Init();

  //
  // Setup sensor power pins
  //
  DDRB = 0;

  led_setup();

  // Configure ADC
  ADC_Init(ADC_PRESCALE_32 | ADC_FREE_RUNNING);

  ADC_SetupChannel(0);
  ADC_SetupChannel(1);
  ADC_SetupChannel(4);
  ADC_SetupChannel(5);

  PORTB = 0xFF;
}

void ButtonStates(void) {
  char buffer[64];

  uint16_t result;
  if (!reading) {
    ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED | buttonChannels[button]);
    reading = true;
  } else if (ADC_IsReadingComplete()) {
    result = ADC_GetResult();

    if (result < 150) {
      result = 0;
    }

    if (results[button] != result) {


      if (!connected) {
        if (result < 1000) {
          colors[button][0] = 0;
          colors[button][1] = 0;
          colors[button][2] = 0;
        } else if (result < 8000) {
          colors[button][0] = 1023;
          colors[button][1] = 0;
          colors[button][2] = 0;
        } else if (result < 13000) {
          colors[button][0] = 0;
          colors[button][1] = 1023;
          colors[button][2] = 0;
        } else {
          colors[button][0] = 0;
          colors[button][1] = 0;
          colors[button][2] = 1023;
        }
      // ignore the bounces at the bottom of the pressure range
      } else {
        sprintf(buffer, "%d,%u\n", button, result);
        fputs(buffer, &USBSerialStream);
      }

      results[button] = result;
    }

    button++;
    if (button > 3) {
      button = 0;
    }
    reading = false;
  }
}

/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void) {
  bool ConfigSuccess = true;
  ConfigSuccess &= CDC_Device_ConfigureEndpoints(&tpad_CDC_Interface);
}

/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void) {
  CDC_Device_ProcessControlRequest(&tpad_CDC_Interface);
}

void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) {
  connected = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR);
}

Something went wrong with that request. Please try again.