Created
September 25, 2022 00:15
-
-
Save chrisisbeef/ba2a13585a322faa1537d5577c7e1b90 to your computer and use it in GitHub Desktop.
SPDT Relay for Wokwi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "wokwi-api.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
DEFINE_PIN(COM); | |
DEFINE_PIN(NC); | |
DEFINE_PIN(NO); | |
DEFINE_PIN(COIL); | |
typedef struct { | |
uint8_t pin_com, | |
pin_t pin_nc, | |
pin_t pin_no, | |
uint32_t coil | |
} chip_state_t; | |
chip_state_t* EXPORT(chip_init)(void)) { | |
chip_state_t *chip = malloc(sizeof(chip_state_t)); | |
chip->pin_com = pin_init("COM", INPUT); | |
chip->pin_coil = pin_init("COIL", INPUT); | |
chip->pin_nc = pin_init("NC", OUTPUT); | |
chip->pin_no = pin_init("NO", OUTPUT); | |
pin_watch(chip->pin_com, BOTH); | |
return chip; | |
} | |
void EXPORT(chip_timer_event)(chip_state_t *chip, uint32_t timer_id) { | |
uint32_t com = pin_read(chip->com); | |
uint32_t coil = pin_read(chip->coil); | |
if (com == HIGH && coil == HIGH) { | |
pin_write(chip->nc, HIGH); | |
pin_write(chip->no, LOW); | |
} else if (com == LOW && coil == HIGH) { | |
pin_write(chip->nc, LOW); | |
pin_write(chip->no, HIGH); | |
} else if (com == HIGH && coil == LOW) { | |
pin_write(chip->nc, LOW); | |
pin_write(chip->no, HIGH); | |
} else if (com == LOW && coil == LOW) { | |
pin_write(chip->nc, HIGH); | |
pin_write(chip->no, LOW); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "SPDT Relay", | |
"author": "Chris Schmidt", | |
"pins": [ | |
"NO", | |
"NC", | |
"COM", | |
"GND", | |
"NC", | |
"COIL" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "chip-spdt-relay", | |
"id": "relay1", | |
"top": 0, | |
"left": 0, | |
"attrs": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment