/* vim: noexpandtab fileencoding=utf-8 nomodified nowrap textwidth=200 foldmethod=marker foldmarker={{{,}}} foldcolumn=4 ruler showcmd lcs=tab\:|- list: */ /* PinSet - enables to set any pin on arduino Mega value: 0; 1; v pulldown; ^ pullup pin0: 2..53 for digital pins, A0-A15 for analog pin if pin0 is Analog, then pin1 is analog too (and is typed without A) syntax: : set pin0 to value :.. set pin0..pin1 to value examples: ^:10 set pin D10 to weak pullup (INPUT_PULLUP) - if it is input on 74HCxx, then it counts as log. 1; if it is output, it will be driven to output value 1:13 set pin 13 to logical 1 (digitalWrite(HIGH);) - LED will be lit, input is logical 1, if it is output and driven toward zero, then it may destroy something 0:2..5 set pins 2..5 to logical 0 (digitalWrite(LOW);) v:A5 set analog 5 to weak pulldown (well set it as INPUT and hope for external resistors) v:A0..15 set all analogs to weak pulldown // ShieldMega-001 have pulldown resistors for all zif40 pins */ #include char dig[54],an[16]; int A[]={ A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15 }; void setup() { for (int i=2; i<54;++i){ // 0,1 Serial, nedotýkati se! pinMode(i,INPUT); // digitalWrite(i,LOW); dig[i]='v'; }; for (int i=0; i<15;++i){ pinMode(A[i],INPUT); // digitalWrite(A[i],LOW); an[i]='v'; }; //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // prints title with ending line break delay(20); Serial.println("-= =-"); Serial.println("PinSet v0.0"); } void mezery(int i) { // {{{ if (i % 10 == 0) Serial.print(" | "); else if (i % 5 == 0) Serial.print(' '); } // }}} bool st(uint8_t x, char y) { // {{{ status of pin - x actual value, y set value, prints "char", returns if it match if (x==0) { if (y=='0' || y=='v' ) {Serial.print(y); return true;}; if (y=='1' || y=='^' ) {Serial.print("L"); return false;}; // unexpected L }; if (x==1) { if (y=='1' || y=='^' ) {Serial.print(y); return true;}; if (y=='0' || y=='v' ) {Serial.print("H"); return false;}; // unexpected H }; Serial.print("WTF st("); Serial.print(x); Serial.print(", '"); Serial.print(y); Serial.println("')"); return false; // Should be unreachable } // }}} void status() { // {{{ uint8_t x; char y; bool ok = true; bool b; // {{{ zahlavi Serial.println(); for (int i=0; i<54;++i){ mezery(i); Serial.print(i/10); }; Serial.println(); for (int i=0; i<54;++i){ mezery(i); Serial.print(i%10); }; Serial.println(); // }}} Serial.println("Digital:"); for (int i=0; i<2;++i){ mezery(i); Serial.print('s'); // Serial }; for (int i=2; i<54;++i){ mezery(i); x=digitalRead(i); y=dig[i]; b= st(x,y); ok &= b; }; Serial.println(); Serial.println("Analog:"); for (int i=0; i<15;++i){ mezery(i); x=digitalRead(an[i]); y=an[i]; b = st(x,y); ok &= b; }; Serial.println(); Serial.print("[01^v][:=]A?[0-9][0-9]((-/..)[0-9][0-9]?) >"); } // }}} char line[80+2]; int linelen; void err(int linepos, const char *msg) { // {{{ if (linepos>1) { for (int i=1; i='0' && c<='9') {pin0 = (c-'0'); c=line[linepos++]; // pin0 if (c>='0' && c<='9') {pin0 = 10*pin0 + (c-'0'); c=line[linepos++];}; }; if ((isA && pin0<16) || (!isA && pin0>1 && pin0<54)) { /* OK */ } else {err(linepos, "not valid pin0 number"); return;}; if (c==0) { pin1=pin0; } else { if (c=='-') {c=line[linepos++];} else if (c=='.') {c=line[linepos++]; if (c=='.') {c=line[linepos++];} else { err(linepos,".. expected"); return;}; } else { err(linepos,"-/.. expected"); return;}; if (c>='0' && c<='9') {pin1 = (c-'0'); c=line[linepos++]; // pin1 if (c>='0' && c<='9') {pin1 = 10*pin1 + (c-'0'); c=line[linepos++];}; }; if ((isA && pin1<16) || (!isA && pin1>1 && pin1<54)) { /* OK */ } else {err(linepos, "not valid pin1 number"); return;}; }; if (pin0>pin1) {err(linepos, "pin1 must be same or larger, than pin0");return;}; if (c!=0 && c!='\n') {err(linepos, "extra characters on line"); return;}; for (int i=pin0; i<=pin1; ++i) { int x=i; if (isA) { x=A[i]; an[i]=val;} else {dig[i]=val;}; if (val=='0') {pinMode(x,OUTPUT);digitalWrite(x,LOW); } else if (val=='1') {pinMode(x,OUTPUT);digitalWrite(x,HIGH); } else if (val=='v') {pinMode(x,INPUT); } else if (val=='^') {pinMode(x,INPUT_PULLUP); } else { err(linepos, "WTF, unknown val"); return; }; Serial.print(val); Serial.print("->"); Serial.print(x); Serial.println(";"); }; Serial.println("OK"); } // }}} int readBytesUntil(char const *terminators, char *buffer, size_t &buffer_top, size_t length) { // {{{ similar to Serial.* but waits for terminator/length and returns -1 until done (buffer_top is zeroed when done) if (buffer_top >= length) { buffer_top=0; return -1;}; if (buffer_top + 1 == length) { buffer_top=0; return length-1;}; while (Serial.available() > 0) { int b=Serial.read(); /* Serial.print(char(b)); Serial.print("["); Serial.print(b); Serial.print("]"); */ char const *t=terminators; while (*t && *t!=b) t++; if (*t) { uint8_t retval; retval=buffer_top; buffer[buffer_top]=0;buffer_top=0;return retval;}; buffer[buffer_top]=b; buffer_top++; buffer[buffer_top]=0; if (buffer_top >= length) { uint8_t retval; retval=buffer_top; buffer[buffer_top]=0;buffer_top=0;return retval;}; }; return -1; } // }}} size_t linetop=0; void loop() { linelen=readBytesUntil("\r\n", line, linetop, 80); if (linelen>0) { line[linelen]=0; solveLine(); status(); } else if (linelen==0) { Serial.print("# LineLen zero - "); Serial.println(linelen); }; }