Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaonian72 committed May 16, 2021
1 parent cd81263 commit 1e9d492
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 9 deletions.
15 changes: 13 additions & 2 deletions src/AIOapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void AIOapp::readRaw() {
for(int i = 0; i < 4*ADC_NUM; i++){
_raw_data[i] = adc_l[i/ADC_NUM].readADC_SingleEnded(i%ADC_NUM);
}
airflow = fmeter.readflow();
flowvalue = fmeter.readflow();
}

void AIOapp::pump_speed(uint8_t speed) {
Expand All @@ -39,6 +39,17 @@ void AIOapp::fragOff() {
valve_switch.turn_off();
}

void AIOapp::fragFlow(uint8_t speed) {
fragOn();
pump1.set_speed(speed);
}

/* let air gas flow */
void AIOapp::airFlow(uint8_t speed) {
fragOff();
pump1.set_speed(speed);
}

void AIOapp::readVoltage() {
readRaw();
for(int i = 0; i < 4*ADC_NUM; i++){
Expand Down Expand Up @@ -73,7 +84,7 @@ void AIOapp::printData(){
for (int i = 0; i < ADC_NUM*4; i++){
mySerial.print(_voltage_data[i]*0.001,7);mySerial.print(",");
}
mySerial.print(airflow);
mySerial.print(flowvalue);
mySerial.println();
}

Expand Down
9 changes: 8 additions & 1 deletion src/AIOapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const uint8_t ADC_ADDRESS[] = {0x48, 0x49, 0x4a, 0x4b};
extern SoftwareSerial mySerial;
const uint32_t UPDATE_INTERVAL = 500;

struct AppConfig{
uint8_t airspeed = 80;
uint8_t fragspeed = 80;
};

class AIOapp {

Expand All @@ -32,6 +36,9 @@ class AIOapp {
inline void update() __attribute__((always_inline));
void pump_speed(uint8_t speed);

void fragFlow(uint8_t speed);
void airFlow(uint8_t speed);

private:
Adafruit_ADS1115 adc_l[ADC_NUM];
uint16_t _raw_data[4*ADC_NUM];
Expand All @@ -47,7 +54,7 @@ class AIOapp {
Pump pump1;
uint8_t p_speed=100;
Flowmeter fmeter;
int16_t airflow;
int flowvalue;

};

Expand Down
1 change: 1 addition & 0 deletions src/Flowmeter.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <define.h>
#include <Flowmeter.h>
#include <Wire.h>

Expand Down
26 changes: 21 additions & 5 deletions src/define.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
#ifndef GSAAIO_DEFINES_H
#define GSAAIO_DEFINES_H

#include <SoftwareSerial.h>
#include <Arduino.h>


// extern SoftwareSerial mySerial(5, 6);
// #define SLAVE_SERIAL mySerial

// #define DEBUG_SERIAL Serial
// #define DBGprint(...) Serial.print(__VA_ARGS__)
// #define DBGprintln(...) Serial.println(__VA_ARGS__)
//


#define ADC_NUM 4

#define SAMPLE_NUM 800
#define SAMPLE_DUR 1600
#define SAMPLE_VALVE_ON 10
#define SAMPLE_VALVE_OFF 12
#define SAMPLE_NUM 800 //800
#define SAMPLE_DUR 1600 //1600
#define SAMPLE_VALVE_ON 10 //10
#define SAMPLE_VALVE_OFF 12 //12
#define SAMPLE_PERIOD 20

#define VALVE_SWITCH 12
#define OLD_PUMP 13
#define OLD_PUMP 13

#endif //GSAAIO_DEFINES_H
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <define.h>
#include <Arduino.h>
#include <AIOapp.h>

Expand All @@ -18,5 +19,5 @@ void loop() {

// mySerial.print(a);
aiOapp.update();
}

}
77 changes: 77 additions & 0 deletions src/protoc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// Created by Zhenyi Ye on 2020-01-23.
//

#include <protoc.h>
#include <AIOapp.h>

extern AIOapp aiOapp;
extern msgBuff txBuff;
extern msgBuff rxBuff;

// app configuration
AppConfig appConfig;

// Functional Command
void parseAirflow(){
aiOapp.airFlow(appConfig.airspeed);
}

void parseFragflow(){
aiOapp.fragFlow(appConfig.fragspeed);
}

// Debug Command
void parsePumpPwm(){
uint8_t speed = rxBuff.buff[1];
aiOapp.airFlow(speed);
}

void parseValveSet(){
uint8_t valve_index = rxBuff.buff[1];
uint8_t valve_state = rxBuff.buff[2];
if (valve_state == 1)
valve_state = HIGH;
else
valve_state = LOW;
switch (valve_index){
case 1:
break;
case 2:
break;
case 3:
digitalWrite(VALVE_SWITCH, valve_state);
break;
default:
break;

}
}

// Parse Command Function
void parseCommand(){

uint8_t command_type = rxBuff.buff[0];

switch (command_type){

// functional command
case AIRFLOW:
parseAirflow();
break;
case FRAGFLOW:
parseFragflow();
break;
case CHAMCLOSE:
break;

// debug command
case PUMP_PWM:
parsePumpPwm();
break;
case VALVE_SET:
parseValveSet();
break;
}

}
28 changes: 28 additions & 0 deletions src/protoc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef GSAAIO_PROTOC_H
#define GSAAIO_PROTOC_H

#endif //GSAAIO_PROTOC_H

#include <Arduino.h>
#include "serial.h"

// DEBUG COMMAND
const uint8_t VALVE_SET = 0xF0;
const uint8_t PUMP_PWM = 0xF1;

// FUNCTIONAL COMMAND
const uint8_t AIRFLOW = 0x00;
const uint8_t FRAGFLOW = 0x01;
const uint8_t CHAMCLOSE = 0x02;



// Function Header
void parseAirflow();
void parseFragflow();


void parseValveSet();
void parsePumpPwm();

// Parse Command Function
136 changes: 136 additions & 0 deletions src/serial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include <serial.h>
// #include <stdint.h>
// #include <Arduino.h>

msgBuff rxBuff;
msgBuff txBuff;
bool ser_escape = false;
bool ser_reading = false;
uint8_t rxByte;

void (*msgHandler)();

void setNewMsgCallback(void (*cb)(void)) {
msgHandler = cb;
}

void parseSerial()
{
while(mySerial.available()) {
rxByte = mySerial.read();
// DBGprintln(rxByte, HEX);
if (!ser_escape) {
if (rxByte == '^') {
rxBuff.len = 0;
ser_reading = true;
return;
}
if (rxByte == '?' && ser_reading) {
ser_escape = true;
return;
}
if (rxByte == '\n') {
ser_reading = false;
// DBGprint("Len: ");
// DBGprintln(rxBuff.len);
parseCmd();
}
}
if (ser_reading && rxBuff.len < S_BUFF_SIZE) {
if (ser_escape) {
ser_escape = false;
}
rxBuff.buff[rxBuff.len++] = rxByte;
return;
}
if (rxBuff.len >= S_BUFF_SIZE) {
rxBuff.len = 0;
ser_reading = false;
ser_escape = false;
}
}
}

void parseCmd()
{

byte our_csum = calcRX_CSum(&rxBuff);
byte snt_csum = rxBuff.buff[rxBuff.len - 1];

if (our_csum != snt_csum) {
//complete csum error return
//SLAVE_SERIAL.write("^E\n");
}
else{
//SLAVE_SERIAL.println("{\"RESP\":1}");

msgHandler();
}
}

uint8_t calcRX_CSum(msgBuff *mb)
{
return calc_CSum(mb->buff, mb->len - 1);
}

uint8_t calcTX_CSum(msgBuff *mb)
{
return calc_CSum(mb->buff, mb->len);
}

uint8_t calc_CSum(uint8_t *buff, uint16_t len)
{
uint8_t csum = 0;
for (uint16_t i = 0; i < len; i++)
{
csum ^= *(buff + i);
}
return csum;
}


bool msgBuffAddByte(msgBuff *mb, uint8_t b)
{
if (mb->len < S_BUFF_SIZE) {
mb->buff[mb->len++] = b;
return true;
}
return false;
}

bool msgBuffAdd(msgBuff *mb, void * data, uint16_t size)
{
if (mb->len + size >= S_BUFF_SIZE)
{
return false;
}
uint8_t *b = (mb->buff + mb->len);
uint8_t *d = (uint8_t *)data;

for (uint16_t i = 0; i < size; i++) {
*(b + i) = *(d + i);
}
mb->len += size;
return true;
}

void sendMsgBuff(msgBuff *mb)
{
uint8_t csum = calcTX_CSum(mb);
msgBuffAddByte(mb,csum);
mySerial.write('^');
for (uint16_t i = 0; i < mb->len; i++) {
switch (mb->buff[i])
{
case '?':
case '^':
case '\n':
mySerial.write('?');
default:
mySerial.write(mb->buff[i]);
}
}
mySerial.write('\n');
}


Loading

0 comments on commit 1e9d492

Please sign in to comment.