comparison projects/7segment/render.cpp @ 268:8d80eda512cd prerelease

Added new overlay for using PRU0 or PRU1, a script to halt board on button press, and several example projects
author andrewm
date Tue, 17 May 2016 14:46:26 +0100
parents
children
comparison
equal deleted inserted replaced
267:247a182adb6d 268:8d80eda512cd
1 /*
2 * render.cpp
3 *
4 * Created on: Oct 24, 2014
5 * Author: parallels
6 */
7
8
9 #include <BeagleRT.h>
10 #include <Utilities.h>
11
12 #define NUM_PINS 12
13
14 // Breadboard wiring layout:
15 // 11 10 12 9 8 7
16 // [ LED DISP ]
17 // 1 2 3 6 4 5
18
19 // Organised by display segments:
20 // e d . X c g b X X X f a
21 const int kPins[NUM_PINS] = {P8_07, P8_08, P8_09, P8_10, P8_11, P8_12,
22 P8_15, P8_16, P8_27, P8_28, P8_29, P8_30};
23
24 // Indices into the above array: pins 12, 9, 8, 6
25 const int kDigits[4] = {9, 8, 7, 3};
26
27 int gCurrentlyDisplayingDigit = 0;
28 int gDigitDisplayTime = 0;
29 const int kDigitMaxDisplayTime = 44;
30
31 int gState = 0;
32 int gStateCounter = 0;
33 const int kMaxState = 25;
34
35 // . g f e d c b a
36 //const unsigned char kBELA[4] = {0x7C, 0x79, 0x38, 0x77};
37 const unsigned char kBELA[4] = {0x7C, 0x7B, 0x38, 0x5F};
38 const unsigned char kPerimeter[6] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20};
39
40 int gCharacterToDisplay[4] = {0, 0, 0, 0};
41
42 // setup() is called once before the audio rendering starts.
43 // Use it to perform any initialisation and allocation which is dependent
44 // on the period size or sample rate.
45 //
46 // userData holds an opaque pointer to a data structure that was passed
47 // in from the call to initAudio().
48 //
49 // Return true on success; returning false halts the program.
50
51 bool setup(BeagleRTContext *context, void *userData)
52 {
53 // This project makes the assumption that the audio and digital
54 // sample rates are the same. But check it to be sure!
55 if(context->audioFrames != context->digitalFrames) {
56 rt_printf("Error: this project needs the audio and digital sample rates to be the same.\n");
57 return false;
58 }
59
60 for(int i = 0; i < NUM_PINS; i++) {
61 pinModeFrame(context, 0, kPins[i], OUTPUT);
62 }
63
64 return true;
65 }
66
67 // render() is called regularly at the highest priority by the audio engine.
68 // Input and output are given from the audio hardware and the other
69 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
70 // will be 0.
71
72 void render(BeagleRTContext *context, void *userData)
73 {
74 for(unsigned int n = 0; n < context->audioFrames; n++) {
75 // Check for rotation between digits
76 if(--gDigitDisplayTime <= 0) {
77 gCurrentlyDisplayingDigit = (gCurrentlyDisplayingDigit + 1) % 4;
78 gDigitDisplayTime = kDigitMaxDisplayTime;
79 }
80
81 // Write the currently displaying digit low and the rest high
82 for(int i = 0; i < 4; i++)
83 digitalWriteFrameOnce(context, n, kPins[kDigits[i]], HIGH);
84 digitalWriteFrameOnce(context, n, kPins[kDigits[gCurrentlyDisplayingDigit]], LOW);
85
86 // Write the digit to the other outputs
87 digitalWriteFrameOnce(context, n, kPins[11],
88 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x01); // a
89 digitalWriteFrameOnce(context, n, kPins[6],
90 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x02); // b
91 digitalWriteFrameOnce(context, n, kPins[4],
92 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x04); // c
93 digitalWriteFrameOnce(context, n, kPins[1],
94 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x08); // d
95 digitalWriteFrameOnce(context, n, kPins[0],
96 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x10); // e
97 digitalWriteFrameOnce(context, n, kPins[10],
98 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x20); // f
99 digitalWriteFrameOnce(context, n, kPins[5],
100 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x40); // g
101 digitalWriteFrameOnce(context, n, kPins[2],
102 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x80); // .
103
104 // Check for changing state
105 if(--gStateCounter <= 0) {
106 gState = (gState + 1) % kMaxState;
107 if(gState != (kMaxState - 1)) {
108 for(int i = 0; i < 4; i++)
109 gCharacterToDisplay[i] = 1 << (gState % 6);
110 gStateCounter = 2000;
111 }
112 else {
113 for(int i = 0; i < 4; i++)
114 gCharacterToDisplay[i] = kBELA[i];
115 gStateCounter = 50000;
116 }
117 }
118 }
119 }
120
121 // cleanup() is called once at the end, after the audio has stopped.
122 // Release any resources that were allocated in setup().
123
124 void cleanup(BeagleRTContext *context, void *userData)
125 {
126
127 }