1D Wolframs Cellular Automata - uVGAII(SGC) 4DGL version 640x480 (slow but nice) pin p9 - rx, pin p10 - tx, pin p11 - rst

Dependencies:   mbed

Committer:
JLS
Date:
Sat Sep 03 14:11:03 2011 +0000
Revision:
0:60aeb7163b27

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JLS 0:60aeb7163b27 1 #include "mbed.h"
JLS 0:60aeb7163b27 2
JLS 0:60aeb7163b27 3 Serial _cmd (p9, p10);
JLS 0:60aeb7163b27 4 DigitalOut _rst (p11);
JLS 0:60aeb7163b27 5
JLS 0:60aeb7163b27 6 void startVGA();
JLS 0:60aeb7163b27 7 void baudrate();
JLS 0:60aeb7163b27 8 void cls();
JLS 0:60aeb7163b27 9 void pixel(int, int, int);
JLS 0:60aeb7163b27 10 int writeCOM(char *, int);
JLS 0:60aeb7163b27 11 void DisplayState(int*,int, int);
JLS 0:60aeb7163b27 12
JLS 0:60aeb7163b27 13
JLS 0:60aeb7163b27 14 int main() {
JLS 0:60aeb7163b27 15
JLS 0:60aeb7163b27 16 startVGA ();
JLS 0:60aeb7163b27 17
JLS 0:60aeb7163b27 18 int iteration = 480;
JLS 0:60aeb7163b27 19 int lenght = 640;
JLS 0:60aeb7163b27 20
JLS 0:60aeb7163b27 21 int rules[8] = {1,1,1,0,1,0,0,1};
JLS 0:60aeb7163b27 22
JLS 0:60aeb7163b27 23 int row = 0;
JLS 0:60aeb7163b27 24
JLS 0:60aeb7163b27 25 int i,j,k;
JLS 0:60aeb7163b27 26 int state[lenght];
JLS 0:60aeb7163b27 27 int newstate[lenght];
JLS 0:60aeb7163b27 28
JLS 0:60aeb7163b27 29 while(1) {
JLS 0:60aeb7163b27 30
JLS 0:60aeb7163b27 31 for (i=0;i<lenght;i++)
JLS 0:60aeb7163b27 32
JLS 0:60aeb7163b27 33 state[i]= int (rand()%2);
JLS 0:60aeb7163b27 34
JLS 0:60aeb7163b27 35 for (i=0;i<iteration;i++) {
JLS 0:60aeb7163b27 36
JLS 0:60aeb7163b27 37 for (j=0;j<lenght;j++)
JLS 0:60aeb7163b27 38 newstate[j] = 0;
JLS 0:60aeb7163b27 39
JLS 0:60aeb7163b27 40 for (j=0;j<lenght;j++) {
JLS 0:60aeb7163b27 41 k = 4*state[(j-1+lenght)%lenght] + 2*state[j] + state[(j+1)%lenght];
JLS 0:60aeb7163b27 42 newstate[j] = rules[k];
JLS 0:60aeb7163b27 43 }
JLS 0:60aeb7163b27 44
JLS 0:60aeb7163b27 45 for (j=0;j<lenght;j++)
JLS 0:60aeb7163b27 46 state[j] = newstate[j];
JLS 0:60aeb7163b27 47
JLS 0:60aeb7163b27 48 DisplayState(state,lenght,row);
JLS 0:60aeb7163b27 49
JLS 0:60aeb7163b27 50 row = row + 1;
JLS 0:60aeb7163b27 51
JLS 0:60aeb7163b27 52 if (row == 480) {
JLS 0:60aeb7163b27 53
JLS 0:60aeb7163b27 54 row = 0;
JLS 0:60aeb7163b27 55
JLS 0:60aeb7163b27 56 }
JLS 0:60aeb7163b27 57
JLS 0:60aeb7163b27 58 }
JLS 0:60aeb7163b27 59
JLS 0:60aeb7163b27 60 }
JLS 0:60aeb7163b27 61 }
JLS 0:60aeb7163b27 62
JLS 0:60aeb7163b27 63
JLS 0:60aeb7163b27 64 void DisplayState(int s[],int len, int row)
JLS 0:60aeb7163b27 65 {
JLS 0:60aeb7163b27 66 int i;
JLS 0:60aeb7163b27 67
JLS 0:60aeb7163b27 68 for (i=0;i<len;i++) {
JLS 0:60aeb7163b27 69 if (s[i] == 1){
JLS 0:60aeb7163b27 70
JLS 0:60aeb7163b27 71 pixel (i,row,65534);
JLS 0:60aeb7163b27 72
JLS 0:60aeb7163b27 73 }
JLS 0:60aeb7163b27 74
JLS 0:60aeb7163b27 75 else{
JLS 0:60aeb7163b27 76
JLS 0:60aeb7163b27 77 pixel (i,row,0);
JLS 0:60aeb7163b27 78
JLS 0:60aeb7163b27 79 }
JLS 0:60aeb7163b27 80
JLS 0:60aeb7163b27 81 }
JLS 0:60aeb7163b27 82
JLS 0:60aeb7163b27 83 }
JLS 0:60aeb7163b27 84
JLS 0:60aeb7163b27 85 void startVGA () {
JLS 0:60aeb7163b27 86
JLS 0:60aeb7163b27 87 _rst = 1;
JLS 0:60aeb7163b27 88 _rst = 0;
JLS 0:60aeb7163b27 89 wait_ms(1);
JLS 0:60aeb7163b27 90 _rst = 1;
JLS 0:60aeb7163b27 91 wait(3);
JLS 0:60aeb7163b27 92
JLS 0:60aeb7163b27 93 while (_cmd.readable()) _cmd.getc();
JLS 0:60aeb7163b27 94
JLS 0:60aeb7163b27 95 char autobaud[1] = "";
JLS 0:60aeb7163b27 96 autobaud[0] = '\x55';
JLS 0:60aeb7163b27 97 writeCOM(autobaud, 1);
JLS 0:60aeb7163b27 98
JLS 0:60aeb7163b27 99 baudrate();
JLS 0:60aeb7163b27 100
JLS 0:60aeb7163b27 101 cls();
JLS 0:60aeb7163b27 102
JLS 0:60aeb7163b27 103 char dispctr[3]= "";
JLS 0:60aeb7163b27 104 dispctr[0] = '\x59';
JLS 0:60aeb7163b27 105 dispctr[1] = 0x0c;
JLS 0:60aeb7163b27 106 dispctr[2] = 0x01;
JLS 0:60aeb7163b27 107 writeCOM(dispctr, 3);
JLS 0:60aeb7163b27 108
JLS 0:60aeb7163b27 109 }
JLS 0:60aeb7163b27 110
JLS 0:60aeb7163b27 111
JLS 0:60aeb7163b27 112 void pixel(int x, int y, int color) {
JLS 0:60aeb7163b27 113
JLS 0:60aeb7163b27 114 char pixel[7]= "";
JLS 0:60aeb7163b27 115
JLS 0:60aeb7163b27 116 pixel[0] = '\x50';
JLS 0:60aeb7163b27 117
JLS 0:60aeb7163b27 118 pixel[1] = (x >> 8) & 0xFF;
JLS 0:60aeb7163b27 119 pixel[2] = x & 0xFF;
JLS 0:60aeb7163b27 120
JLS 0:60aeb7163b27 121 pixel[3] = (y >> 8) & 0xFF;
JLS 0:60aeb7163b27 122 pixel[4] = y & 0xFF;
JLS 0:60aeb7163b27 123
JLS 0:60aeb7163b27 124 pixel[5] = (color >> 8) & 0xFF;
JLS 0:60aeb7163b27 125 pixel[6] = color & 0xFF;
JLS 0:60aeb7163b27 126
JLS 0:60aeb7163b27 127 writeCOM(pixel, 7);
JLS 0:60aeb7163b27 128 }
JLS 0:60aeb7163b27 129
JLS 0:60aeb7163b27 130
JLS 0:60aeb7163b27 131 void baudrate() {
JLS 0:60aeb7163b27 132
JLS 0:60aeb7163b27 133 char baudrate[2]= "";
JLS 0:60aeb7163b27 134
JLS 0:60aeb7163b27 135 baudrate[0] = '\x51';
JLS 0:60aeb7163b27 136 baudrate[1] = '\x0F';
JLS 0:60aeb7163b27 137
JLS 0:60aeb7163b27 138 int i, resp = 0;
JLS 0:60aeb7163b27 139
JLS 0:60aeb7163b27 140 while (_cmd.readable()) _cmd.getc();
JLS 0:60aeb7163b27 141
JLS 0:60aeb7163b27 142 long speed = speed = 281000;
JLS 0:60aeb7163b27 143
JLS 0:60aeb7163b27 144 for (i = 0; i <2; i++) _cmd.putc(baudrate[i]);
JLS 0:60aeb7163b27 145 _cmd.baud(speed);
JLS 0:60aeb7163b27 146
JLS 0:60aeb7163b27 147 while (!_cmd.readable()) wait_ms(1);
JLS 0:60aeb7163b27 148
JLS 0:60aeb7163b27 149 if (_cmd.readable()) resp = _cmd.getc();
JLS 0:60aeb7163b27 150 switch (resp) {
JLS 0:60aeb7163b27 151 case '\x06' :
JLS 0:60aeb7163b27 152 resp = 1;
JLS 0:60aeb7163b27 153 break;
JLS 0:60aeb7163b27 154 case '\x15' :
JLS 0:60aeb7163b27 155 resp = -1;
JLS 0:60aeb7163b27 156 break;
JLS 0:60aeb7163b27 157 default :
JLS 0:60aeb7163b27 158 resp = 0;
JLS 0:60aeb7163b27 159 break;
JLS 0:60aeb7163b27 160 }
JLS 0:60aeb7163b27 161
JLS 0:60aeb7163b27 162 }
JLS 0:60aeb7163b27 163
JLS 0:60aeb7163b27 164
JLS 0:60aeb7163b27 165 int writeCOM(char *command, int number) {
JLS 0:60aeb7163b27 166
JLS 0:60aeb7163b27 167 int i, resp = 0;
JLS 0:60aeb7163b27 168
JLS 0:60aeb7163b27 169 while (_cmd.readable()) _cmd.getc();
JLS 0:60aeb7163b27 170
JLS 0:60aeb7163b27 171 for (i = 0; i < number; i++) _cmd.putc(command[i]);
JLS 0:60aeb7163b27 172
JLS 0:60aeb7163b27 173 while (!_cmd.readable()) wait_ms(1);
JLS 0:60aeb7163b27 174 if (_cmd.readable()) resp = _cmd.getc();
JLS 0:60aeb7163b27 175 switch (resp) {
JLS 0:60aeb7163b27 176 case '\x06' :
JLS 0:60aeb7163b27 177 resp = 1;
JLS 0:60aeb7163b27 178 break;
JLS 0:60aeb7163b27 179 case '\x15' :
JLS 0:60aeb7163b27 180 resp = -1;
JLS 0:60aeb7163b27 181 break;
JLS 0:60aeb7163b27 182 default :
JLS 0:60aeb7163b27 183 resp = 0;
JLS 0:60aeb7163b27 184 break;
JLS 0:60aeb7163b27 185 }
JLS 0:60aeb7163b27 186
JLS 0:60aeb7163b27 187 return resp;
JLS 0:60aeb7163b27 188 }
JLS 0:60aeb7163b27 189
JLS 0:60aeb7163b27 190
JLS 0:60aeb7163b27 191 void cls() {
JLS 0:60aeb7163b27 192 char cls[1] = "";
JLS 0:60aeb7163b27 193 cls[0] = '\x45';
JLS 0:60aeb7163b27 194 writeCOM(cls, 1);
JLS 0:60aeb7163b27 195 }