mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 19:59:50 +03:00
Encoder support
This commit is contained in:
@@ -29,6 +29,16 @@ e-mail anklimov@gmail.com
|
|||||||
extern PubSubClient mqttClient;
|
extern PubSubClient mqttClient;
|
||||||
//DHT dht();
|
//DHT dht();
|
||||||
|
|
||||||
|
#if defined(__AVR__)
|
||||||
|
static volatile long encoder_value[6];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__SAM3X8E__)
|
||||||
|
static short encoder_irq_map[54];
|
||||||
|
static long encoder_value[54];
|
||||||
|
static int encoders_count;
|
||||||
|
#endif
|
||||||
|
|
||||||
Input::Input(char * name) //Constructor
|
Input::Input(char * name) //Constructor
|
||||||
{
|
{
|
||||||
if (name)
|
if (name)
|
||||||
@@ -104,14 +114,12 @@ void Input::encoderPoll() {
|
|||||||
if (store->nextPollMillis > millis())
|
if (store->nextPollMillis > millis())
|
||||||
return;
|
return;
|
||||||
if (store->logicState == 0) {
|
if (store->logicState == 0) {
|
||||||
short real_pin;
|
|
||||||
#if defined(__AVR__)
|
#if defined(__AVR__)
|
||||||
#define interrupt_number pin
|
#define interrupt_number pin
|
||||||
if (interrupt_number >= 0 && interrupt_number < 6) {
|
if (interrupt_number >= 0 && interrupt_number < 6) {
|
||||||
short mega_interrupt_array[6] = {2, 3, 21, 20, 19, 18};
|
const short mega_interrupt_array[6] = {2, 3, 21, 20, 19, 18};
|
||||||
real_pin = mega_interrupt_array[interrupt_number];
|
short real_pin = mega_interrupt_array[interrupt_number];
|
||||||
pinMode(real_pin, INPUT);
|
attachInterruptPinIrq(real_pin,interrupt_number);
|
||||||
attachInterrupt(interrupt_number, onEncoderChanged, RISING);
|
|
||||||
} else {
|
} else {
|
||||||
Serial.print(F("IRQ:"));
|
Serial.print(F("IRQ:"));
|
||||||
Serial.print(pin);
|
Serial.print(pin);
|
||||||
@@ -121,19 +129,22 @@ void Input::encoderPoll() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__SAM3X8E__)
|
#if defined(__SAM3X8E__)
|
||||||
pinMode(pin, INPUT);
|
attachInterruptPinIrq(pin,encoders_count);
|
||||||
attachInterrupt(pin, onEncoderChanged, RISING);
|
encoder_irq_map[encoders_count]=pin;
|
||||||
|
encoders_count++;
|
||||||
#endif
|
#endif
|
||||||
store->logicState = 1;
|
store->logicState = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
long encoderValue = encoder_value[pin];
|
||||||
|
Serial.print(F("IN:"));Serial.print(pin);Serial.print(F(" Encoder type. val="));Serial.print(encoderValue);
|
||||||
|
|
||||||
aJsonObject *emit = aJson.getObjectItem(inputObj, "emit");
|
aJsonObject *emit = aJson.getObjectItem(inputObj, "emit");
|
||||||
Serial.print(F("IN:"));Serial.print(pin);Serial.print(F(" Encoder type. val="));Serial.print(store->aslong);
|
|
||||||
if (emit) {
|
if (emit) {
|
||||||
char valstr[10];
|
char valstr[10];
|
||||||
char addrstr[100] = "";
|
char addrstr[100] = "";
|
||||||
strcat(addrstr, emit->valuestring);
|
strcat(addrstr, emit->valuestring);
|
||||||
sprintf(valstr, "%d", store->aslong);
|
sprintf(valstr, "%d", encoderValue);
|
||||||
mqttClient.publish(addrstr, valstr);
|
mqttClient.publish(addrstr, valstr);
|
||||||
store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT;
|
store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT;
|
||||||
Serial.print(F(" NextPollMillis="));Serial.println(store->nextPollMillis);
|
Serial.print(F(" NextPollMillis="));Serial.println(store->nextPollMillis);
|
||||||
@@ -142,6 +153,33 @@ void Input::encoderPoll() {
|
|||||||
Serial.print(F(" No emit data!"));
|
Serial.print(F(" No emit data!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Input::attachInterruptPinIrq(int realPin, int irq) {
|
||||||
|
pinMode(realPin, INPUT);
|
||||||
|
switch(irq){
|
||||||
|
case 0:
|
||||||
|
attachInterrupt(realPin, onEncoderChanged0, RISING);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
attachInterrupt(realPin, onEncoderChanged1, RISING);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
attachInterrupt(realPin, onEncoderChanged2, RISING);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
attachInterrupt(realPin, onEncoderChanged3, RISING);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
attachInterrupt(realPin, onEncoderChanged4, RISING);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
attachInterrupt(realPin, onEncoderChanged5, RISING);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Serial.print(F("Incorrect irq:"));Serial.println(irq);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Input::dht22Poll() {
|
void Input::dht22Poll() {
|
||||||
#ifndef DHT_DISABLE
|
#ifndef DHT_DISABLE
|
||||||
if (store->nextPollMillis > millis())
|
if (store->nextPollMillis > millis())
|
||||||
@@ -249,6 +287,62 @@ void Input::onContactChanged(int val)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Input::onEncoderChanged() {
|
void Input::onEncoderChanged0() {
|
||||||
store->aslong++;
|
#if defined(__SAM3X8E__)
|
||||||
|
encoder_value[encoder_irq_map[0]]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__AVR__)
|
||||||
|
encoder_value[0]++;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::onEncoderChanged1() {
|
||||||
|
#if defined(__SAM3X8E__)
|
||||||
|
encoder_value[encoder_irq_map[1]]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__AVR__)
|
||||||
|
encoder_value[1]++;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::onEncoderChanged2() {
|
||||||
|
#if defined(__SAM3X8E__)
|
||||||
|
encoder_value[encoder_irq_map[2]]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__AVR__)
|
||||||
|
encoder_value[2]++;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::onEncoderChanged3() {
|
||||||
|
#if defined(__SAM3X8E__)
|
||||||
|
encoder_value[encoder_irq_map[3]]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__AVR__)
|
||||||
|
encoder_value[3]++;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::onEncoderChanged4() {
|
||||||
|
#if defined(__SAM3X8E__)
|
||||||
|
encoder_value[encoder_irq_map[4]]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__AVR__)
|
||||||
|
encoder_value[4]++;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::onEncoderChanged5() {
|
||||||
|
#if defined(__SAM3X8E__)
|
||||||
|
encoder_value[encoder_irq_map[5]]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__AVR__)
|
||||||
|
encoder_value[5]++;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,8 +64,7 @@ e-mail anklimov@gmail.com
|
|||||||
extern aJsonObject *inputs;
|
extern aJsonObject *inputs;
|
||||||
|
|
||||||
|
|
||||||
typedef union
|
typedef union {
|
||||||
{
|
|
||||||
long int aslong;
|
long int aslong;
|
||||||
struct {
|
struct {
|
||||||
int8_t reserve;
|
int8_t reserve;
|
||||||
@@ -76,33 +75,44 @@ typedef union
|
|||||||
unsigned long nextPollMillis;
|
unsigned long nextPollMillis;
|
||||||
} inStore;
|
} inStore;
|
||||||
|
|
||||||
class Input
|
class Input {
|
||||||
{
|
public:
|
||||||
public:
|
|
||||||
aJsonObject *inputObj;
|
aJsonObject *inputObj;
|
||||||
uint8_t inType;
|
uint8_t inType;
|
||||||
uint8_t pin;
|
uint8_t pin;
|
||||||
inStore * store;
|
inStore *store;
|
||||||
|
|
||||||
Input(int pin);
|
Input(int pin);
|
||||||
Input(aJsonObject * obj);
|
|
||||||
Input(char * name);
|
|
||||||
|
|
||||||
boolean isValid ();
|
Input(aJsonObject *obj);
|
||||||
|
|
||||||
|
Input(char *name);
|
||||||
|
|
||||||
|
boolean isValid();
|
||||||
|
|
||||||
void onContactChanged(int val);
|
void onContactChanged(int val);
|
||||||
|
|
||||||
int poll();
|
int poll();
|
||||||
protected:
|
|
||||||
|
static void onEncoderChanged0();
|
||||||
|
static void onEncoderChanged1();
|
||||||
|
static void onEncoderChanged2();
|
||||||
|
static void onEncoderChanged3();
|
||||||
|
static void onEncoderChanged4();
|
||||||
|
static void onEncoderChanged5();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
void Parse();
|
void Parse();
|
||||||
|
|
||||||
void contactPoll();
|
void contactPoll();
|
||||||
|
|
||||||
void dht22Poll();
|
void dht22Poll();
|
||||||
|
|
||||||
|
|
||||||
void printFloatValueToStr(float temp, char *valstr);
|
void printFloatValueToStr(float temp, char *valstr);
|
||||||
|
|
||||||
void encoderPoll();
|
void encoderPoll();
|
||||||
|
|
||||||
void onEncoderChanged();
|
void attachInterruptPinIrq(int realPin, int irq);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user