Skip to content

Commit 265460f

Browse files
committed
extras
1 parent ac6ba5d commit 265460f

File tree

9 files changed

+774
-0
lines changed

9 files changed

+774
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Firmware for functions generator
2+
3+
You need `Arduino Renesas Core`.
4+
5+
Select `Science Kit R3 Audio Module`.
6+
7+
Use USB-C to upload
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
This file is part of the Arduino_GroveI2C_Ultrasonic library.
3+
Copyright (c) 2023 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "Wire.h"
21+
#include "pin_def.h"
22+
#include "led_gauge.h"
23+
#include "led_range.h"
24+
#include "analogWave.h"
25+
26+
#define RES 100
27+
28+
// Firmware version
29+
uint8_t version[2]={0,6};
30+
31+
char command=0;
32+
byte data[6];
33+
34+
uint16_t wave[RES];
35+
36+
LedGauge frequency_1(FQ_1, 10, 10, ROW_A_1, ROW_A_2, ROW_A_3, COL_A_1, COL_A_2, COL_A_3);
37+
LedGauge frequency_2(FQ_2, 10, 10, ROW_B_1, ROW_B_2, ROW_B_3, COL_B_1, COL_B_2, COL_B_3, true);
38+
39+
LedRange range(FQ_R, ROW_C_1, ROW_C_2, ROW_C_3, COL_C_1, COL_C_2);
40+
41+
analogWave wave1(DAC,wave,RES,0);
42+
analogWave wave2(DAC2,wave,RES,0);
43+
44+
int previous_frequency_1 = 10;
45+
int previous_frequency_2 = 10;
46+
int freq1 = 10;
47+
int freq2 = 10;
48+
49+
int previous_phase_1 = 0;
50+
int previous_phase_2 = 0;
51+
52+
int phase1 = 0;
53+
int phase2 = 0;
54+
55+
const float step = ADC_RES/float(RES);
56+
const float phase_to_deg = 180/float(RES);
57+
58+
bool test_flag = false;
59+
unsigned long time_rephase;
60+
int rephase;
61+
62+
63+
64+
// This function loads an wave array with a sinewave using RES samples.
65+
66+
void generate_wave(){
67+
float s = (2 * 3.1415) / RES;
68+
for (auto i = 0; i < RES; ++i) {
69+
float sample = (1 + sin((float)i * s)) * (float)65535 / 2.0;
70+
wave[i] = (uint16_t)sample;
71+
}
72+
}
73+
74+
75+
void comm_event(int event){
76+
command=Wire.read();
77+
}
78+
79+
void comm_request(){
80+
switch(command){
81+
case 'V': // version, high rev, low rev
82+
Wire.write(version,2);
83+
break;
84+
case 'D':
85+
data[0]=frequency_1.getGaugeValue();
86+
data[1]=range.getRange1();
87+
data[2]=phase1*phase_to_deg;
88+
data[3]=frequency_2.getGaugeValue();
89+
data[4]=range.getRange2();
90+
data[5]=phase2*phase_to_deg;
91+
Wire.write(data,6);
92+
break;
93+
/*
94+
case 'T':
95+
test_flag=true;
96+
break;
97+
case 'N':
98+
test_flag=false;
99+
break;
100+
*/
101+
}
102+
}
103+
104+
105+
void setup() {
106+
Serial.begin(115200);
107+
generate_wave();
108+
pinMode(PH_1,INPUT);
109+
pinMode(PH_2,INPUT);
110+
frequency_1.refresh();
111+
frequency_2.refresh();
112+
range.update();
113+
114+
Wire.begin(0x2F);
115+
Wire.onReceive(comm_event);
116+
Wire.onRequest(comm_request);
117+
118+
//wave1.sine(freq1); //these use default sampling (24)
119+
//wave2.sine(freq1);
120+
wave1.begin(freq1);
121+
wave2.begin(freq2);
122+
wave1.sync(wave2);
123+
rephase=0;
124+
time_rephase=millis();
125+
}
126+
127+
128+
129+
void loop() {
130+
frequency_1.refresh();
131+
frequency_2.refresh();
132+
range.update();
133+
134+
freq1=frequency_1.getGaugeValue()*range.getRange1();
135+
freq2=frequency_2.getGaugeValue()*range.getRange2();
136+
137+
phase1=analogRead(PH_1)/step;
138+
if (phase1>(RES+10)){
139+
phase1=0;
140+
}
141+
142+
phase2=analogRead(PH_2)/step;
143+
if (phase2>RES){
144+
phase2=0;
145+
}
146+
147+
if (freq1!=previous_frequency_1){
148+
wave1.freq(freq1);
149+
previous_frequency_1=freq1;
150+
wave1.sync(wave2);
151+
}
152+
153+
if (freq2!=previous_frequency_2){
154+
wave2.freq(freq2);
155+
previous_frequency_2=freq2;
156+
wave2.sync(wave1);
157+
}
158+
159+
if (millis()-time_rephase>500){
160+
if (phase1!=previous_phase_1){
161+
wave1.offset(phase1/2);
162+
previous_phase_1=phase1;
163+
if (phase2==0){
164+
wave1.sync(wave2);
165+
}
166+
}
167+
if (phase2!=previous_phase_2){
168+
wave2.offset(phase2/2);
169+
previous_phase_2=phase2;
170+
if (phase1==0){
171+
wave2.sync(wave1);
172+
}
173+
}
174+
time_rephase=millis();
175+
}
176+
177+
178+
179+
180+
delay(1);
181+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
This file is part of the Arduino_GroveI2C_Ultrasonic library.
3+
Copyright (c) 2023 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "Arduino.h"
21+
#include "led_array.h"
22+
23+
LedArray::LedArray(const uint8_t _r1, const uint8_t _r2, const uint8_t _r3, const uint8_t _c1, const uint8_t _c2, const uint8_t _c3){
24+
r1=_r1;
25+
r2=_r2;
26+
r3=_r3;
27+
c1=_c1;
28+
c2=_c2;
29+
c3=_c3;
30+
31+
pinMode(r1,OUTPUT);
32+
pinMode(r2,OUTPUT);
33+
pinMode(r3,OUTPUT);
34+
pinMode(c1,OUTPUT);
35+
pinMode(c2,OUTPUT);
36+
pinMode(c3,OUTPUT);
37+
38+
set(0,0,0,0,0,0);
39+
}
40+
41+
/* frequency selector
42+
+-----+-----+------+
43+
| ROW | COL | LED |
44+
+-----+-----+------+
45+
| A_1 | A_1 | 90 |
46+
| A_2 | A_1 | 70 |
47+
| A_3 | A_1 | 80 |
48+
| A_1 | A_2 | 60 |
49+
| A_2 | A_2 | 40 |
50+
| A_3 | A_2 | 50 |
51+
| A_1 | A_3 | 30 |
52+
| A_2 | A_3 | 10 |
53+
| A_3 | A_3 | 20 |
54+
+-----+-----+------+
55+
*/
56+
57+
/* frequency selector 2 mid-march
58+
+-----+-----+------+
59+
| ROW | COL | LED |
60+
+-----+-----+------+
61+
| B_1 | B_1 | 90 |
62+
| B_2 | B_1 | 80 |
63+
| B_3 | B_1 | 70 |
64+
| B_1 | B_2 | 60 |
65+
| B_2 | B_2 | 50 |
66+
| B_3 | B_2 | 40 |
67+
| B_1 | B_3 | 30 |
68+
| B_2 | B_3 | 10 |
69+
| B_3 | B_3 | 20 |
70+
+-----+-----+------+
71+
*/
72+
73+
void LedArray::set(const int x){
74+
switch(x){
75+
case 0:
76+
set(1,0,1,0,0,1);
77+
break;
78+
case 1:
79+
set(1,1,0,0,0,1);
80+
break;
81+
case 2:
82+
set(0,1,1,0,0,1);
83+
break;
84+
case 3:
85+
set(1,0,1,0,1,0);
86+
break;
87+
case 4:
88+
set(1,1,0,0,1,0);
89+
break;
90+
case 5:
91+
set(0,1,1,0,1,0);
92+
break;
93+
case 6:
94+
set(1,0,1,1,0,0);
95+
break;
96+
case 7:
97+
set(1,1,0,1,0,0);
98+
break;
99+
case 8:
100+
set(0,1,1,1,0,0);
101+
break;
102+
103+
case 20:
104+
set(1,1,0,0,0,1);
105+
break;
106+
case 21:
107+
set(1,0,1,0,0,1);
108+
break;
109+
case 22:
110+
set(0,1,1,0,0,1);
111+
break;
112+
case 23:
113+
set(1,1,0,0,1,0);
114+
break;
115+
case 24:
116+
set(1,0,1,0,1,0);
117+
break;
118+
case 25:
119+
set(0,1,1,0,1,0);
120+
break;
121+
case 26:
122+
set(1,1,0,1,0,0);
123+
break;
124+
case 27:
125+
set(1,0,1,1,0,0);
126+
break;
127+
case 28:
128+
set(0,1,1,1,0,0);
129+
break;
130+
131+
132+
133+
default:
134+
set(1,1,1,0,0,0);
135+
}
136+
}
137+
138+
139+
void LedArray::set(const bool _r1, const bool _r2, const bool _r3, const bool _c1, const bool _c2, const bool _c3){
140+
digitalWrite(r1,_r1);
141+
digitalWrite(r2,_r2);
142+
digitalWrite(r3,_r3);
143+
digitalWrite(c1,_c1);
144+
digitalWrite(c2,_c2);
145+
digitalWrite(c3,_c3);
146+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
This file is part of the Arduino_GroveI2C_Ultrasonic library.
3+
Copyright (c) 2023 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef __LED_ARRAY_H__
21+
#define __LED_ARRAY_H__
22+
23+
#include "Arduino.h"
24+
25+
class LedArray{
26+
private:
27+
uint8_t r1, r2, r3, c1, c2, c3;
28+
public:
29+
LedArray(const uint8_t _r1, const uint8_t _r2, const uint8_t _r3, const uint8_t _c1, const uint8_t _c2, const uint8_t _c3);
30+
void set(const int x);
31+
void set(const bool _r1, const bool _r2, const bool _r3, const bool _c1, const bool _c2, const bool _c3);
32+
};
33+
34+
#endif

0 commit comments

Comments
 (0)