@@ -75,8 +75,8 @@ class WorkshopVoltageSource : public WorkshopVoltageSourceBase {
75
75
if (!already_succeeded)
76
76
Debug_printf (" WorkshopVoltageSource#fetch_current_voltage reading from channel %i, check you're using correct address ADC board if crash here!\n " , this ->channel );
77
77
78
- digitalWrite (MX_A, bank & 1 );
79
- digitalWrite (MX_B, bank & 2 );
78
+ gpio_put (MX_A, bank & 1 );
79
+ gpio_put (MX_B, bank & 2 );
80
80
81
81
// NB this seems to need 1us delay for pins to 'settle' before reading.
82
82
sleep_us (1 );
@@ -115,4 +115,69 @@ class WorkshopVoltageSource : public WorkshopVoltageSourceBase {
115
115
116
116
};
117
117
118
+ #include " ComputerCard.h"
119
+
120
+ class ComputerCardVoltageSource : public WorkshopVoltageSourceBase {
121
+ public:
122
+ byte channel = 0 ;
123
+ ComputerCard *sw = nullptr ;
124
+
125
+ ComputerCardVoltageSource (int global_slot, ComputerCard *sw, byte channel, float maximum_input_voltage = 5.0 , bool supports_pitch = false )
126
+ : WorkshopVoltageSourceBase(global_slot, supports_pitch) {
127
+ this ->sw = sw;
128
+ this ->channel = channel;
129
+ this ->maximum_input_voltage = maximum_input_voltage;
130
+ // this->debug = true;
131
+ }
132
+
133
+ // returns the last read raw voltage value
134
+ virtual float get_voltage () override {
135
+ // this->update();
136
+ return this ->current_value ;
137
+ }
138
+
139
+ bool already_succeeded = false ;
140
+ virtual float fetch_current_voltage () override {
141
+ if (this ->debug ) {
142
+ Serial.printf (" in WorkshopVoltageSource#fetch_current_voltage(slot=%i, channel=%i).." , global_slot, channel);
143
+ // Debug_printf(F("\tads_source is @%p, reading from channel %i\n"), this->ads_source, this->channel);
144
+ }
145
+ if (!already_succeeded)
146
+ Debug_printf (" WorkshopVoltageSource#fetch_current_voltage reading from channel %i, check you're using correct address ADC board if crash here!\n " , this ->channel );
147
+
148
+
149
+ int adcReading = channel < 3 ? sw->KnobVal ((ComputerCard::Knob)channel) :
150
+ channel==3 ? sw->SwitchVal () : sw->CVIn (channel-4 );
151
+
152
+ float voltageFromAdc = this ->adcread_to_voltage (adcReading);
153
+
154
+ if (this ->debug ) {
155
+ Serial.printf (" WorkshopVoltageSource channel %i read ADC voltageFromAdc %i\t :" , channel, adcReading); Serial_flush ();
156
+ }
157
+
158
+ float voltageCorrected = this ->get_corrected_voltage (voltageFromAdc);
159
+
160
+ if (this ->debug ) {
161
+ Serial.print (F (" after correction stage 2 got " ));
162
+ Serial.println (voltageCorrected);
163
+ }
164
+
165
+ if (this ->debug ) Serial.printf (" in WorkshopVoltageSource#fetch_current_voltage() finishing (and returning %f)\n " , voltageCorrected);
166
+
167
+ return maximum_input_voltage - voltageCorrected;
168
+ }
169
+
170
+ virtual float adcread_to_voltage (int16_t adcReading) {
171
+ float voltageFromAdc = float (adcReading) * (maximum_input_voltage / 4095.0 ); // 12 bit ADC, 0-4095
172
+ return voltageFromAdc;
173
+ }
174
+
175
+ virtual float get_corrected_voltage (float voltageFromAdc) {
176
+ return (voltageFromAdc * correction_value_1) + correction_value_2;
177
+ // return (voltageFromAdc + correction_value_2) * correction_value_1;
178
+ // return (voltageFromAdc - 0.25) * 2.0;
179
+ }
180
+
181
+ };
182
+
118
183
#endif
0 commit comments