robert@269: /* robert@269: ____ _____ _ _ robert@269: | __ )| ____| | / \ robert@269: | _ \| _| | | / _ \ robert@269: | |_) | |___| |___ / ___ \ robert@269: |____/|_____|_____/_/ \_\.io robert@269: robert@269: */ robert@269: giuliomoro@246: /* giuliomoro@77: * giuliomoro@77: * Andrew McPherson and Victor Zappi giuliomoro@77: * Queen Mary, University of London giuliomoro@77: */ giuliomoro@77: robert@269: /** robert@269: \example 2_digital_button robert@269: robert@269: Switching an LED on and off robert@269: --------------------------- robert@269: robert@285: This example brings together digital input and digital output. The program will read robert@269: a button and turn the LED on and off according to the state of the button. robert@269: robert@269: - connect an LED in series with a 470ohm resistor between P8_07 and ground. robert@269: - connect a 1k resistor to P9_03 (+3.3V), robert@269: - connect the other end of the resistor to both a button and P8_08 robert@269: - connect the other end of the button to ground. robert@269: robert@269: You will notice that the LED will normally stay on and will turn off as long as robert@269: the button is pressed. This is due to the fact that the LED is set to the same robert@269: value read at input P8_08. When the button is not pressed, P8_08 is `HIGH` and so robert@269: P8_07 is set to `HIGH` as well, so that the LED conducts and emits light. When robert@269: the button is pressed, P8_08 goes `LOW` and P8_07 is set to `LOW`, turning off the LED. robert@269: robert@269: As an exercise try and change the code so that the LED only turns on when robert@285: the button is pressed. robert@269: */ robert@269: robert@269: giuliomoro@77: #include giuliomoro@77: #include giuliomoro@77: #include giuliomoro@77: #include giuliomoro@246: #include giuliomoro@77: giuliomoro@77: // setup() is called once before the audio rendering starts. giuliomoro@77: // Use it to perform any initialisation and allocation which is dependent giuliomoro@77: // on the period size or sample rate. giuliomoro@77: // giuliomoro@77: // userData holds an opaque pointer to a data structure that was passed giuliomoro@77: // in from the call to initAudio(). giuliomoro@77: // giuliomoro@77: // Return true on success; returning false halts the program. giuliomoro@77: giuliomoro@77: bool setup(BeagleRTContext *context, void *userData) giuliomoro@77: { giuliomoro@246: pinModeFrame(context, 0, P8_08, INPUT); giuliomoro@246: pinModeFrame(context, 0, P8_07, OUTPUT); giuliomoro@77: return true; giuliomoro@77: } giuliomoro@77: giuliomoro@77: // render() is called regularly at the highest priority by the audio engine. giuliomoro@77: // Input and output are given from the audio hardware and the other giuliomoro@77: // ADCs and DACs (if available). If only audio is available, numAnalogFrames giuliomoro@77: // will be 0. giuliomoro@77: giuliomoro@77: /* basic_button giuliomoro@77: * - connect an LED in series with a 470ohm resistor between P8_07 and ground. giuliomoro@77: * - connect a 1k resistor to P9_03 (+3.3V), giuliomoro@77: * - connect the other end of the resistor to both a button and P8_08 giuliomoro@77: * - connect the other end of the button to ground. giuliomoro@77: * The program will read the button and make the LED blink when the button is pressed. giuliomoro@77: */ giuliomoro@77: giuliomoro@77: void render(BeagleRTContext *context, void *userData) giuliomoro@77: { giuliomoro@77: for(unsigned int n=0; ndigitalFrames; n++){ giuliomoro@223: int status=digitalReadFrame(context, 0, P8_08); //read the value of the button giuliomoro@246: digitalWriteFrameOnce(context, n, P8_07, status); //write the status to the LED giuliomoro@246: float out = 0.1 * status * rand() / (float)RAND_MAX * 2 - 1; //generate some noise, gated by the button giuliomoro@246: for(unsigned int j = 0; j < context->audioChannels; j++){ giuliomoro@246: audioWriteFrame(context, n, j, out); //write the audio output giuliomoro@246: } giuliomoro@223: } giuliomoro@77: } giuliomoro@77: giuliomoro@77: // cleanup() is called once at the end, after the audio has stopped. giuliomoro@77: // Release any resources that were allocated in setup(). giuliomoro@77: giuliomoro@77: void cleanup(BeagleRTContext *context, void *userData) giuliomoro@77: { giuliomoro@77: // Nothing to do here giuliomoro@77: } giuliomoro@246: