Skip to content

Commit 1169ddf

Browse files
authored
Merge pull request #27 from openUC2/master_merge
Master merge
2 parents 7f1da27 + 9e09057 commit 1169ddf

71 files changed

Lines changed: 8318 additions & 154 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ESP32/main/control_DAC.ino

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
2+
#if defined(IS_DAC) || defined(IS_DAC_FAKE)
3+
4+
// Custom function accessible by the API
5+
void dac_act_fct() {
6+
// here you can do something
7+
8+
Serial.println("dac_act_fct");
9+
10+
// apply default parameters
11+
// DAC Channel
12+
dac_channel = DAC_CHANNEL_1;
13+
if (jsonDocument.containsKey("dac_channel")) {
14+
dac_channel = jsonDocument["dac_channel"];
15+
}
16+
17+
// DAC Frequency
18+
frequency = 1000;
19+
if (jsonDocument.containsKey("frequency")) {
20+
frequency = jsonDocument["frequency"];
21+
}
22+
23+
// DAC offset
24+
int offset = 0;
25+
if (jsonDocument.containsKey("offset")) {
26+
int offset = jsonDocument["offset"];
27+
}
28+
29+
// DAC amplitude
30+
int amplitude = 0;
31+
if (jsonDocument.containsKey("amplitude")) {
32+
int amplitude = jsonDocument["amplitude"];
33+
}
34+
35+
// DAC clk_div
36+
int clk_div = 0;
37+
if (jsonDocument.containsKey("clk_div")) {
38+
int clk_div = jsonDocument["clk_div"];
39+
}
40+
41+
if (jsonDocument["dac_channel"] == 1)
42+
dac_channel = DAC_CHANNEL_1;
43+
else if (jsonDocument["dac_channel"] == 2)
44+
dac_channel = DAC_CHANNEL_2;
45+
46+
//Scale output of a DAC channel using two bit pattern:
47+
if (amplitude == 0) scale = 0;
48+
else if (amplitude == 1) scale = 01;
49+
else if (amplitude == 2) scale = 10;
50+
else if (amplitude == 3) scale = 11;
51+
52+
53+
if (DEBUG) {
54+
Serial.print("dac_channel "); Serial.println(dac_channel);
55+
Serial.print("frequency "); Serial.println(frequency);
56+
Serial.print("offset "); Serial.println(offset);
57+
}
58+
59+
#ifdef IS_DAC
60+
61+
if (dac_is_running)
62+
if (frequency == 0) {
63+
dac_is_running = false;
64+
dac->Stop(dac_channel);
65+
dacWrite(dac_channel, offset);
66+
}
67+
else {
68+
dac->Stop(dac_channel);
69+
dac->Setup(dac_channel, clk_div, frequency, scale, phase, invert);
70+
dac_is_running = true;
71+
}
72+
else {
73+
dac->Setup(dac_channel, clk_div, frequency, scale, phase, invert);
74+
dac->dac_offset_set(dac_channel, offset);
75+
}
76+
#endif
77+
78+
jsonDocument.clear();
79+
jsonDocument["return"] = 1;
80+
}
81+
82+
void dac_set_fct() {
83+
// here you can set parameters
84+
int value = jsonDocument["value"];
85+
86+
if (DEBUG) {
87+
Serial.print("value "); Serial.println(value);
88+
}
89+
90+
int dac_set = jsonDocument["dac_set"];
91+
92+
if (dac_set != NULL) {
93+
if (DEBUG) Serial.print("dac_set "); Serial.println(dac_set);
94+
// SET SOMETHING
95+
}
96+
97+
jsonDocument.clear();
98+
jsonDocument["return"] = 1;
99+
100+
}
101+
102+
103+
104+
105+
106+
// Custom function accessible by the API
107+
void dac_get_fct() {
108+
// GET SOME PARAMETERS HERE
109+
int dac_variable = 12343;
110+
111+
jsonDocument.clear();
112+
jsonDocument["dac_variable"] = dac_variable;
113+
}
114+
115+
116+
/*
117+
wrapper for HTTP requests
118+
*/
119+
void dac_act_fct_http() {
120+
String body = server.arg("plain");
121+
deserializeJson(jsonDocument, body);
122+
dac_act_fct();
123+
serializeJson(jsonDocument, output);
124+
server.send(200, "application/json", output);
125+
}
126+
127+
// wrapper for HTTP requests
128+
void dac_get_fct_http() {
129+
String body = server.arg("plain");
130+
deserializeJson(jsonDocument, body);
131+
dac_get_fct();
132+
serializeJson(jsonDocument, output);
133+
server.send(200, "application/json", output);
134+
}
135+
136+
137+
// wrapper for HTTP requests
138+
void dac_set_fct_http() {
139+
String body = server.arg("plain");
140+
deserializeJson(jsonDocument, body);
141+
dac_set_fct();
142+
serializeJson(jsonDocument, output);
143+
server.send(200, "application/json", output);
144+
}
145+
146+
147+
148+
void drive_galvo(void * parameter){
149+
while(true){ // infinite loop
150+
digitalWrite(DAC_FAKE_PIN_1, HIGH);
151+
digitalWrite(DAC_FAKE_PIN_2, HIGH);
152+
vTaskDelay(frequency/portTICK_PERIOD_MS); // pause 1ms
153+
digitalWrite(DAC_FAKE_PIN_1, LOW);
154+
digitalWrite(DAC_FAKE_PIN_2, LOW);
155+
vTaskDelay(frequency/portTICK_PERIOD_MS); // pause 1ms
156+
}
157+
}
158+
#endif

ESP32/main/control_LEDarray.ino

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
2+
3+
4+
// Custom function accessible by the API
5+
void ledarr_act_fct() {
6+
7+
// here you can do something
8+
if (DEBUG) Serial.println("ledarr_act_fct");
9+
isBusy = true;
10+
11+
12+
const char* LEDArrMode = jsonDocument["LEDArrMode"]; // "array", "full", "full", "single", "off", "left", "right", "top", "bottom",
13+
14+
// individual pattern gets adressed
15+
// PYTHON: send_LEDMatrix_array(self, led_pattern, timeout=1)
16+
if (strcmp(LEDArrMode, "array") == 0) {
17+
if (DEBUG) Serial.println("pattern");
18+
for (int iled = 0; iled < LED_ARRAY_NUM; iled++) { //Iterate through results
19+
int red = jsonDocument["red"][iled]; //Implicit cast
20+
int green = jsonDocument["green"][iled]; //Implicit cast
21+
int blue = jsonDocument["blue"][iled]; //Implicit cast
22+
set_led_RGB(iled, red, green, blue);
23+
}
24+
}
25+
// only if a single led will be updated, all others stay the same
26+
// PYTHON: send_LEDMatrix_single(self, indexled=0, intensity=(255,255,255), timeout=1)
27+
else if (strcmp(LEDArrMode, "single") == 0) {
28+
if (DEBUG) Serial.println("single");
29+
int indexled = jsonDocument["indexled"];
30+
int red = jsonDocument["red"]; //Implicit cast
31+
int green = jsonDocument["green"]; //Implicit cast
32+
int blue = jsonDocument["blue"]; //Implicit cast
33+
//if (DEBUG) Serial.print(red); Serial.print(green); Serial.println(blue);
34+
set_led_RGB(indexled, red, green, blue);
35+
}
36+
// only few leds will be updated, all others stay the same
37+
// PYTHON: send_LEDMatrix_multi(self, indexled=(0), intensity=((255,255,255)), Nleds=8*8, timeout=1)
38+
else if (strcmp(LEDArrMode, "multi") == 0) {
39+
if (DEBUG) Serial.println("multi");
40+
int Nleds = jsonDocument["Nleds"];
41+
for (int i = 0; i < Nleds; i++) { //Iterate through results
42+
int indexled = jsonDocument["indexled"][i];
43+
int red = jsonDocument["red"][indexled]; //Implicit cast
44+
int green = jsonDocument["green"][indexled]; //Implicit cast
45+
int blue = jsonDocument["blue"][indexled]; //Implicit cast
46+
//if (DEBUG) Serial.print(red); Serial.print(green); Serial.println(blue);
47+
set_led_RGB(indexled, red, green, blue); }
48+
}
49+
// turn on all LEDs
50+
// PYTHON: send_LEDMatrix_full(self, intensity = (255,255,255),timeout=1)
51+
else if (strcmp(LEDArrMode, "full") == 0) {
52+
if (DEBUG) Serial.println("full");
53+
int red = jsonDocument["red"];
54+
int green = jsonDocument["green"];
55+
int blue = jsonDocument["blue"];
56+
set_all(red, green, blue);
57+
}
58+
// turn off all LEDs
59+
else if (strcmp(LEDArrMode, "left") == 0) {
60+
if (DEBUG) Serial.println("left");
61+
int red = jsonDocument["red"];
62+
int green = jsonDocument["green"];
63+
int blue = jsonDocument["blue"];
64+
int NLeds = jsonDocument["NLeds"];
65+
set_left(NLeds, red, green, blue);
66+
}
67+
// turn off all LEDs
68+
else if (strcmp(LEDArrMode, "right") == 0) {
69+
if (DEBUG) Serial.println("right");
70+
int red = jsonDocument["red"];
71+
int green = jsonDocument["green"];
72+
int blue = jsonDocument["blue"];
73+
int NLeds = jsonDocument["NLeds"];
74+
set_right(NLeds, red, green, blue);
75+
}
76+
// turn off all LEDs
77+
else if (strcmp(LEDArrMode, "top") == 0) {
78+
if (DEBUG) Serial.println("top");
79+
int red = jsonDocument["red"];
80+
int green = jsonDocument["green"];
81+
int blue = jsonDocument["blue"];
82+
int NLeds = jsonDocument["NLeds"];
83+
set_top(NLeds, red, green, blue);
84+
}
85+
// turn off all LEDs
86+
else if (strcmp(LEDArrMode, "bottom") == 0) {
87+
if (DEBUG) Serial.println("bottom");
88+
int red = jsonDocument["red"];
89+
int green = jsonDocument["green"];
90+
int blue = jsonDocument["blue"];
91+
int NLeds = jsonDocument["NLeds"];
92+
set_bottom(NLeds, red, green, blue);
93+
}
94+
jsonDocument.clear();
95+
jsonDocument["return"] = 1;
96+
jsonDocument["LEDArrMode"] = LEDArrMode;
97+
isBusy = false;
98+
99+
}
100+
101+
void ledarr_set_fct() {
102+
103+
Serial.println("Updating Hardware config of LED Array");
104+
105+
jsonDocument.clear();
106+
jsonDocument["return"] = 1;
107+
}
108+
109+
110+
111+
// Custom function accessible by the API
112+
void ledarr_get_fct() {
113+
jsonDocument.clear();
114+
jsonDocument["LED_ARRAY_PIN"] = LED_ARRAY_PIN;
115+
jsonDocument["LED_ARRAY_NUM"] = LED_ARRAY_NUM;
116+
117+
118+
}
119+
120+
121+
122+
/***************************************************************************************************/
123+
/******************************************* LED Array *******************************************/
124+
/***************************************************************************************************/
125+
/*******************************FROM OCTOPI ********************************************************/
126+
127+
void set_led_RGB(int iLed, int R, int G, int B) {
128+
matrix->setPixelColor(iLed, matrix->Color(R, G, B)); // Set pixel's color (in RAM)
129+
matrix->show(); // Update strip to match
130+
}
131+
132+
void setup_matrix() {
133+
// LED Matrix
134+
if(DEBUG) Serial.println("Setting up LED array");
135+
if(DEBUG) Serial.println("LED_ARRAY_PIN: " + String(LED_ARRAY_PIN));
136+
matrix = new Adafruit_NeoPixel(LED_ARRAY_NUM, LED_ARRAY_PIN, NEO_GRB + NEO_KHZ800);
137+
matrix->begin();
138+
matrix->setBrightness(255);
139+
set_all(0,0,0);
140+
delay(100);
141+
set_all(100,100,100);
142+
}
143+
144+
void set_all(int R, int G, int B)
145+
{
146+
for (int i = 0; i < (LED_ARRAY_NUM); i++) {
147+
set_led_RGB(i, R, G, B);
148+
}
149+
}
150+
151+
void set_left(int NLed, int R, int G, int B){
152+
if(NLed == NLED4x4){
153+
for (int i = 0; i < (NLED4x4); i++) {
154+
set_led_RGB(i, LED_PATTERN_DPC_LEFT_4x4[i]*R, LED_PATTERN_DPC_LEFT_4x4[i]*G, LED_PATTERN_DPC_LEFT_4x4[i]*B);
155+
}}
156+
if(NLed == NLED8x8){
157+
for (int i = 0; i < (NLED8x8); i++) {
158+
set_led_RGB(i, LED_PATTERN_DPC_LEFT_8x8[i]*R, LED_PATTERN_DPC_LEFT_8x8[i]*G, LED_PATTERN_DPC_LEFT_8x8[i]*B);
159+
}}
160+
}
161+
162+
void set_right(int NLed, int R, int G, int B){
163+
if(NLed == NLED4x4){
164+
for (int i = 0; i < (NLED4x4); i++) {
165+
set_led_RGB(i, (1-LED_PATTERN_DPC_LEFT_4x4[i])*R, (1-LED_PATTERN_DPC_LEFT_4x4[i])*G, (1-LED_PATTERN_DPC_LEFT_4x4[i])*B);
166+
}}
167+
if(NLed == NLED8x8){
168+
for (int i = 0; i < (NLED8x8); i++) {
169+
set_led_RGB(i, (1-LED_PATTERN_DPC_LEFT_8x8[i])*R, (1-LED_PATTERN_DPC_LEFT_8x8[i])*G, (1-LED_PATTERN_DPC_LEFT_8x8[i])*B);
170+
}}
171+
}
172+
173+
void set_top(int NLed, int R, int G, int B){
174+
if(NLed == NLED4x4){
175+
for (int i = 0; i < (NLED4x4); i++) {
176+
set_led_RGB(i, (LED_PATTERN_DPC_TOP_4x4[i])*R, (LED_PATTERN_DPC_TOP_4x4[i])*G, (LED_PATTERN_DPC_TOP_4x4[i])*B);
177+
}}
178+
if(NLed == NLED8x8){
179+
for (int i = 0; i < (NLED8x8); i++) {
180+
set_led_RGB(i, (LED_PATTERN_DPC_TOP_8x8[i])*R, (LED_PATTERN_DPC_TOP_8x8[i])*G, (LED_PATTERN_DPC_TOP_8x8[i])*B);
181+
}}
182+
}
183+
184+
void set_bottom(int NLed, int R, int G, int B){
185+
if(NLed == NLED4x4){
186+
for (int i = 0; i < (NLED4x4); i++) {
187+
set_led_RGB(i, (1-LED_PATTERN_DPC_TOP_4x4[i])*R, (1-LED_PATTERN_DPC_TOP_4x4[i])*G, (1-LED_PATTERN_DPC_TOP_4x4[i])*B);
188+
}}
189+
if(NLed == NLED8x8){
190+
for (int i = 0; i < (NLED8x8); i++) {
191+
set_led_RGB(i, (1-LED_PATTERN_DPC_TOP_8x8[i])*R, (1-LED_PATTERN_DPC_TOP_8x8[i])*G, (1-LED_PATTERN_DPC_TOP_8x8[i])*B);
192+
}}
193+
}
194+
195+
void set_center(int R, int G, int B)
196+
{
197+
/*
198+
matrix->fillScreen(matrix->Color(0, 0, 0));
199+
matrix->drawPixel(4, 4, matrix->Color(R, G, B));
200+
matrix->show();
201+
*/
202+
}
203+
204+
/*
205+
wrapper for HTTP requests
206+
*/
207+
void ledarr_act_fct_http() {
208+
String body = server.arg("plain");
209+
deserializeJson(jsonDocument, body);
210+
ledarr_act_fct();
211+
serializeJson(jsonDocument, output);
212+
server.send(200, "application/json", output);
213+
}
214+
215+
// wrapper for HTTP requests
216+
void ledarr_get_fct_http() {
217+
String body = server.arg("plain");
218+
deserializeJson(jsonDocument, body);
219+
ledarr_get_fct();
220+
serializeJson(jsonDocument, output);
221+
server.send(200, "application/json", output);
222+
}
223+
224+
// wrapper for HTTP requests
225+
void ledarr_set_fct_http() {
226+
String body = server.arg("plain");
227+
deserializeJson(jsonDocument, body);
228+
ledarr_set_fct();
229+
serializeJson(jsonDocument, output);
230+
server.send(200, "application/json", output);
231+
}

0 commit comments

Comments
 (0)