Skip to content

Commit

Permalink
Adding digital input support for TCA
Browse files Browse the repository at this point in the history
  • Loading branch information
beniroquai committed Aug 29, 2023
1 parent 284225e commit 20ec3c3
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 16 deletions.
8 changes: 4 additions & 4 deletions main/PinConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,9 @@ struct UC2_3 : PinConfig
int8_t LED_COUNT = 64;

// FIXME: Is this redudant?!
int8_t PIN_DEF_END_X = disabled;
int8_t PIN_DEF_END_Y = disabled;
int8_t PIN_DEF_END_Z = disabled;
int8_t PIN_DEF_END_X = 105; // I2C TCA
int8_t PIN_DEF_END_Y = 106;
int8_t PIN_DEF_END_Z = 107;
int8_t DIGITAL_IN_1 = PIN_DEF_END_X;
int8_t DIGITAL_IN_2 = PIN_DEF_END_Y;
int8_t DIGITAL_IN_3 = PIN_DEF_END_Z;
Expand Down Expand Up @@ -585,4 +585,4 @@ struct UC2_3 : PinConfig
int8_t SPI_CS = GPIO_NUM_5;
};

const UC2_2 pinConfig; // UC2_1 pinConfig; //_WEMOS pinConfig; //_2 pinConfig; //_2 pinConfig; OMNISCOPE;
const UC2_3 pinConfig; // UC2_1 pinConfig; //_WEMOS pinConfig; //_2 pinConfig; //_2 pinConfig; OMNISCOPE;
5 changes: 0 additions & 5 deletions main/json_api_BD.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@


# DIGITAL IN
{"task": "/digitalin_set", "digitalinid":1, "digitalinpin":13}
{"task": "/digitalin_set", "digitalinid":2, "digitalinpin":5}
{"task": "/digitalin_set", "digitalinid":3, "digitalinpin":23}

