Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion ASCOM_Driver/CoverCalibratorDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public class CoverCalibrator : ICoverCalibratorV1
private const string COMMAND_CALIBRATOR_OFF = "COMMAND:CALIBRATOR:OFF";
private const string RESULT_CALIBRATOR_BRIGHTNESS = "RESULT:CALIBRATOR:BRIGHTNESS:";

private const string COMMAND_CALIBRATOR_GETSTATE = "COMMAND:CALIBRATOR:GETSTATE";
private const string RESULT_CALIBRATOR_STATE = "RESULT:CALIBRATOR:STATE:";

private const int MAX_BRIGHTNESS = 255;

/// <summary>
Expand Down Expand Up @@ -367,7 +370,46 @@ public CalibratorStatus CalibratorState
{
get
{
return CalibratorStatus.Ready;
CheckConnected("CalibratorState");
tl.LogMessage("CalibratorState", "Sending request to device to read calibrator state");
objSerial.Transmit(COMMAND_CALIBRATOR_GETSTATE + SEPARATOR);
tl.LogMessage("CalibratorState", "Sent request to device to read calibrator state");
string response;
try
{
response = objSerial.ReceiveTerminated(SEPARATOR).Trim();
}
catch (Exception e)
{
tl.LogMessage("CalibratorState", "Exception: " + e.Message);
throw e;
}
tl.LogMessage("CalibratorState", "Received response from device");
if (!response.StartsWith(RESULT_CALIBRATOR_STATE))
{
tl.LogMessage("CalibratorState", "Invalid response from device: " + response);
throw new ASCOM.DriverException("Invalid response from device: " + response);
}
string arg = response.Substring(RESULT_CALIBRATOR_STATE.Length);
int value;
try
{
value = Int32.Parse(arg);
}
catch (FormatException)
{
tl.LogMessage("CalibratorState", "Invalid state value received from device: " + arg);
throw new ASCOM.DriverException("Invalid state value received from device: " + arg);
}
if (value < 0 || value > 5)
{
tl.LogMessage("CalibratorState", "Invalid state value received from device: " + arg);
throw new ASCOM.DriverException("Invalid state value received from device: " + arg);
}
else
{
return (CalibratorStatus)value;
}
}
}

Expand Down
50 changes: 38 additions & 12 deletions Arduino_Firmware/Arduino_Firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,44 @@
* Licensed under the MIT License. See the accompanying LICENSE file for terms.
*/

constexpr auto DEVICE_GUID = "7e2006ab-88b5-4b09-b0b3-1ac3ca8da43e";
constexpr auto DEVICE_GUID = "ff46156d-2bfe-4cf3-acca-68324f7d6d92";

constexpr auto COMMAND_PING = "COMMAND:PING";
constexpr auto RESULT_PING = "RESULT:PING:OK:";
constexpr auto COMMAND_PING = "COMMAND::PING";
constexpr auto RESULT_PING = "RESULT::PING::PONG:";

constexpr auto COMMAND_INFO = "COMMAND:INFO";
constexpr auto RESULT_INFO = "RESULT:INFO:DarkSkyGeek's Flat Panel Firmware v1.0";
constexpr auto COMMAND_INFO = "COMMAND::INFO";
constexpr auto RESULT_INFO = "RESULT::INFO::LHAFlatOne v1.0";

constexpr auto COMMAND_CALIBRATOR_GETBRIGHTNESS = "COMMAND:CALIBRATOR:GETBRIGHTNESS";
constexpr auto COMMAND_CALIBRATOR_ON = "COMMAND:CALIBRATOR:ON:";
constexpr auto COMMAND_CALIBRATOR_OFF = "COMMAND:CALIBRATOR:OFF";
constexpr auto RESULT_CALIBRATOR_BRIGHTNESS = "RESULT:CALIBRATOR:BRIGHTNESS:";
constexpr auto COMMAND_CALIBRATOR_GETBRIGHTNESS = "COMMAND::CALIBRATOR::GETBRIGHTNESS";
constexpr auto COMMAND_CALIBRATOR_ON = "COMMAND::CALIBRATOR::ON:";
constexpr auto COMMAND_CALIBRATOR_OFF = "COMMAND::CALIBRATOR::OFF";
constexpr auto COMMAND_CALIBRATOR_GETSTATE = "COMMAND::CALIBRATOR::GETSTATE";
constexpr auto RESULT_CALIBRATOR_STATE = "RESULT::CALIBRATOR::STATE:";
constexpr auto RESULT_CALIBRATOR_BRIGHTNESS = "RESULT::CALIBRATOR::BRIGHTNESS:";

constexpr auto ERROR_INVALID_COMMAND = "ERROR:INVALID_COMMAND";
constexpr auto ERROR_INVALID_COMMAND = "ERROR::INVALID_COMMAND";

#define MIN_BRIGHTNESS 0
#define MAX_BRIGHTNESS 255
#define PWM_FREQ 20000
#define PWM_FREQ 5000

// initialize variables to precompute gamma correction table to ensure a perceived linearity to brightness
constexpr float GAMMA = 2.2;
int gammaTable[MAX_BRIGHTNESS + 1];

byte brightness = 0;
byte state = 1;

int ledPin = 8;

// The `setup` function runs once when you press reset or power the board.
void setup() {
// Precompute gamma correction table
for (int i = 0; i < 256; i++) {
gammaTable[i] = map(pow((i - MIN_BRIGHTNESS) / float(MAX_BRIGHTNESS - MIN_BRIGHTNESS), GAMMA) * (MAX_BRIGHTNESS - MIN_BRIGHTNESS),
MIN_BRIGHTNESS, MAX_BRIGHTNESS, 0, 1023);
}

// Initialize serial port I/O.
Serial.begin(57600);
while (!Serial) {
Expand Down Expand Up @@ -70,6 +83,9 @@ void loop() {
else if (command == COMMAND_CALIBRATOR_OFF) {
calibratorOff();
}
else if (command == COMMAND_CALIBRATOR_GETSTATE) {
getState();
}
else {
handleInvalidCommand();
}
Expand All @@ -92,18 +108,23 @@ void setBrightness() {
// The nice thing about the `pwm` function is that we can set the frequency
// to a much higher value (I use 20kHz) This does not work on all pins!
// For example, it does not work on pin 7 of the Xiao, but it works on pin 8.
int value = map(brightness, MIN_BRIGHTNESS, MAX_BRIGHTNESS, 0, 1023);

// Do a lookup to the precomputed gamma table here
// int value = map(brightness, MIN_BRIGHTNESS, MAX_BRIGHTNESS, 0, 1023);
int value = gammaTable[brightness];
pwm(ledPin, PWM_FREQ, value);
}

void calibratorOn(byte _brightness) {
brightness = _brightness;
setBrightness();
state = 3;
}

void calibratorOff() {
brightness = 0;
setBrightness();
state = 1;
}

//-- MISCELLANEOUS ------------------------------------------------------------
Expand All @@ -120,3 +141,8 @@ void sendFirmwareInfo() {
void handleInvalidCommand() {
Serial.println(ERROR_INVALID_COMMAND);
}

void getState() {
Serial.print(RESULT_CALIBRATOR_STATE);
Serial.println(state);
}