Exemplo n.º 1
0
/*
/ lightSetting represents a set of bits corresponding to the lights that are on//off
/ Ex: 0b1001 - {on, off, off, on}
*/
void LightController::setLights(int lightSetting){
  for(int i = 0; i < numLights; i++){
    if(readLight(lightSetting, i)){
      digitalWrite(lights[i], HIGH);
    } else {
      digitalWrite(lights[i], LOW);
    }   
  } 
}
Exemplo n.º 2
0
/*=============<< main >>==============*
 * Get that party started!             *
 * Initialize for rendering and audio. *
 *=====================================*/
int main(int argc, char* argv[]) {
  // Audio vars
  SDL_AudioSpec want, have;
  SDL_AudioDeviceID dev;
  wavedata my_wavedata;
  
  // Rendering vars
  SDL_Window *window;
  SDL_Renderer *renderer;
  SDL_Event event;

  // Text vars
  TTF_Font* font;
  SDL_Surface *surfaceMessage;
  SDL_Texture *message;
  SDL_Rect message_rect;

  SDL_Surface *noteMessage;
  SDL_Texture *nmessage;
  SDL_Rect nmessage_rect;
  
  // Keycode for key presses
  SDL_Keycode key;

  /*******<Initial Settings>*******/

  // Initialize with appropriate flags
  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0 ||
      TTF_Init() < 0)
    return 1;
  atexit(SDL_Quit); // Set exit function s.t. SDL resources deallocated on quit



  /* ======<< AUDIO SETTINGS >>======= */
  SDL_memset(&want, 0, sizeof(want));
  createWant(&want, &my_wavedata);    // Call function to initialize vals
  dev = SDL_OpenAudioDevice(NULL, 0, &want, &have,
                            SDL_AUDIO_ALLOW_FORMAT_CHANGE);
  if (dev == 0) 
    printf("Error opening audio device: %s\n", SDL_GetError());



  /* ======<< RENDERING SETTINGS >====== */

  // Create window and renderer
  window = SDL_CreateWindow("SDL_RenderClear",
      SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 512, 512, 0);
  renderer = SDL_CreateRenderer(window, -1, 0);

  /* Text */

  // Opens font
  //font = TTF_OpenFont("/Library/Fonts/Arial.ttf", 12);
  font = TTF_OpenFont("/usr/share/fonts/Droid/Roboto_Regular.ttf");
  if(font == NULL) {
    printf("Font not found\n");
    return 1;
  }
  SDL_Color normalFontColor = {50, 170, 255};   // Darker blue
  SDL_Color cbFontColor = {80, 100, 80};        // Weird green
  SDL_Color fontColor = normalFontColor;
  

  /*********< Okay, game time! >***********/
  while (!quit) {

    //get current light level
    updateWavedata(&my_wavedata, readLight());
    /* ==========<< Poll for events >>============ */
    while (SDL_PollEvent(&event)) {
      switch (event.type) {

        /* Key pressed */
        case SDL_KEYDOWN:
          key = event.key.keysym.sym;

          /* Quit */
          if (key == SDLK_ESCAPE || key == SDLK_q) {
            quit = 1;
          }
          /* Raise pitch by one note */
          else if (key == SDLK_UP && my_wavedata.pitchindex < 7) {
            updateWavedata(&my_wavedata, my_wavedata.pitchindex+1);
            printf("%d\n", my_wavedata.pitchindex);
          }
          /* Lower pitch by one note */
          else if (key == SDLK_DOWN && my_wavedata.pitchindex > 0) {
            updateWavedata(&my_wavedata, my_wavedata.pitchindex-1);
            printf("%d\n", my_wavedata.pitchindex);
          }
          /* Change to colorblind mode */
          else if (key == SDLK_BACKSPACE) {
            colorblind = (colorblind+1)%2;
          }
          /* Change instruments */
          else if (key == SDLK_i) {
            instr = (instr == PIANO) ? GUITAR : PIANO;
            updateWavedata(&my_wavedata, my_wavedata.pitchindex);
          }
          /* Mute */
          else if (key == SDLK_m) {
            mute = (mute+1)%2;
          }
          break;
        /* Exit */
        case SDL_QUIT:
          quit = 1;
          break;
        default:
          break;
      }
    }

    /* ========<< Text >>======== */

    // Set font color
    fontColor = normalFontColor;
    if (colorblind) {
      fontColor = cbFontColor;
    }
    
    // Create surface and convert it to texture
    surfaceMessage =
      TTF_RenderText_Solid(font, "Theremin Hero!", fontColor);
    if (colorblind) {
      surfaceMessage =
        TTF_RenderText_Solid(font, "Colorblind Mode ;D", fontColor);
    }
    message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);

    // {xPos, yPos, width, height}
    message_rect.x = 150;
    message_rect.y = 200;
    message_rect.w = 200;
    message_rect.h = 80;

    /* Shows note on screen */
    noteMessage =
      TTF_RenderText_Solid(font, pitchNames[my_wavedata.pitchindex], fontColor);
    nmessage = SDL_CreateTextureFromSurface(renderer, noteMessage);

    nmessage_rect.x = 210;
    nmessage_rect.y = 350;
    nmessage_rect.w = 100;
    nmessage_rect.h = 50;

    /* ========<< Background >>========= */

    // Choose background color
    SDL_SetRenderDrawColor(renderer, 170, 200, 215, 255);   // Light blue
    if (colorblind) {
      SDL_SetRenderDrawColor(renderer, 90, 60, 80, 255);    // Dark magenta
    }

    // Set background color
    SDL_RenderClear(renderer);

    SDL_SetRenderDrawColor(renderer, 200, 255, 0, 255);     // Green
    SDL_RenderDrawLine(renderer, 5, 5, 340, 340);     // Awkward diagonal line

    // Render message texture
    SDL_RenderCopy(renderer, message, NULL, &message_rect);
    SDL_RenderCopy(renderer, nmessage, NULL, &nmessage_rect);

    // Move to foreground
    SDL_RenderPresent(renderer);

    /* =========<< Audio >>========== */
    SDL_PauseAudioDevice(dev, mute);
  }

  // CLEAN YO' ROOM (Cleanup)
  TTF_CloseFont(font);
  SDL_CloseAudioDevice(dev);
  SDL_Quit();

  return 0;
}
Exemplo n.º 3
0
application_event_result application_event(application_request* request, buffer_read_t* read_buffer, buffer_write_t* write_buffer) {
  switch(request->queryId) {
      case 1: {
          //  <query name="light_write.json" description="Turn light on and off" id="1">
          //    <request>
          //      <parameter name="light_id" type="uint8"/>
          //      <parameter name="light_on" type="uint8"/>
          //    </request>
          //    <response>
          //      <parameter name="light_state" type="uint8"/>
          //    </response>
          //  </query>

          uint8_t light_id;
          uint8_t light_on;
          uint8_t light_state;

          // Read parameters in request
          if (!buffer_read_uint8(read_buffer, &light_id)) {
              NABTO_LOG_ERROR(("Can't read light_id, the buffer is too small"));
              return AER_REQ_TOO_SMALL;
          }
          if (!buffer_read_uint8(read_buffer, &light_on)) {
              NABTO_LOG_ERROR(("Can't read light_state, the buffer is too small"));
              return AER_REQ_TOO_SMALL;
          }

          // Set light according to request
          light_state = setLight(light_id, light_on);

          // Write back led state
          if (!buffer_write_uint8(write_buffer, light_state)) {
              return AER_REQ_RSP_TOO_LARGE;
          }

          return AER_REQ_RESPONSE_READY;
      }
      case 2: {
          //  <query name="light_read.json" description="Read light status" id="2">
          //    <request>
          //      <parameter name="light_id" type="uint8"/>
          //    </request>
          //    <response>
          //      <parameter name="light_state" type="uint8"/>
          //    </response>
          //  </query>

          uint8_t light_id;
          uint8_t light_state;

          // Read parameters in request
          if (!buffer_read_uint8(read_buffer, &light_id)) return AER_REQ_TOO_SMALL;

          // Read light state
          light_state = readLight(light_id);

          // Write back led state
          if (!buffer_write_uint8(write_buffer, light_state)) return AER_REQ_RSP_TOO_LARGE;

          return AER_REQ_RESPONSE_READY;
      }
  }
  return AER_REQ_INV_QUERY_ID;
}
Exemplo n.º 4
0
int main(int argc, char** argv) {
    printf("i2c c test.\n");
    int fd[2];
    unsigned char buf[10];
    char *fileName = "/dev/i2c-1";
    int addr[2] = {0x29, 0x39}; // Adressen der Chips

    openPort(&fd[0]);
    openPort(&fd[1]);
    addSlave(addr[0], &fd[0]);
    addSlave(addr[1], &fd[1]);

    int i;

    int readcount[2] = {0, 1};
    double maxlux = 0.0;
    int maxCh0 = 0;


    //Variablen zur Berechnung des Lichteinfalls
    int cLow[2] = {65536, 65536};
    int cHigh[2] = {0, 0};
    double cc[2] = {0.0, 0.0};
    double maxcc[2] = {0.0, 0.0};
    double lastcc[2] = {0.0, 0.0};
    double epsilon = 0.01;

    //Variablen zur Aktivierung der Dosen:

    int active[2] = {0, 0};
    double actThr = 0.1; //Schwellenwert um Verdunklung zu erkennen

    //initialisiere OSC:

    lo_address oscaddr = initOSC("192.168.1.8", "7777");

    while(1) {
        for(i = 0; i < 2; i++) {
            cc[i] = readLight(&fd[i], &cLow[i], &cHigh[i]);
            readcount[i]++;
            if(cc[i] > maxcc[i]) {
                maxcc[i] = cc[i];
            }
            if(readcount[i] >= 3) {
                if(active[i]) {
                    if((lastcc[i] > maxcc[i] + epsilon) || (lastcc[i] < maxcc[i] - epsilon)) {
                        //Lichtintensitaet hat sich signifikant geaendert -> Ausgabe!
                        printf("%d Licht: %f\n", i, maxcc[i]);
                        if(i == 0) sendOSC(&oscaddr, "/licht0", maxcc[i]);
                        else if(i == 1) sendOSC(&oscaddr, "/licht1", maxcc[i]);
                        lastcc[i] = maxcc[i];
                    }
                }
                else {
                    if(maxcc[i] > (1.0 - actThr)) {
                        printf("Dose %d ist jetzt aktiv.\n", i);
                        active[i] = 1;
                    }
                }
                readcount[i] = 0;
                maxcc[i] = 0.0;
            }
        }

        /* lo_address t = lo_address_new("192.168.1.8", "7777");

        if(lo_send(t, "/light", "f", cc) == -1) {
        printf("OSC Fehler: %s\n", lo_address_errstr(t));
        }*/
        
        usleep(13 * 1000);

    }
    return 0;
}