{"task": "/digitalin_get", "digitalinid":1}
{"task": "/digitalin_get", "digitalinid":2}
{"task": "/digitalin_get", "digitalinid":3}
Expand All @@ -101,7 +97,6 @@ payload = {

# HOME
{"task": "/home_get"}
{"task": "/home_set"}
{"task":"/home_act", "home": {"steppers": [{"stepperid":1, "timeout": 20000, "speed": 10000, "direction":1, "endposrelease":3000, "endstoppolarity":1}]}}
{"task":"/home_act", "home": {"steppers": [{"stepperid":2, "timeout": 20000, "speed": 10000, "direction":-1, "endposrelease":3000, "endstoppolarity":1}]}}
{"task":"/home_act", "home": {"steppers": [{"stepperid":3, "timeout": 2000, "speed": 10000, "direction":-1, "endposrelease":3000, "endstoppolarity":0}]}}
Expand Down
40 changes: 36 additions & 4 deletions main/src/digitalin/DigitalInController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,41 @@ cJSON* DigitalInController::get(cJSON* jsonDocument)
int digitalinval = 0;

// cretae new json document
//
// {"task": "/digitalin_get", "digitalinid":1}
// {"task": "/digitalin_get", "digitalinid":2}
FocusMotor *motor = (FocusMotor *)moduleController.get(AvailableModules::motor);
if (digitalinid == 1)
{
digitalinpin = pinConfig.DIGITAL_IN_1;
digitalinval = digitalRead(pinConfig.DIGITAL_IN_1);
if (pinConfig.I2C_SCL > 0){
// use the external port extender on UC2v3
digitalinval = motor->getExternalPinValue(pinConfig.DIGITAL_IN_1);
}
else{
digitalinval = digitalRead(pinConfig.DIGITAL_IN_1);
}
}
else if (digitalinid == 2)
{
digitalinpin = pinConfig.DIGITAL_IN_2;
digitalinval = digitalRead(pinConfig.DIGITAL_IN_2);
if (pinConfig.I2C_SCL > 0){
// use the external port extender on UC2v3
digitalinval = motor->getExternalPinValue(pinConfig.DIGITAL_IN_2);
}
else{
digitalinval = digitalRead(pinConfig.DIGITAL_IN_2);
}
}
else if (digitalinid == 3)
{
digitalinpin = pinConfig.DIGITAL_IN_3;
digitalinval = digitalRead(pinConfig.DIGITAL_IN_3);
if (pinConfig.I2C_SCL > 0){
// use the external port extender on UC2v3
digitalinval = motor->getExternalPinValue(pinConfig.DIGITAL_IN_3);
}
else{
digitalinval = digitalRead(pinConfig.DIGITAL_IN_3);
}
}
monitor_json = cJSON_CreateObject();
cJSON * id = cJSON_CreateNumber(digitalinid);
Expand All @@ -51,25 +71,37 @@ cJSON* DigitalInController::get(cJSON* jsonDocument)

void DigitalInController::setup()
{

log_i("Setting Up digitalin");
if (pinConfig.I2C_SCL == 0){
/* setup the output nodes and reset them to 0*/
log_i("DigitalIn 1: %i", pinConfig.DIGITAL_IN_1);
pinMode(pinConfig.DIGITAL_IN_1, INPUT_PULLDOWN);
log_i("DigitalIn 2: %i", pinConfig.DIGITAL_IN_2);
pinMode(pinConfig.DIGITAL_IN_2, INPUT_PULLDOWN);
log_i("DigitalIn 3: %i", pinConfig.DIGITAL_IN_3);
pinMode(pinConfig.DIGITAL_IN_3, INPUT_PULLDOWN);
}
}

void DigitalInController::loop()
{

//FIXME: Never reaches this position..

if (pinConfig.I2C_SCL == 0){
// readout digital pins one by one
digitalin_val_1 = digitalRead(pinConfig.DIGITAL_IN_1);
digitalin_val_2 = digitalRead(pinConfig.DIGITAL_IN_2);
digitalin_val_3 = digitalRead(pinConfig.DIGITAL_IN_3);
}
else{
// read values from the TCA input register
FocusMotor *motor = (FocusMotor *)moduleController.get(AvailableModules::motor);
digitalin_val_1 = motor->getExternalPinValue(pinConfig.DIGITAL_IN_1);
digitalin_val_2 = motor->getExternalPinValue(pinConfig.DIGITAL_IN_2);
digitalin_val_3 = motor->getExternalPinValue(pinConfig.DIGITAL_IN_3);
}
//log_i("digitalin_val_1: %i, digitalin_val_2: %i, digitalin_val_3: %i", digitalin_val_1, digitalin_val_2, digitalin_val_3);
}

2 changes: 1 addition & 1 deletion main/src/digitalin/DigitalInController.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "../../Module.h"

#include "../motor/FocusMotor.h"
class DigitalInController : public Module
{
private:
Expand Down
18 changes: 18 additions & 0 deletions main/src/motor/FocusMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@ cJSON *FocusMotor::get(cJSON *docin)
return doc;
}


int FocusMotor::getExternalPinValue(uint8_t pin)
{
// read register?
//_tca9535->TCA9535WriteOutput(&outRegister);
_tca9535->TCA9535ReadInput(&inRegister);

int value = -1;
pin = pin & ~PIN_EXTERNAL_FLAG;
if (pin == 105) // endstop X
value = inRegister.Port.P0.bit.Bit5;
if (pin == 106) // endstop Y
value = inRegister.Port.P0.bit.Bit6;
if (pin == 107) // endstop Z
value = inRegister.Port.P0.bit.Bit7;
return value;
}

bool FocusMotor::setExternalPin(uint8_t pin, uint8_t value)
{
// This example returns the previous value of the output.
Expand Down
2 changes: 2 additions & 0 deletions main/src/motor/FocusMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ class FocusMotor : public Module
void startStepper(int i);
void sendMotorPos(int i, int arraypos);
bool setExternalPin(uint8_t pin, uint8_t value);
int getExternalPinValue(uint8_t pin);
void setPosition(Stepper s, int pos);
void move(Stepper s, int steps, bool blocking);
bool isRunning(int i);
private:
tca9535 *_tca9535;
TCA9535_Register outRegister;
TCA9535_Register inRegister;
FAccelStep faccel;

int logcount;
Expand Down
2 changes: 1 addition & 1 deletion main/src/serial/SerialProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void SerialProcess::loop()
cJSON *tasks = cJSON_GetObjectItemCaseSensitive(root, "tasks");
if (tasks != NULL)
{

// {"tasks:":[{"task": "/state_get"}, {"task": "/state_act", "delay": 1000}], "nTimes": 3}
// {"tasks":[{"task":"/state_get"},{"task":"/state_act", "delay":1000}],"nTimes":2}
int nTimes = 1;
// perform the table n-times
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ framework =
espidf
arduino
build_flags =
-DCORE_DEBUG_LEVEL=0
-DCORE_DEBUG_LEVEL=5
-Wno-format


Expand Down

0 comments on commit 20ec3c3

Please sign in to comment.