コード例 #1
0
ファイル: main.c プロジェクト: jjd3592/I2C-bitbang
int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  init(); 
  UCA0CTL1 &= ~UCSWRST;
  accelinit();
  
  
  while(1)
  {
   readaccel();
   UCA0TXBUF=msbyte;
   while(UCA0STAT&0x01);
   for(i=0;i<200;i++);// delay for a little bit before reading again
  }


}
コード例 #2
0
ファイル: main.c プロジェクト: tcauchois/Syzycubes
int main(void)
{
  //LEDs
  uint16_t hue = 0, brightness = BRI_LIMIT - 1, lastBrightness = brightness;
  //motion 
  int16_t oldaccel[3], rawoldaccel[3], accel[3] = {0}, rawaccel[3] = {0};
  int32_t dotp[3];
  double flt_div, flt_acos;
  int8_t int_acos;
  //microphone
  uint32_t histPower[HIST_SIZE] = {0}, histPowerIndex = 0;
  uint32_t currentPower, avgPower = 0;
  double scale;
  //idle
  uint16_t idleIters = 0, idleActivity = 0;
  uint8_t idleMinutes = 0;

  // Initialize io ports
  ioinit();

  // Initialize serial
  serialinit();
  stdout = &mystdout;

  // print banner/battery check
  banner();

  // Initialize the timer
  timerinit();

  // Initialize audio capture
  audioinit();

  // Initialize accelerometer
  accelinit();

  // Enable interrupts
  sei();

  while(1)
  {
    // Wait until audio sample is ready
    while(!curAudioPowerReady) {}
    curAudioPowerReady = 0;

    // Grab the current audio power
    currentPower = curAudioPower;

    //Read accelerometer, determine hue
    oldaccel[0] = accel[0]; oldaccel[1] = accel[1]; oldaccel[2] = accel[2];
    rawoldaccel[0] = rawaccel[0]; rawoldaccel[1] = rawaccel[1]; rawoldaccel[2] = rawaccel[2];
    adxl345_getxyz(&rawaccel[0], &rawaccel[1], &rawaccel[2]);
    //(lowpass filter the accelerometer data)
    accel[0] = (rawoldaccel[0] + rawaccel[0]) / 2;
    accel[1] = (rawoldaccel[1] + rawaccel[1]) / 2;
    accel[2] = (rawoldaccel[2] + rawaccel[2]) / 2;
    //theta = acos(a.b / |a||b|)
    dotp[0] = accel[0] * oldaccel[0] + accel[1] * oldaccel[1] + accel[2] * oldaccel[2];
    dotp[1] = accel[0] * accel[0] + accel[1] * accel[1] + accel[2] * accel[2];
    dotp[2] = oldaccel[0] * oldaccel[0] + oldaccel[1] * oldaccel[1] + oldaccel[2] * oldaccel[2];
    if(dotp[1] == 0) dotp[1] = 1;
    if(dotp[2] == 0) dotp[2] = 1;
    flt_div = dotp[0] / (sqrt(dotp[1]) * sqrt(dotp[2]));
    flt_acos = acos(flt_div);
    int_acos = (int8_t)(flt_acos * 180 / M_PI);

    //the amount of motion varies pretty widely; it seems like the best
    //aesthetic results are from motion-triggering
    if(int_acos > 15)
    {
      hue += 15;
      idleActivity++;
    }
    if(hue >= HUE_LIMIT) hue -= HUE_LIMIT;

    //determine intensity (sliding scale)
    avgPower -= histPower[histPowerIndex];
    histPower[histPowerIndex] = currentPower;
    avgPower += histPower[histPowerIndex];
    histPowerIndex++;
    if(histPowerIndex >= HIST_SIZE) histPowerIndex = 0;

    //scale should vary between 0.5ish and 1.5ish... clamp to [0,1]
    scale = (float)(currentPower * HIST_SIZE) / avgPower;
    scale -= 0.5;
    if(scale > 1.0) scale = 1.0;
    if(scale < 0.0) scale = 0.0;
    //perception mapping: y=x^4 to give it a concave shape
    scale = scale*scale*scale*scale;
    brightness = BRI_MIN + (BRI_MAX-BRI_MIN) * scale;
    if(brightness < lastBrightness)
      brightness = (brightness + lastBrightness) / 2;
    lastBrightness = brightness;

    //Update LED colors
    send_hsb(hue, SATURATION, brightness);

    //Handle console
    dispatch_console();

    // Idle check
    idleIters++;
    if(idleIters >= ITERS_PER_MINUTE) {
      if(idleActivity >= IDLE_ACTIVITY_THRESHOLD) {
        // Activity!!!  Reset idle minutes
        idleMinutes = 0;
      } else {
        // Not enough activity, mark minute as idle
        idleMinutes++;
      }

      // Shutdown if idle for too long
      if(idleMinutes >= IDLE_MINUTES_UNTIL_SHUTDOWN) {
        shutdown(0);
      }

      // Reset iteration counts
      idleIters = 0;
      idleActivity = 0;
    }

    // Idle backstop check
    if(get_ticks() >= MINUTES_UNTIL_SHUTDOWN * 60) {
      shutdown(0);
    }
  }
}