Skip to content

Instantly share code, notes, and snippets.

@fxprime
Created January 31, 2021 10:58
Show Gist options
  • Save fxprime/5574426a2d5792bde8352349c67af5a7 to your computer and use it in GitHub Desktop.
Save fxprime/5574426a2d5792bde8352349c67af5a7 to your computer and use it in GitHub Desktop.
/* -------------------------------------------------------------------------- */
/* 16 ch analog reading Mux vs TTGO display exam */
/* By www.ModuleMore.com */
/* -------------------------------------------------------------------------- */
#include <Arduino.h>
/* -------------------------------------------------------------------------- */
/* Display */
/* -------------------------------------------------------------------------- */
/* ----------- Edit User_Setup_Select.h to Setup25_TTGO_T_Display ----------- */
#include <SPI.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
/* -------------------------------------------------------------------------- */
/* Mux */
/* -------------------------------------------------------------------------- */
const int muxTable[16][4] = {
// s0, s1, s2, s3 channel
{0, 0, 0, 0}, // 0
{1, 0, 0, 0}, // 1
{0, 1, 0, 0}, // 2
{1, 1, 0, 0}, // 3
{0, 0, 1, 0}, // 4
{1, 0, 1, 0}, // 5
{0, 1, 1, 0}, // 6
{1, 1, 1, 0}, // 7
{0, 0, 0, 1}, // 8
{1, 0, 0, 1}, // 9
{0, 1, 0, 1}, // 10
{1, 1, 0, 1}, // 11
{0, 0, 1, 1}, // 12
{1, 0, 1, 1}, // 13
{0, 1, 1, 1}, // 14
{1, 1, 1, 1} // 15
};
const int SIG = 36;// SIG pin
const int EN = 12;// Enable pin
const int controlPin[4] = {13, 15, 2, 17}; // 4 pins used for binary output
static inline void setCh(int ch) {
digitalWrite(controlPin[0], muxTable[ch][0]);
digitalWrite(controlPin[1], muxTable[ch][1]);
digitalWrite(controlPin[2], muxTable[ch][2]);
digitalWrite(controlPin[3], muxTable[ch][3]);
}
/* -------------------------------------------------------------------------- */
/* IR control */
/* -------------------------------------------------------------------------- */
const int IR = 27;
void setup() {
Serial.begin(115200);
tft.init();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(5);
// tft.setTextColor(TFT_WHITE, TFT_BLACK);
// tft.setFreeFont(&Orbitron_Light_24);
for(int i=0; i<4; i++)
{
pinMode(controlPin[i], OUTPUT);// set pin as output
digitalWrite(controlPin[i], HIGH); // set initial state as HIGH
}
pinMode(IR, OUTPUT);
digitalWrite(IR, HIGH);
pinMode(EN, OUTPUT);// set EN pin as output
digitalWrite(EN, LOW); // set EN in (enable pin) Low to activate the chip
// setCh(8);
// pinMode(SIG, OUTPUT);// set SIG pin as output
// digitalWrite(SIG, HIGH);
// delay(100);
setCh(0);
adcAttachPin(SIG);
// adcStart(SIG);
analogSetClockDiv(255); // 1338mS
// analogReadResolution(10); // Default of 12 is not very linear. Recommended to use 10 or 11 depending on needed resolution.
// analogSetAttenuation(ADC_6db); // Default is 11db which is very noisy. Recommended to use 2.5 or 6.
}
/********************************************************
*
* Input 135x240
*
*
* Output
* x = position of bar
* h = value
* color = color of bar
*
********************************************************/
static inline void draw_data(const uint16_t& x, const uint16_t& data, const uint16_t& old_data, const uint16_t& color) {
float max = 1023;
float min = 0;
const uint16_t rec_x = x;
const uint16_t rec_y = 0;
const uint16_t rec_h = 100;
const uint16_t rec_w = 240/16;
float pct=0;
/*******************
* Data value |
*******************/
float data_f = (float)(data);
float old_data_f = (float)old_data;
const uint16_t data_x = rec_x+2;
const uint16_t data_y = rec_y;
const uint16_t data_h = rec_h;
const uint16_t data_w = rec_w-4;
/***************************
* erase or write new line |
**************************/
pct = (float)(data_f-min)/(max-min);
int32_t targetpy = pct*data_h;
pct = (float)(old_data_f-min)/(max-min);
int32_t old_targetpy = pct*data_h;
while( targetpy != old_targetpy ) {
if( targetpy > old_targetpy ) {
tft.drawLine(data_x, data_y+old_targetpy, data_x+data_w, data_y+old_targetpy, TFT_GREENYELLOW);
old_targetpy++;
}else{
tft.drawLine(data_x, data_y+old_targetpy, data_x+data_w, data_y+old_targetpy, TFT_BLACK);
old_targetpy--;
}
}
}
void loop() {
int dat[16];
for(int i=0;i<16;i++) {
setCh(i);
delayMicroseconds(450);
dat[i] = analogRead(SIG);
}
static int old_h[16];
for(int i=0;i<16;i++) {
draw_data(i*240.0/16, dat[i], old_h[i], TFT_CYAN);
old_h[i]=dat[i];
}
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment