Ardino, MiniCom

이도현·2023년 8월 9일
0

아두이노 학습

목록 보기
13/34

1. MiniCom

  • SimpleTimer, 16x2 LCD의 간편 사용을 위한 클래스
// MiniCom.h
#pragam once

#include<Arduino.h>
#include<LiquidCrystal_I2C.h>
#include<SimpleTimer.h>

class MiniCom{
	long serial_bps; //Serial 속도
	LiquidCrystal_I2C lcd; // 1602 LCD
	SimpleTimer timer; //타이머
	bool no_lcd;  // LCD 사용 여부

public:
	MiniCom(long serial_bps = 115200, int lcd_addr = 0x27);
	void setNoLcd() {no_lcd = true;};
	void init();
	void run();

	int setInterval(unsigned long interval, timer_callback f);
	SimpleTimer& getTimer() {return timer; }

	// LCD 출력 지원 메서드(helper 함수)
	void print(int row, const char *pMsg);
	void print(int row, String title, int value);
	void print(int row, String title1, int value1, String title2, int value2);
	void print(int row, String title, double value);
	void print(int row, String tilte1, double value1,
							String title2, double value2);
};
// MiniCom.cpp
#include "MiniCom.h"

MiniCom::MiniCom(long serial_bps, int lcd_addr)
	:serial_bps(serial_bps), lcd(lcd_addr, 16, 2){
	no_lcd = false;
}

void MiniCom::init(){
	Serial.begin(serial_bps);
	if(!no_lcd){
		lcd.init();
		lcd.backlight();
	}
}

int MiniCom::setInterval(unsigned long interval, timer_callback f)
	return timer.setInterval(interval, f);
}

void MiniCom::run(){
	timer.run();
}

void MiniCom::print(int row, const char *pMsg) {
	if(no_lcd) return;
	char buf[17];
	sprintf(buf, "%-16s", pMsg);
	lcd.setCursor(0, row);
	lcd.print(buf);
}

void MiniCom::print(int row, String title, int value) {
	if(no_lcd) return;
	String buf = title + value;
	print(row, buf.c_str());
}
void MiniCom::print(int row, String title1, int value1, 
String title2, int value2) {
	if(no_lcd) return;
	String buf = title1 + value1 + "," + title2 + value2;
	print(row, buf.c_str());
}
void MiniCom::print(int row, String title, double value) {
	if(no_lcd) return;
	String buf = title + value;
	print(row, buf.c_str());
}
void MiniCom::print(int row, String title1, double value1, 
	String title2, double value2) {
	if(no_lcd) return;
	String buf = title1 + value1 + "," + title2 + value2;
	print(row, buf.c_str());
}
//arduino
#include "MiniCom.h"
#include <Led.h>

Led led(8);
MiniCom com;

void setup(){
	com.init();
	com.print(0, "[[MiniCom]]");
	com.setInterval(1000, check); // 0.1초 간격으로
}

void loop(){
	com.run();
}

void check(){
	int on = led.toggle();
	if(on){
		com.print(1,"led on");
	}
	else{
	com.print(1, "led off");
	}
}
profile
좋은 지식 나누어요

0개의 댓글