/* LucidTronix Arduino HSB RGB Color library
* http://www.lucidtronix.com/tutorials/9
* Control rgb color by specifying
* Hue saturation and brightness.
* This code shows a rainbow
*/
#include <ST7735.h>
#include <SPI.h>
#define cs 10 // for MEGAs you probably want this to be pin 53
#define dc 9
#define rst 8 // you can also connect this to the Arduino reset
ST7735 tft = ST7735(cs, dc, rst);
const int scanline_size = 20;
uint8_t scanline[scanline_size];
uint8_t nextline[scanline_size];
int cur_rule = 0;
const int num_rules = 8;
boolean rules[num_rules][8] = {
{ 0,1,0,1,1,0,1,0},
{ 0,0,0,1,1,1,1,0},
{ 1,0,1,1,0,1,1,0},
{ 0,1,1,1,1,1,1,0},
{ 0,1,0,1,1,1,1,0},
{ 1,1,0,1,1,0,1,1},
{ 0,1,1,1,1,0,1,0},
{ 0,1,0,1,1,0,1,0}
};
int btnPin0 = 0;
int btnPin1 = 1;
int cur_y = 0;
int mode = 0;
int num_modes = 4;
unsigned int last_press = 0;
int background_color = 0xFF;
int color = 0xFF;
int backlight_pwm_pin = 3;
void setup(){
pinMode(btnPin1,INPUT);
pinMode(btnPin0,INPUT);
tft.initR(); // initialize a ST7735R chip
tft.writecommand(ST7735_DISPON);
pinMode(backlight_pwm_pin,OUTPUT);
digitalWrite(backlight_pwm_pin, HIGH);
delay(1000);
delay(500);
int bx = 0;
int by = 0;
delay(2000);
for (int i = 0; i < scanline_size; i++) {
scanline[i] = 0;
if ( i == scanline_size/2) scanline[i] = 0x08;
}
}
void loop(){
for (int i = 0; i < (scanline_size*8)-1; i++) {
int left = 0;
if (i > 0) left = get_bit_from_index(i-1); // Left neighbor state
int me = get_bit_from_index(i); // Current state
int right = get_bit_from_index(i+1); // Right neighbor state
boolean abit = get_bit_from_rules(left,me,right); // Compute next generation state based on ruleset
set_bit_from_index(i, abit);
if (mode == 0){
int cur_r = 15;
int cur_g = 2;
int cur_b = random(20,28);
background_color = cur_r | (cur_g << 5) | (cur_b << 11);
cur_r = 30;
cur_g = 62;
cur_b = 0;
color = cur_r | (cur_g << 5) | (cur_b << 11);
}else if (mode == 1){
int cur_r = 0;
int cur_g = 0;
int cur_b = 0;
background_color = cur_r | (cur_g << 5) | (cur_b << 11);
cur_r = random(3,30);
cur_g = 62;
cur_b = 30;
color = cur_r | (cur_g << 5) | (cur_b << 11);
}else if (mode == 2){
int cur_r = 5;
int cur_g = random(50,62);
int cur_b = 15;
background_color = cur_r | (cur_g << 5) | (cur_b << 11);
cur_r = 19;
cur_g = 2;
cur_b = 30;
color = cur_r | (cur_g << 5) | (cur_b << 11);
}
if (abit ) tft.drawPixel(cur_y, i, color);
else tft.drawPixel(cur_y, i, background_color);
if( digitalRead(btnPin1) == HIGH && millis() - last_press > 500 ){
cur_rule = ++cur_rule%num_rules;
for (int i = 0; i < scanline_size; i++) {
nextline[i] = 0;
if ( i == scanline_size/2) nextline[i] = 0x08;
}
last_press = millis();
}
if( digitalRead(btnPin0) == HIGH ){
tft.fillScreen(background_color);
for (int i = 0; i < scanline_size; i++) {
nextline[i] = 0;
if ( i == scanline_size/2) nextline[i] = 0x08;
}
mode = ++mode%num_modes;
}
}
cur_y++;
if (cur_y > 128) cur_y = 0;
for (int i = 0; i < scanline_size; i++) {
scanline[i] = nextline[i];
}
}
void testdrawtext(int ax, int ay, char *text, uint16_t color) {
tft.drawString(ax, ay, text, color);
}
boolean get_bit_from_index(int index){
int byte_index = index / 8;
int bit_index = 7 - (index % 8);
byte cur_b = scanline[byte_index];
if ( (cur_b & (1 << bit_index)) != 0x00 ) return 1;
else return 0;
}
void set_bit_from_index(int index, boolean abit){
int byte_index = index / 8;
int bit_index = 7 - (index % 8);
byte cur_b = nextline[byte_index];
if ( abit) cur_b |= (1 << bit_index);
else cur_b &= ~(1 << bit_index) ;
nextline[byte_index] = cur_b;
}
int get_bit_from_rules(int a, int b, int c) {
if (a == 1 && b == 1 && c == 1) return rules[cur_rule][0];
if (a == 1 && b == 1 && c == 0) return rules[cur_rule][1];
if (a == 1 && b == 0 && c == 1) return rules[cur_rule][2];
if (a == 1 && b == 0 && c == 0) return rules[cur_rule][3];
if (a == 0 && b == 1 && c == 1) return rules[cur_rule][4];
if (a == 0 && b == 1 && c == 0) return rules[cur_rule][5];
if (a == 0 && b == 0 && c == 1) return rules[cur_rule][6];
if (a == 0 && b == 0 && c == 0) return rules[cur_rule][7];
return 0;
}
Comments: