andrewm@66: /** andrewm@66: * @file andrewm@66: * @brief Wiring-inspired utility functions and macros andrewm@0: * andrewm@66: * Macros and functions for I/O and data processing taking after the Wiring andrewm@66: * (Arduino) language. This code began as part of the Hackable Instruments andrewm@66: * project (EPSRC) at Queen Mary University of London, 2013-14. andrewm@66: * andrewm@66: * (c) 2014-15 Andrew McPherson, Victor Zappi and Giulio Moro, andrewm@66: * Queen Mary University of London andrewm@0: */ andrewm@0: andrewm@0: #ifndef UTILITIES_H_ andrewm@0: #define UTILITIES_H_ andrewm@0: andrewm@45: #include "BeagleRT.h" andrewm@45: andrewm@66: /// Set the given bit in \c word to 1. andrewm@45: #define setBit(word,bit) ((word) | (1 << (bit))) andrewm@66: andrewm@66: /// Clear the given bit in \c word to 0. andrewm@45: #define clearBit(word,bit) ((word) &~ (1 << (bit))) andrewm@66: andrewm@66: /// Check if the given bit in \c word is 1 (returns nonzero) or 0 (returns zero). andrewm@45: #define getBit(word,bit) (((word) >> (bit)) & 1) andrewm@66: andrewm@66: /// Set/clear the given bit in \c word to \c value. andrewm@45: #define changeBit(word,bit,value) ((clearBit((word),(bit))) | ((value) << (bit))) andrewm@45: andrewm@45: #if 1 andrewm@56: // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from setup() andrewm@56: // Likewise, thread launch should be able to be called from setup() andrewm@45: // Also, make volume change functions callable from render() thread -- as an aux task? andrewm@45: andrewm@45: float analogReadFrame(BeagleRTContext *context, int frame, int channel); andrewm@45: void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value); andrewm@45: void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value); andrewm@45: andrewm@45: int digitalReadFrame(BeagleRTContext *context, int frame, int channel); andrewm@45: void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value); andrewm@45: void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value); andrewm@45: andrewm@45: void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode); andrewm@45: void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode); andrewm@45: andrewm@45: #else andrewm@13: giuliomoro@19: // Macros for accessing the analog values: usable _only_ within render() andrewm@5: giuliomoro@19: // Read an Analog input from input pin p at frame f giuliomoro@23: #define analogRead(p, f) (analogIn[(f)*gNumAnalogChannels + (p)]) giuliomoro@19: // Write an Analog output frame at output pin p, frame f, to value v giuliomoro@23: #define analogWriteFrame(p, f, v) (analogOut[(f)*gNumAnalogChannels + (p)] = (v)) giuliomoro@23: #define analogWrite(pin, frame, value) \ giuliomoro@18: (({do {\ giuliomoro@19: for (int _privateI=(frame); _privateI