diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2023-04-28 21:17:02 +0200 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2023-04-28 21:17:02 +0200 |
commit | 97c5aa834b041bf52b1b9f508d58d39b03f70712 (patch) | |
tree | eb4278adb833fd86529eda097c471d299641ef22 /prog/upravljalnik/naprava/src/main.cpp | |
parent | razno (diff) | |
download | r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.gz r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.bz2 r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.lz r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.xz r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.zst r-97c5aa834b041bf52b1b9f508d58d39b03f70712.zip |
Diffstat (limited to 'prog/upravljalnik/naprava/src/main.cpp')
-rw-r--r-- | prog/upravljalnik/naprava/src/main.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/prog/upravljalnik/naprava/src/main.cpp b/prog/upravljalnik/naprava/src/main.cpp new file mode 100644 index 0000000..78dd46a --- /dev/null +++ b/prog/upravljalnik/naprava/src/main.cpp @@ -0,0 +1,68 @@ +#include <Arduino.h> +#include <ADS1115_WE.h> +#include <ESP8266WiFi.h> +#include <Wire.h> +extern "C" { + #include "user_interface.h" +} +void incoming (unsigned char * buf, short unsigned int type) { + Serial.println(type); +} +uint8_t curchan[] = { 0 }; // we have sizeof curchan modules connected (at most four) +bool ready[sizeof curchan] = { false }; +IRAM_ATTR void rdy0 () { + ready[0] = true; +} +ADS1115_WE adc[] = { ADS1115_WE(0x49) }; // VDD +void (*rdyfuncs[])() = { rdy0 }; +int rdypins[] = { D7 }; +uint16_t adcval[sizeof curchan][4]; +bool upload = false; +void setup () { + Wire.begin(); + Wire.setClock(400000L); + Serial.begin(MONITOR_SPEED); + Serial.println("setupstart"); + for (unsigned i = 0; i < sizeof curchan; i++) { + Serial.println("nastavljam ADC " + String(i)); + if (!adc[i].init()) + Serial.println("ADS1115 " + String(i) + " not connected!"); + pinMode(rdypins[i], INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(rdypins[i]), rdyfuncs[i], RISING); + adc[i].setVoltageRange_mV(ADS1115_RANGE_4096); + adc[i].setSingleChannel(curchan[i]); + adc[i].setConvRate(ADS1115_32_SPS); + adc[i].setMeasureMode(ADS1115_CONTINOUS); + adc[i].setAlertPinMode(ADS1115_ASSERT_AFTER_1); + adc[i].setAlertPinToConversionReady(); + adc[i].skip_delays = true; + } + Serial.println("nastavljam wifi"); + WiFi.setOutputPower(20.5); + wifi_set_opmode(STATION_MODE); + wifi_station_disconnect(); + wifi_promiscuous_enable(1); + wifi_set_promiscuous_rx_cb(incoming); + wifi_set_channel(13); + Serial.println("nastavil"); +} +uint8_t packet[] = { 0x08, 0x00, 0x00, 0x00, 'K', '@', '4', 'A', '.', 'S', 'I', ' ', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +void loop () { + for (unsigned i = 0; i < sizeof curchan; i++) { + if (!ready[i]) + continue; + uint16_t val = adc[i].getRawResult(); + if (val != adcval[i][curchan[i]]) { + adcval[i][curchan[i]] = val; + packet[12+i*8+curchan[i]*2] = val >> 8; + packet[12+i*8+curchan[i]*2+1] = val & 0xff; + upload = true; + } + adc[i].setSingleChannel((curchan[i] = (curchan[i]+1) % 4)); // XXX kaj če dobim ready signal še preden naprava dejansko zamenja naslov? za vsak slučaj ready=false nastavim tukaj spodaj, ampak vseeno zna biti kaj težav, še posebej glede na to, da izvorna knjižnica za ads1115 za chansel da nekaj delaya + ready[i] = false; + } + if (upload) { + upload = false; + wifi_send_pkt_freedom(packet, sizeof packet, 0); + } +} |