andrewm@0
|
1 /*
|
andrewm@0
|
2 * Utilities.h
|
andrewm@0
|
3 *
|
andrewm@0
|
4 * Created on: Oct 27, 2014
|
andrewm@0
|
5 * Author: parallels
|
andrewm@0
|
6 */
|
andrewm@0
|
7
|
andrewm@0
|
8 #ifndef UTILITIES_H_
|
andrewm@0
|
9 #define UTILITIES_H_
|
andrewm@0
|
10
|
andrewm@45
|
11 #include "BeagleRT.h"
|
andrewm@45
|
12
|
andrewm@45
|
13 #define setBit(word,bit) ((word) | (1 << (bit)))
|
andrewm@45
|
14 #define clearBit(word,bit) ((word) &~ (1 << (bit)))
|
andrewm@45
|
15 #define getBit(word,bit) (((word) >> (bit)) & 1)
|
andrewm@45
|
16 #define changeBit(word,bit,value) ((clearBit((word),(bit))) | ((value) << (bit)))
|
andrewm@45
|
17
|
andrewm@45
|
18 #if 1
|
andrewm@45
|
19 // Note: pinMode(), analogWrite() and digitalWrite() should be able to be called from initialise_render()
|
andrewm@45
|
20 // Likewise, thread launch should be able to be called from initialise_render()
|
andrewm@45
|
21 // Also, make volume change functions callable from render() thread -- as an aux task?
|
andrewm@45
|
22
|
andrewm@45
|
23 float analogReadFrame(BeagleRTContext *context, int frame, int channel);
|
andrewm@45
|
24 void analogWriteFrame(BeagleRTContext *context, int frame, int channel, float value);
|
andrewm@45
|
25 void analogWriteFrameOnce(BeagleRTContext *context, int frame, int channel, float value);
|
andrewm@45
|
26
|
andrewm@45
|
27 int digitalReadFrame(BeagleRTContext *context, int frame, int channel);
|
andrewm@45
|
28 void digitalWriteFrame(BeagleRTContext *context, int frame, int channel, int value);
|
andrewm@45
|
29 void digitalWriteFrameOnce(BeagleRTContext *context, int frame, int channel, int value);
|
andrewm@45
|
30
|
andrewm@45
|
31 void pinModeFrame(BeagleRTContext *context, int frame, int channel, int mode);
|
andrewm@45
|
32 void pinModeFrameOnce(BeagleRTContext *context, int frame, int channel, int mode);
|
andrewm@45
|
33
|
andrewm@45
|
34 #else
|
andrewm@13
|
35
|
giuliomoro@19
|
36 // Macros for accessing the analog values: usable _only_ within render()
|
andrewm@5
|
37
|
giuliomoro@19
|
38 // Read an Analog input from input pin p at frame f
|
giuliomoro@23
|
39 #define analogRead(p, f) (analogIn[(f)*gNumAnalogChannels + (p)])
|
giuliomoro@19
|
40 // Write an Analog output frame at output pin p, frame f, to value v
|
giuliomoro@23
|
41 #define analogWriteFrame(p, f, v) (analogOut[(f)*gNumAnalogChannels + (p)] = (v))
|
giuliomoro@23
|
42 #define analogWrite(pin, frame, value) \
|
giuliomoro@18
|
43 (({do {\
|
giuliomoro@19
|
44 for (int _privateI=(frame); _privateI<numAnalogFrames; _privateI++){ \
|
giuliomoro@23
|
45 analogWriteFrame(pin,_privateI,value); \
|
giuliomoro@18
|
46 }\
|
giuliomoro@18
|
47 } while (0);}),(void)0)\
|
andrewm@5
|
48
|
andrewm@45
|
49
|
giuliomoro@19
|
50 //digital API:
|
giuliomoro@33
|
51 #define setDigitalDirectionFrame(pin,frame,direction) digital[(frame)]=changeBit(digital[(frame)],(pin),(direction)),void(0)
|
giuliomoro@33
|
52 #define setDigitalDirection(pin,frame,direction)\
|
giuliomoro@33
|
53 (({do {\
|
giuliomoro@33
|
54 for(int _privateI=(frame); _privateI<numDigitalFrames; _privateI++)\
|
giuliomoro@33
|
55 setDigitalDirectionFrame(pin,_privateI,direction);\
|
giuliomoro@33
|
56 } while (0);}), (void)0)
|
giuliomoro@19
|
57 #define digitalWriteAll(frame,value) digital[(frame)]=0xffff0000*(!(!value));
|
giuliomoro@16
|
58 //sets the bit in the high word, clears the bit in the low word (just in case the direction was not previously set)
|
giuliomoro@19
|
59 #define digitalWriteFrame(pin, frame, value) digital[(frame)]=( changeBit(digital[(frame)], (pin+16), (value)) & (0xffffffff-(1<<(pin))) ) //could have been done with two subsequent assignments
|
giuliomoro@18
|
60 #define digitalWrite(pin, frame, value) \
|
giuliomoro@18
|
61 (({do {\
|
giuliomoro@33
|
62 for (int _privateI=(frame); _privateI<numDigitalFrames; _privateI++) \
|
giuliomoro@18
|
63 digitalWriteFrame(pin,_privateI,value); \
|
giuliomoro@18
|
64 } while (0);}),(void)0)\
|
giuliomoro@18
|
65
|
giuliomoro@19
|
66 #define digitalRead(pin, frame) ( getBit(digital[(frame)], pin+16) )
|
giuliomoro@16
|
67
|
andrewm@45
|
68 #endif
|
andrewm@45
|
69
|
andrewm@0
|
70 float map(float x, float in_min, float in_max, float out_min, float out_max);
|
andrewm@0
|
71 float constrain(float x, float min_val, float max_val);
|
andrewm@0
|
72
|
andrewm@0
|
73 #endif /* UTILITIES_H_ */
|