diff --git a/analog_input.h b/analog_input.h index 1173e60..fb44018 100644 --- a/analog_input.h +++ b/analog_input.h @@ -19,7 +19,7 @@ class AnalogInput { ~AnalogInput() {} /** - * @brief Initializes a analog input object. + * Initializes a analog input object. * * @param pin gpio pin for the analog input. */ @@ -29,7 +29,7 @@ class AnalogInput { } /** - * @brief Read the value of the analog input. + * Read the value of the analog input and set instance state. * */ void Process() { @@ -41,16 +41,25 @@ class AnalogInput { // Set calibration values. void AdjustCalibrationLow(int amount) { low_ += amount; } - void AdjustCalibrationOffset(int amount) { offset_ += amount; } + void AdjustCalibrationOffset(int amount) { offset_ -= amount; } void AdjustCalibrationHigh(int amount) { high_ += amount; } /** - * @brief Get the current value of the analog input within a range of +/-512. + * Get the current value of the analog input within a range of +/-512. * * @return read value within a range of +/-512. + * */ inline int16_t Read() { return read_; } + /** + * Return the analog read value as voltage. + * + * @return A float representing the voltage (-5.0 to +5.0). + * + */ + inline float Voltage() { return ((read_ / 512.0) * 5.0); } + private: uint8_t pin_; int16_t read_; diff --git a/examples/calibrate_analog/calibrate_analog.ino b/examples/calibrate_analog/calibrate_analog.ino index fa5115c..b7112c3 100644 --- a/examples/calibrate_analog/calibrate_analog.ino +++ b/examples/calibrate_analog/calibrate_analog.ino @@ -1,19 +1,19 @@ /** * Analog Input Calibration Script - * + * * Provide each CV input with a constant voltage of -5v, 0v, and 5v. For * each config point, provide the appropriate voltage value and then adjust * the encoder until you have the correct calibration value set. - * + * * With the arrow on the left side of the bar, provide a -5v signal and adjust * the encoder until you read -512. - * - * With the arrow in the center of the bar, provide a 0v signal and adjust the + * + * With the arrow in the center of the bar, provide a 0v signal and adjust the * encoder until you read 0. - * + * * With the arrow on the right side of the bar, provide a 5v signal and adjust * the encoder until you read 512. - * + * * TODO: store the calibration value in EEPROM. */ @@ -56,44 +56,50 @@ void CalibrateCV(Direction dir, int val) { void UpdateDisplay() { gravity.display.clearDisplay(); + DisplayCalibration(&gravity.cv1, "CV1: ", 0); + DisplayCalibration(&gravity.cv2, "CV2: ", 1); + gravity.display.display(); +} - int cv1 = gravity.cv1.Read(); - int cv2 = gravity.cv2.Read(); +void DisplayCalibration(AnalogInput* cv, String title, int index) { + int barWidth = 100; + int barHeight = 10; + int textHeight = 10; + int half = barWidth / 2; + int offsetX = 16; + int offsetY = (32 * index); + int color = 1; - // CV1 Value - gravity.display.setCursor(10, 2); - gravity.display.print(F("CV1: ")); - gravity.display.print(cv1); + // CV value reading. + int value = constrain(cv->Read(), -512, 512); - gravity.display.drawRect(10, 12, 100, 10, 1); - if (cv1 > 0) { + gravity.display.setCursor(0, offsetY); + gravity.display.print(title); + gravity.display.print(value >= 0 ? " " : ""); + gravity.display.print(value); + + gravity.display.setCursor(92, offsetY); + gravity.display.print(value >= 0 ? " " : ""); + gravity.display.print(cv->Voltage()); + gravity.display.print(F("V")); + + gravity.display.drawRect(offsetX, textHeight + offsetY, barWidth, barHeight, color); + if (value > 0) { // 0 to 512 - int x = (float(cv1) / 512.0) * 50; - gravity.display.fillRect(60, 12, x, 10, 1); + int x = (float(value) / 512.0) * half; + int fill = min(x, 512); + gravity.display.fillRect(half + offsetX, textHeight + offsetY, fill, barHeight, color); } else { // -512 to 0 - int x = (float(abs(cv1)) / 512.0) * 50; - gravity.display.fillRect(60 - x, 12, x, 10, 1); + int x = (float(abs(value)) / 512.0) * half; + int fill = min(half, x); + gravity.display.fillRect((half + offsetX) - x, textHeight + offsetY, fill, barHeight, color); } - // CV2 Value - gravity.display.setCursor(10, 32); - gravity.display.print(F("CV2: ")); - gravity.display.print(cv2); - - gravity.display.drawRect(10, 42, 100, 10, 1); - if (cv2 >= 0) { - int x = (float(cv2) / 512.0) * 50; - gravity.display.fillRect(60, 42, x, 10, 1); - } else { - int x = (float(abs(cv2)) / 512.0) * 50; - gravity.display.fillRect(60 - x, 42, x, 10, 1); + // Display selected calibration point if selected calibration point belongs to current cv input. + if (selected_param / 3 == index) { + int left = offsetX + ((half - 2) * (selected_param % 3)); + int top = barHeight + textHeight + offsetY + 2; + gravity.display.drawChar(left, top, 0x1E, 1, 0, 1); } - - // Selected calibration point. - int left = 10 + (48 * (selected_param % 3)); - int top = 22 + (selected_param > 2 ? 32 : 0); - gravity.display.drawChar(left, top, 0x1E, 1, 0, 1); - - gravity.display.display(); -} \ No newline at end of file +}