Skip to content

Commit 7fd29e3

Browse files
committed
added Serial command
1 parent eaf0862 commit 7fd29e3

File tree

2 files changed

+294
-12
lines changed

2 files changed

+294
-12
lines changed

MicroView.cpp

Lines changed: 267 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
// Change the total fonts included
1919
#define TOTALFONTS 7
20+
#define recvLEN 100
21+
char serInStr[recvLEN]; // TODO - need to fix a value so that this will not take up too much memory.
22+
uint8_t serCmd[recvLEN];
2023

2124
// Add the font name as declared in the header file. Remove as many as possible to get conserve FLASH memory.
2225
const unsigned char *MicroView::fontsPointer[]={
@@ -157,6 +160,7 @@ void MicroView::begin() {
157160

158161
command(DISPLAYON); //--turn on oled panel
159162
clear(ALL); // Erase hardware memory inside the OLED controller to avoid random data in memory.
163+
Serial.begin(115200);
160164
}
161165

162166
void MicroView::command(uint8_t c) {
@@ -662,21 +666,272 @@ size_t MicroView::write(uint8_t c) {
662666
command(0xFF);
663667
command(ACTIVATESCROLL);
664668
}
669+
670+
void MicroView::doCmd(uint8_t cmdCount) {
671+
// decode command
672+
switch (serCmd[0]) {
673+
case CMD_CLEAR: {
674+
Serial.println("clear");
675+
if (cmdCount==1) {
676+
clear(serCmd[1]);
677+
} else if (cmdCount==2) {
678+
clear(serCmd[1], serCmd[2]);
679+
}
680+
break;
681+
}
682+
683+
case CMD_INVERT: {
684+
Serial.println("invert");
685+
if (cmdCount==1) {
686+
invert(serCmd[1]);
687+
}
688+
break;
689+
}
690+
691+
case CMD_CONTRAST: {
692+
Serial.println("contrast");
693+
if (cmdCount==1) {
694+
contrast(serCmd[1]);
695+
}
696+
break;
697+
}
698+
699+
case CMD_DISPLAY: {
700+
Serial.println("display");
701+
if (cmdCount==0) {
702+
display();
703+
}
704+
break;
705+
}
706+
707+
case CMD_SETCURSOR: {
708+
Serial.println("setCursor");
709+
if (cmdCount==2) {
710+
setCursor(serCmd[1], serCmd[2]);
711+
}
712+
break;
713+
}
714+
715+
case CMD_PIXEL: {
716+
Serial.println("pixel");
717+
if (cmdCount==2) {
718+
pixel(serCmd[1],serCmd[2]);
719+
display();
720+
} else if (cmdCount=4) {
721+
pixel(serCmd[1],serCmd[2],serCmd[3],serCmd[4]);
722+
}
723+
break;
724+
}
725+
726+
case CMD_LINE: {
727+
Serial.println("line");
728+
if (cmdCount==4) {
729+
line(serCmd[1],serCmd[2],serCmd[3],serCmd[4]);
730+
display();
731+
} else if (cmdCount==6) {
732+
line(serCmd[1],serCmd[2],serCmd[3],serCmd[4],serCmd[5],serCmd[6]);
733+
display();
734+
}
735+
break;
736+
}
737+
738+
case CMD_LINEH: {
739+
Serial.println("lineH");
740+
if (cmdCount==3) {
741+
lineH(serCmd[1], serCmd[2], serCmd[3]);
742+
display();
743+
} else if (cmdCount==5) {
744+
lineH(serCmd[1], serCmd[2], serCmd[3], serCmd[4], serCmd[5]);
745+
display();
746+
}
747+
break;
748+
}
749+
750+
case CMD_LINEV: {
751+
Serial.println("lineV");
752+
if (cmdCount==3) {
753+
lineV(serCmd[1], serCmd[2], serCmd[3]);
754+
display();
755+
} else if (cmdCount==5) {
756+
lineV(serCmd[1], serCmd[2], serCmd[3], serCmd[4], serCmd[5]);
757+
display();
758+
}
759+
break;
760+
}
761+
762+
case CMD_RECT: {
763+
Serial.println("rect");
764+
if (cmdCount==4) {
765+
rect(serCmd[1], serCmd[2], serCmd[3], serCmd[4]);
766+
display();
767+
} else if (cmdCount==6) {
768+
rect(serCmd[1], serCmd[2], serCmd[3], serCmd[4], serCmd[5], serCmd[6]);
769+
display();
770+
}
771+
break;
772+
}
773+
774+
case CMD_RECTFILL: {
775+
Serial.println("rectFill");
776+
if (cmdCount==4) {
777+
rectFill(serCmd[1], serCmd[2], serCmd[3], serCmd[4]);
778+
display();
779+
} else if (cmdCount==6) {
780+
rectFill(serCmd[1], serCmd[2], serCmd[3], serCmd[4], serCmd[5], serCmd[6]);
781+
display();
782+
}
783+
break;
784+
785+
}
786+
787+
case CMD_CIRCLE: {
788+
Serial.println("circle");
789+
if (cmdCount==3) {
790+
circle(serCmd[1], serCmd[2], serCmd[3]);
791+
display();
792+
} else if (cmdCount==5) {
793+
circle(serCmd[1], serCmd[2], serCmd[3], serCmd[4], serCmd[5]);
794+
display();
795+
}
796+
break;
797+
}
798+
799+
case CMD_CIRCLEFILL: {
800+
Serial.println("circleFill");
801+
802+
if (cmdCount==3) {
803+
circleFill(serCmd[1], serCmd[2], serCmd[3]);
804+
display();
805+
} else if (cmdCount==5) {
806+
circleFill(serCmd[1], serCmd[2], serCmd[3], serCmd[4], serCmd[5]);
807+
display();
808+
}
809+
break;
810+
}
811+
812+
case CMD_DRAWCHAR: {
813+
Serial.println("drawChar");
814+
if (cmdCount==3) {
815+
drawChar(serCmd[1], serCmd[2], serCmd[3]);
816+
display();
817+
} else if (cmdCount==5) {
818+
drawChar(serCmd[1], serCmd[2], serCmd[3], serCmd[4], serCmd[5]);
819+
display();
820+
}
821+
break;
822+
}
665823

824+
case CMD_DRAWBITMAP: {
825+
// TODO
826+
827+
break;
828+
}
829+
830+
case CMD_GETLCDWIDTH: {
831+
Serial.println("getLCDWidth");
832+
833+
if (cmdCount==0) {
834+
Serial.println(getLCDWidth());
835+
}
836+
break;
837+
}
838+
839+
case CMD_GETLCDHEIGHT: {
840+
Serial.println("getLCDHeight");
841+
if (cmdCount==0) {
842+
Serial.println(getLCDHeight());
843+
}
844+
break;
845+
}
846+
847+
case CMD_SETCOLOR: {
848+
Serial.println("setColor");
849+
if (cmdCount==1) {
850+
setColor(serCmd[1]);
851+
}
852+
break;
853+
}
854+
855+
case CMD_SETDRAWMODE: {
856+
Serial.println("drawMode");
857+
if (cmdCount==1) {
858+
setDrawMode(serCmd[1]);
859+
}
860+
break;
861+
862+
}
863+
default:
864+
break;
865+
}
866+
}
867+
868+
void MicroView::checkComm(void) {
869+
int count = readSerial();
870+
char *result;
871+
uint8_t index=0;
872+
int temp;
873+
874+
if (count>0) {
875+
// process Serial data
876+
result=strtok(serInStr,",");
877+
if (result !=NULL) {
878+
temp=atoi(result);
879+
serCmd[index]=(uint8_t)temp & 0xff; // we only need 8 bit number
880+
index++;
881+
for (uint8_t i;i<recvLEN;i++) {
882+
result=strtok(NULL,",");
883+
if (result != NULL) {
884+
885+
temp=atoi(result);
886+
serCmd[index]=(uint8_t)temp & 0xff; // we only need 8 bit number
887+
index++;
888+
}
889+
else {
890+
break;
891+
}
892+
}
893+
/*
894+
// debug output
895+
Serial.print("command received=");
896+
Serial.println(index);
897+
for (uint8_t i=0;i<index;i++) {
898+
Serial.println(serCmd[i]);
899+
}
900+
*/
901+
}
902+
doCmd(index-1); // index-1 is the total parameters count of a command
903+
}
904+
}
905+
906+
int MicroView::readSerial(void)
907+
{
908+
int i=0;
909+
if(!Serial.available())
910+
return -1;
911+
912+
while (Serial.available()>0)
913+
{
914+
if( i < recvLEN)
915+
{
916+
serInStr[i++] = Serial.read();
917+
delay(2);
918+
}
919+
else
920+
break;
921+
}
922+
serInStr[i]='\0';
923+
return i;
924+
}
925+
926+
// -------------------------------------------------------------------------------------
927+
// MicroViewWidget Class - start
928+
// -------------------------------------------------------------------------------------
666929
MicroViewWidget::MicroViewWidget(uint8_t newx, uint8_t newy, int16_t min, int16_t max) {
667930
setX(newx);
668931
setY(newy);
669932
value=0;
670-
// if (min>max) {
671-
// setMinValue(max);
672-
// setMaxValue(min);
673-
// }
674-
// else {
675933
setMinValue(min);
676934
setMaxValue(max);
677-
// }
678-
//drawFace();
679-
//setValue(min);
680935
}
681936

682937
uint8_t MicroViewWidget::getX() { return x; }
@@ -698,6 +953,10 @@ size_t MicroView::write(uint8_t c) {
698953
this->draw();
699954
}
700955
}
956+
957+
// -------------------------------------------------------------------------------------
958+
// MicroViewWidget Class - end
959+
// -------------------------------------------------------------------------------------
701960

702961
// -------------------------------------------------------------------------------------
703962
// Slider Widget - start
@@ -836,7 +1095,6 @@ size_t MicroView::write(uint8_t c) {
8361095
draw();
8371096
}
8381097

839-
8401098
void MicroViewGauge::drawFace() {
8411099
uint8_t offsetX, offsetY, majorLine;
8421100
float degreeSec, fromSecX, fromSecY, toSecX, toSecY;

MicroView.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,27 @@
6464
#define VERTICALRIGHTHORIZONTALSCROLL 0x29
6565
#define VERTICALLEFTHORIZONTALSCROLL 0x2A
6666

67-
typedef enum CMD {CMD_CLEAR,CMD_INVERT,CMD_CONTRAST,CMD_DISPLAY,CMD_SETCURSOR,CMD_PIXEL,CMD_LINE,
68-
CMD_LINEH,CMD_LINEV,CMD_RECT, CMD_RECTFILL, CMD_CIRCLE,CMD_CIRCLEFILL, CMD_DRAWCHAR, CMD_DRAWBITMAP,
69-
CMD_GETLCDWIDTH, CMD_GETLCDHEIGHT,CMD_SETCOLOR, CMD_SETDRAWMODE} commCommand_t;
67+
typedef enum CMD {
68+
CMD_CLEAR,
69+
CMD_INVERT,
70+
CMD_CONTRAST,
71+
CMD_DISPLAY,
72+
CMD_SETCURSOR,
73+
CMD_PIXEL,
74+
CMD_LINE,
75+
CMD_LINEH,
76+
CMD_LINEV,
77+
CMD_RECT,
78+
CMD_RECTFILL,
79+
CMD_CIRCLE,
80+
CMD_CIRCLEFILL,
81+
CMD_DRAWCHAR,
82+
CMD_DRAWBITMAP,
83+
CMD_GETLCDWIDTH,
84+
CMD_GETLCDHEIGHT,
85+
CMD_SETCOLOR,
86+
CMD_SETDRAWMODE
87+
} commCommand_t;
7088

7189
class MicroView : public Print{
7290
public:
@@ -132,6 +150,10 @@ class MicroView : public Print{
132150
void scrollVertLeft(uint8_t start, uint8_t stop);
133151
void scrollStop(void);
134152

153+
// Communication
154+
void checkComm(void);
155+
void doCmd(uint8_t index);
156+
135157
private:
136158
//uint8_t cs;
137159
volatile uint8_t *mosiport, *sckport, *ssport, *dcport; // use volatile because these are fixed location port address
@@ -140,6 +162,8 @@ class MicroView : public Print{
140162
uint16_t fontMapWidth;
141163
//unsigned char *fontsPointer[TOTALFONTS];
142164
static const unsigned char *fontsPointer[];
165+
166+
int readSerial(void);
143167
};
144168

145169
class MicroViewWidget {

0 commit comments

Comments
 (0)