summaryrefslogtreecommitdiffstats
path: root/prog/upravljalnik/naprava/src/main.cpp
blob: 78dd46a50120ad3ce3f6a1a33ff97f9637b0d61e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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);
	}
}