{"id":33,"date":"2025-04-26T17:55:28","date_gmt":"2025-04-26T17:55:28","guid":{"rendered":"https:\/\/8p8c.org\/?page_id=33"},"modified":"2025-04-26T17:55:28","modified_gmt":"2025-04-26T17:55:28","slug":"arduino-code-esp-32","status":"publish","type":"page","link":"https:\/\/8p8c.org\/?page_id=33","title":{"rendered":"Arduino Code &#8211; ESP-32"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;Wire.h>\n\n#define DEVICE_ADDR 0x09   \/\/ I\u00b2C address of the DiSEqC module\n#define CONTROL_REG 0x01   \/\/ Register to which commands are sent\n#define STATUS_REG  0x02   \/\/ Register from which responses are read\n\n\/\/ Structure that defines a DiSEqC test command.\nstruct DiseqcCommand {\n  const char* description; \/\/ Short description of the command\n  uint8_t bytes&#91;6];        \/\/ Command bytes (up to 6 bytes; adjust size if needed)\n  uint8_t length;          \/\/ Actual number of bytes in the command\n};\n\n\/*\n  Comprehensive list of known DiSEqC commands from the specification.\n  Many devices support only a subset; this program tests them all.\n*\/\nDiseqcCommand commands&#91;] = {\n  { \"Reset ALL devices\",                 { 0xE0, 0x00, 0x00 },        3 },\n  { \"Reset polar\/azimuth positioner\",      { 0xE0, 0x31, 0x00 },        3 },\n  { \"Select Input Port A (Switch A)\",      { 0xE0, 0x10, 0x38 },        3 },\n  { \"Select Input Port B (Switch B)\",      { 0xE0, 0x10, 0x39 },        3 },\n  { \"Select Input Port C (Switch C)\",      { 0xE0, 0x10, 0x3A },        3 },\n  { \"Select Input Port D (Switch D)\",      { 0xE0, 0x10, 0x3B },        3 },\n  { \"Stop polar\/azimuth movement\",         { 0xE0, 0x31, 0x60 },        3 },\n  { \"Disable limits\",                      { 0xE0, 0x31, 0x63 },        3 },\n  { \"Set CW limit to current pos\",         { 0xE0, 0x31, 0x66 },        3 },\n  { \"Set CCW limit to current pos\",        { 0xE0, 0x31, 0x67 },        3 },\n  { \"Drive to stored position #1\",         { 0xE0, 0x31, 0x6B, 0x01 },  4 },\n  { \"Drive to stored position #2\",         { 0xE0, 0x31, 0x6B, 0x02 },  4 },\n  { \"Relative movement east (5 steps)\",    { 0xE0, 0x31, 0x40, 0x05 },  4 },\n  { \"Relative movement west (5 steps)\",    { 0xE0, 0x31, 0x41, 0x05 },  4 },\n  { \"Absolute position: 10\u00b0 CW from zero\", { 0xE0, 0x31, 0x6E, 0xE0, 0xA0 }, 5 },\n  { \"Absolute position: 10\u00b0 CCW from zero\", { 0xE0, 0x31, 0x6E, 0xD0, 0xA0 }, 5 }\n};\n\nconst int numCommands = sizeof(commands) \/ sizeof(commands&#91;0]);\n\n\/\/ Function prototypes\nvoid runAllTests();\nvoid runTest(const DiseqcCommand &amp;cmd, int testNumber);\nvoid printBytes(const uint8_t *data, uint8_t length);\nvoid interpretResponse(uint8_t response);\n\nvoid setup() {\n  Serial.begin(115200);\n  while (!Serial); \/\/ Wait for Serial Monitor, if needed\n\n  \/\/ Initialize I\u00b2C (for ESP32 use GPIO21 as SDA, GPIO22 as SCL)\n  Wire.begin(21, 22);\n  \n  Serial.println(\"====================================================\");\n  Serial.println(\" Comprehensive DiSEqC Commands Test Program\");\n  Serial.println(\"====================================================\");\n  Serial.println(\"This program will issue ALL known DiSEqC commands one by one.\");\n  Serial.println(\"Since we don't yet know which commands are implemented on the chip,\");\n  Serial.println(\"each command is tested and its response is logged.\");\n  Serial.println(\"Press any key to begin the tests...\");\n\n  \/\/ Wait for a keystroke from the Serial Monitor\n  while (Serial.available() == 0) {}\n  while (Serial.available() > 0) { Serial.read(); } \/\/ Flush input\n  \n  runAllTests();\n}\n\nvoid loop() {\n  \/\/ All tests are executed in setup(); loop remains idle.\n}\n\n\/\/----------------------------------------------------------------\n\/\/ runAllTests()\n\/\/  Iterates through the commands array and executes each test.\n\/\/----------------------------------------------------------------\nvoid runAllTests() {\n  Serial.println(\"\\n--- Starting DiSEqC Command Tests ---\\n\");\n  for (int i = 0; i &lt; numCommands; i++) {\n    runTest(commands&#91;i], i + 1);\n    \n    Serial.println(\"\\n>> Press any key when ready for the next test...\");\n    while (Serial.available() == 0) {}         \/\/ Wait for user to hit a key\n    while (Serial.available() > 0) { Serial.read(); } \/\/ Flush input\n    Serial.println(\"----------------------------------------------------\\n\");\n  }\n  Serial.println(\"\\n=== All DiSEqC Tests Completed ===\");\n}\n\n\/\/----------------------------------------------------------------\n\/\/ runTest()\n\/\/  Sends a DiSEqC command, retrieves the response, and prints the result.\n\/\/----------------------------------------------------------------\nvoid runTest(const DiseqcCommand &amp;cmd, int testNumber) {\n  Serial.print(\"Test \");\n  Serial.print(testNumber);\n  Serial.print(\": \");\n  Serial.println(cmd.description);\n  \n  Serial.print(\"Sending Command Bytes: \");\n  printBytes(cmd.bytes, cmd.length);\n\n  \/\/ Send the command over I\u00b2C (using CONTROL_REG as a command header)\n  Wire.beginTransmission(DEVICE_ADDR);\n  Wire.write(CONTROL_REG);\n  for (uint8_t i = 0; i &lt; cmd.length; i++) {\n    Wire.write(cmd.bytes&#91;i]);\n  }\n  uint8_t i2cResult = Wire.endTransmission();\n  \n  if (i2cResult == 0) {\n    Serial.println(\"&#91;INFO] I\u00b2C Transmission successful.\");\n  } else {\n    Serial.print(\"&#91;ERROR] I\u00b2C Transmission failed with code: \");\n    Serial.println(i2cResult);\n    return;  \/\/ Skip response reading if transmission failed.\n  }\n  \n  \/\/ Allow the module time to process the command\n  delay(200);\n  \n  \/\/ Request a response from the STATUS_REG (expecting one status byte)\n  Wire.beginTransmission(DEVICE_ADDR);\n  Wire.write(STATUS_REG);\n  Wire.endTransmission();\n  Wire.requestFrom(DEVICE_ADDR, 1);\n\n  if (Wire.available()) {\n    uint8_t ack = Wire.read();\n    Serial.print(\"Received Acknowledgment: 0x\");\n    Serial.print(ack, HEX);\n    Serial.print(\" (Binary: \");\n    Serial.print(ack, BIN);\n    Serial.println(\")\");\n    interpretResponse(ack);\n  } else {\n    Serial.println(\"&#91;ERROR] No response received from device.\");\n  }\n}\n\n\/\/----------------------------------------------------------------\n\/\/ printBytes()\n\/\/  Helper function to print an array of bytes in hexadecimal format.\n\/\/----------------------------------------------------------------\nvoid printBytes(const uint8_t *data, uint8_t length) {\n  for (uint8_t i = 0; i &lt; length; i++) {\n    Serial.print(\"0x\");\n    if (data&#91;i] &lt; 0x10)\n      Serial.print(\"0\");  \/\/ For neat alignment of single-digit values\n    Serial.print(data&#91;i], HEX);\n    Serial.print(\" \");\n  }\n  Serial.println();\n}\n\n\/\/----------------------------------------------------------------\n\/\/ interpretResponse()\n\/\/  Extends the response interpretation to handle the ubiquitous 0x21 code.\n\/\/----------------------------------------------------------------\nvoid interpretResponse(uint8_t response) {\n  if (response == 0x21) {\n    Serial.println(\"&#91;RESULT] Response 0x21: This value is returned for every command.\");\n    Serial.println(\"           It may indicate a default acknowledgement (e.g., with internal flags set).\");\n    Serial.println(\"           Consult the datasheet or further test with additional registers\/commands.\");\n  }\n  else {\n    switch (response) {\n      case 0x00:\n        Serial.println(\"&#91;RESULT] Response 0x00: Undefined or no response.\");\n        break;\n      case 0x01:\n        Serial.println(\"&#91;RESULT] Success: Command executed successfully.\");\n        break;\n      case 0x02:\n        Serial.println(\"&#91;RESULT] Warning: Device busy; try again later.\");\n        break;\n      case 0x03:\n        Serial.println(\"&#91;RESULT] Error: Command not recognized by device.\");\n        break;\n      default:\n        Serial.print(\"&#91;RESULT] Unknown response code: 0x\");\n        Serial.print(response, HEX);\n        Serial.println(\".\");\n        break;\n    }\n  }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-33","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/pages\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/8p8c.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33"}],"version-history":[{"count":1,"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/pages\/33\/revisions"}],"predecessor-version":[{"id":35,"href":"https:\/\/8p8c.org\/index.php?rest_route=\/wp\/v2\/pages\/33\/revisions\/35"}],"wp:attachment":[{"href":"https:\/\/8p8c.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}