Skip to content

Instantly share code, notes, and snippets.

@AnthonyDiGirolamo
Created March 26, 2012 15:25
Show Gist options
  • Save AnthonyDiGirolamo/2205877 to your computer and use it in GitHub Desktop.
Save AnthonyDiGirolamo/2205877 to your computer and use it in GitHub Desktop.
time.h testing
#include <Time.h>
#include <Wire.h>
#include <LibTemperature.h>
LibTemperature temp = LibTemperature(0);
//echo T$(($(date +%s)+60*60*-4)) > /dev/tty.usbserial-A4006DN3
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
#include <SPI.h>
#include <string.h>
#include <avr/pgmspace.h>
//#include <avr/sleep.h>
//#include <JeeLib.h>
#define CS1 9 //ss
#define CS2 8 //ss
#define DATAOUT 11 //MOSI
#define DATAIN 12 //MISO
#define CLK 13 //sck
#define CHECKMALLOC(var) if((var)==NULL){abort();}
#define POUND 24
#define YEN 25
#define AE 26
#define DEG 27
#define HALFM 28
#define CROSS 29
#define ROCKETUP 30
#define ROCKETDOWN 31
#define DEL 127
/*
- - A B
|\|/| CDEFG
- - H I
|/|\| JKLMN
- - . O P Q
0x20 0x28
bOAGNPJCH bQIDEFMLK
*/
prog_uint8_t character_map[][2] = {
{ B00000000, B00000000 }, // space
{ B00000000, B00000000 }, // ! 32
{ B00000000, B00000000 }, // "
{ B00000000, B00000000 }, // #
{ B00000000, B00000000 }, // $
{ B00000000, B00000000 }, // %
{ B00000000, B00000000 }, // &
{ B00000000, B00000000 }, // '
{ B00000000, B00000000 }, // (
{ B00000000, B00000000 }, // )
{ B00000000, B00000000 }, // *
{ B00000000, B00000000 }, // +
{ B00000000, B00000000 }, // ,
{ B00000000, B00000000 }, // -
{ B00000000, B00000000 }, // .
{ B00000000, B00000000 }, // /
{ B00000000, B00000000 }, // 0
{ B00000000, B00000000 }, // 1
{ B00000000, B00000000 }, // 2
{ B00000000, B00000000 }, // 3
{ B00000000, B00000000 }, // 4
{ B00000000, B00000000 }, // 5
{ B00000000, B00000000 }, // 6
{ B00000000, B00000000 }, // 7
{ B00000000, B00000000 }, // 8
{ B00000000, B00000000 }, // 9
{ B00000000, B00000000 }, // :
{ B00000000, B00000000 }, // ;
{ B00000000, B00000000 }, // <
{ B00000000, B00000000 }, // =
{ B00000000, B00000000 }, // >
{ B00000000, B00000000 }, // ?
{ B00000000, B00000000 }, // @
{ B10001101, B00000010 }, // A
{ B10000111, B00000010 }, // B
{ B10000101, B00000000 }, // C
{ B10000101, B00010010 }, // D
{ B10000101, B00000001 }, // E
{ B01000111, B00000000 }, // F
{ B11000011, B00010010 }, // G
{ B00000111, B00000010 }, // H
{ B00000100, B00000000 }, // I
{ B10000000, B00000010 }, // J
{ B00000000, B01010110 }, // K
{ B00001000, B00010010 }, // L
{ B00010101, B01000010 }, // M
{ B00000101, B00000010 }, // N
{ B10000101, B00000010 }, // O
{ B01000111, B00010000 }, // P
{ B01001011, B00010010 }, // Q
{ B00000101, B00000000 }, // R
{ B00001000, B01000100 }, // S
{ B00001001, B01010010 }, // T
{ B10000100, B00000010 }, // U
{ B00000100, B00000001 }, // V
{ B10011100, B00000010 }, // W
{ B00000000, B00101101 }, // X
{ B10000011, B00010010 }, // Y
{ B10000001, B00000001 }, // Z
{ B00000000, B00000000 }, // [
{ B00000000, B00000000 }, // backslash
{ B00000000, B00000000 }, // ]
{ B00000000, B00000000 }, // ^
{ B00000000, B00000000 }, // _
{ B00000000, B00000000 } // ` 96
// { 123
// |
// }
// ~
};
/*
void read_max6954(byte address){
Serial.print(address, HEX);
Serial.print(" = ");
digitalWrite(CS1, LOW);
SPI.transfer(address | B10000000);
SPI.transfer(0x00);
digitalWrite(CS1, HIGH);
digitalWrite(CS1, LOW);
SPI.transfer(address);
Serial.println(SPI.transfer(0x00), BIN);
digitalWrite(CS1, HIGH);
}
*/
void write_max6954(byte address, byte value) {
digitalWrite(CS1, LOW);
digitalWrite(CS2, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS1, HIGH);
digitalWrite(CS2, HIGH);
}
void write_max6954a(byte address, byte value) {
digitalWrite(CS1, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS1, HIGH);
}
void write_max6954b(byte address, byte value) {
digitalWrite(CS2, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS2, HIGH);
}
void write_max6954(byte address, byte value, int chip) {
digitalWrite(chip, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(chip, HIGH);
}
void control_intensity(int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7, int chip=0) {
if (chip==CS1) {
write_max6954a(0x10, d1 << 4 | d0);
write_max6954a(0x11, d3 << 4 | d2);
write_max6954a(0x12, d5 << 4 | d4);
write_max6954a(0x13, d7 << 4 | d6);
write_max6954a(0x14, d1 << 4 | d0);
write_max6954a(0x15, d3 << 4 | d2);
write_max6954a(0x16, d5 << 4 | d4);
write_max6954a(0x17, d7 << 4 | d6);
} else if (chip==CS2) {
write_max6954b(0x10, d1 << 4 | d0);
write_max6954b(0x11, d3 << 4 | d2);
write_max6954b(0x12, d5 << 4 | d4);
write_max6954b(0x13, d7 << 4 | d6);
write_max6954b(0x14, d1 << 4 | d0);
write_max6954b(0x15, d3 << 4 | d2);
write_max6954b(0x16, d5 << 4 | d4);
write_max6954b(0x17, d7 << 4 | d6);
} else {
write_max6954(0x10, d1 << 4 | d0);
write_max6954(0x11, d3 << 4 | d2);
write_max6954(0x12, d5 << 4 | d4);
write_max6954(0x13, d7 << 4 | d6);
write_max6954(0x14, d1 << 4 | d0);
write_max6954(0x15, d3 << 4 | d2);
write_max6954(0x16, d5 << 4 | d4);
write_max6954(0x17, d7 << 4 | d6);
}
}
void write_string(char str[], int row = 0){
int i=0;
for (int digit = 0x20; digit <= 0x27; digit++) {
if (str[i+1] == '.') {
if (row==0)
write_max6954a(digit, str[i] | B10000000);
else
write_max6954b(digit, str[i] | B10000000);
i+=2;
}
else {
if (row==0)
write_max6954a(digit, str[i]);
else
write_max6954b(digit, str[i]);
i++;
}
}
}
void write_string1(char str[]){
write_max6954a(0x20, str[0]);
write_max6954a(0x21, str[1]);
write_max6954a(0x22, str[2]);
write_max6954a(0x23, str[3]);
write_max6954a(0x24, str[4]);
write_max6954a(0x25, str[5]);
write_max6954a(0x26, str[6]);
write_max6954a(0x27, str[7]);
}
void write_string2(char str[]){
write_max6954b(0x20, str[0]);
write_max6954b(0x21, str[1]);
write_max6954b(0x22, str[2]);
write_max6954b(0x23, str[3]);
write_max6954b(0x24, str[4]);
write_max6954b(0x25, str[5]);
write_max6954b(0x26, str[6]);
write_max6954b(0x27, str[7]);
}
void write_int(int value, int row=0){
char buffer[16];
int column = 0x20;
ultoa(value, buffer, 10);
int len = strlen(buffer);
for (int i=0; i<len; i++) {
if (row==0)
write_max6954a(column++, buffer[i]);
else
write_max6954b(column++, buffer[i]);
}
}
void setup_max6954() {
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(CLK, OUTPUT);
pinMode(CS1, OUTPUT);
pinMode(CS2, OUTPUT);
digitalWrite(CS1,HIGH); //disable device
digitalWrite(CS2,HIGH); //disable device
SPI.begin();
// SPI.setBitOrder(MSBFIRST);
// SPI.setDataMode(SPI_MODE0);
// SPI Speed = 1MHz
SPI.setClockDivider(SPI_CLOCK_DIV8); // 8MHz Arduino
// SPI.setClockDivider(SPI_CLOCK_DIV16); // 16MHz Arduino
// SPI_CLOCK_DIV possibilities 2,4,8,16,32,64,128
// Decode mode enabled
write_max6954(0x01, B11111111);
// Scan Limit - all digits
write_max6954(0x03, B00000111);
// Configuration - fast blinking and normal operation, invidual digit brightness
//write_max6954(0x04, B01000101);
// Configuration - fast blinking and normal operation, global brightness
write_max6954(0x04, B00000101);
// Digit Type - all 16 segments
write_max6954(0x0C, B00000000);
}
void set_global_brightness(int i) {
if (i>15) i=15;
if (i<0) i=0;
// Global Intensity to one
write_max6954(0x02, i);
}
/*
void individual_segment_test() {
// Decode mode disabled
write_max6954(0x01, B00000000);
// Turn off all segments manually
for (int i=0x20; i<=0x2F; i++) {
write_max6954(i, B00000000);
}
// Turn on all segments manually (decode mode off)
// write_max6954(0x20, B11111111);
// write_max6954(0x28, B11111111);
// delay(5000);
int dl = 40;
for (int i=0; i<4; i++) {
write_max6954(0x20,B00000001);
delay(dl);
write_max6954(0x20,B00000000);
write_max6954(0x28,B00100000);
delay(dl);
write_max6954(0x28,B00010000);
delay(dl);
write_max6954(0x28,B00001000);
delay(dl);
write_max6954(0x28,B01000000);
delay(dl);
write_max6954(0x28,B00000100);
delay(dl);
write_max6954(0x28,B00000010);
delay(dl);
write_max6954(0x28,B00000001);
delay(dl);
}
for (int i=0x20; i<=0x2F; i++) {
write_max6954(i, B00000000);
}
// Test each segment bit slowly
// dl = 3000;
// int segment = B10000000;
// for (int i=0; i<8; i++) {
// write_max6954(0x20,segment);
// segment = segment >> 1;
// delay(dl);
// }
// write_max6954(0x20,B00000000);
// segment = B10000000;
// for (int i=0; i<8; i++) {
// write_max6954(0x28,segment);
// segment = segment >> 1;
// delay(dl);
// }
// write_max6954(0x28,B00000000);
// Decode mode enabled
write_max6954(0x01, B11111111);
}
*/
#define EXPRSIZE 128
#define LCDCOLS 8
char expression[EXPRSIZE];
int expr_size;
int lcd_c;
int lcd_r;
int light_avg;
//char c;
int b = 0;
float t = 0.0;
//int window_start, window_end;
int dl = 70;
char buffer[64];
char lower_test[] PROGMEM = "abcdefghijklmnop";
char lower_test2[] PROGMEM = "qrstuvwxyz ";
char lower_test3[] PROGMEM = "mitchellgriffith";
void led_print(char* string, int row, int col) {
strcpy_P(buffer, string);
}
void write_lowercase(char c, int row=0, int col=0) {
int offset = 32;
if (c >= 'a')
offset = 64;
if (row==0) {
write_max6954a(0x20+col, character_map[c-offset][0]);
write_max6954a(0x28+col, character_map[c-offset][1]);
}
else {
write_max6954b(0x20+col, character_map[c-offset][0]);
write_max6954b(0x28+col, character_map[c-offset][1]);
}
}
void write_lowercase_string(char* string, int row=0, int col=0) {
strcpy_P(buffer, string);
for (int i=0; i<strlen(buffer); i++) {
write_lowercase(buffer[i], i/8, i%8);
}
}
void set_auto_brightness() {
light_avg = (light_avg + analogRead(3)) / 2;
//light_avg = analogRead(3);
b = (int) map((long)light_avg, 150, 1000, 0, 13);
if (b<0)
b=0;
set_global_brightness(b);
}
void setup() {
analogReference(DEFAULT);
// Time.h
Serial.begin(9600);
setSyncProvider(requestSync);
//Serial.println("Waiting for sync message");
//keyboard.begin(PS2DATA, PS2IRQ);
setup_max6954();
//expression = (char*) malloc(EXPRSIZE * sizeof(char));
//CHECKMALLOC(expression);
for (int i=0; i<EXPRSIZE; i++) {
expression[i] = ' ';
}
expression[EXPRSIZE-1]='\0';
lcd_c = 0;
lcd_r = 0;
expr_size = 0;
set_auto_brightness();
// Top seconds display
// Decode mode disabled
write_max6954a(0x01, B00000000);
// Turn off all segments manually
for (int i=0x20; i<=0x2F; i++) {
write_max6954a(i, B00000000);
}
//for (int i=0; i<2; i++) {
//write_max6954(0x01, B00000000);
//write_lowercase_string(lower_test3);
//delay(1000);
//write_max6954(0x01, B11111111);
//write_string("mitchell",0);
//write_string("griffith",1);
//delay(1000);
//}
randomSeed(analogRead(3));
//write_max6954(0x01, B00000000);
//for (int i=0; i<16000; i++) {
//write_max6954a(0x20+random(8), random(255));
//write_max6954a(0x28+random(8), random(255));
//write_max6954b(0x20+random(8), random(255));
//write_max6954b(0x28+random(8), random(255));
//delay(100);
//}
write_max6954a(0x01, B00000000);
write_max6954b(0x01, B11111111);
}
prog_uint8_t segment[]= {
B00000001,
B00100000,
B00110000,
B00111000,
B01111000,
B01111100,
B01111110,
B01111111,
B00000011,
B00000111,
B10000111,
B10001111,
B10011111,
B10111111,
B11111111,
};
prog_uint8_t segment_register[]= {
0x20,
0x28,
0x28,
0x28,
0x28,
0x28,
0x28,
0x28,
0x20,
0x20,
0x20,
0x20,
0x20,
0x20,
0x28,
};
void second_display(int seconds) {
int digit = seconds / 15;
int s = seconds % 15;
if (digit == 0 && seconds == 0) {
for (int i=0x20; i<=0x23; i++) {
write_max6954a(i, B00000000);
}
write_max6954a(0x20, B00000001);
}
write_max6954a(segment_register[s]+digit,segment[s]);
}
float temp_to_f(float celsius) {
return (celsius * 9.0 / 5) + 32;
}
int last_second = -1;
void loop() {
if(Serial.available() )
{
processSyncMessage();
}
if (second() == 0 || second() > last_second) {
last_second = second();
t = temp_to_f(temp.GetTemperature());
set_auto_brightness();
sprintf(expression, "%02u.%02u %2d.%1d", hour(), minute(), (int)t, (int)round(t*10)%10);
write_string(expression, 1);
second_display(second());
}
write_max6954a(0x24+random(4), random(255));
write_max6954a(0x2c+random(4), random(255));
delay(50);
//write_string(" ",1);
//write_int1(b);
//if(timeStatus()!= timeNotSet)
//{
//digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
//digitalClockDisplay();
//}
//sprintf(expression, "%02u/%02u/%02u", month(), day(), year()-2000);
//write_string(expression, 0);
//sprintf(expression, " %02u.%02u.%02u ", hour(), minute(), second());
/*write_string("#.#.#.#.#.#.#.#.");*/
/*delay(1000);*/
/*
for (int i = 48; i<=127; i++) {
for(int j=0x20; j<=0x27; j++) {
write_max6954(j, i);
}
delay(50);
}
for (int i=0; i<10; i++) {
for(int j=0x20; j<=0x27; j++)
write_max6954(j, '\\');
delay(dl);
for(int j=0x20; j<=0x27; j++)
write_max6954(j, '|');
delay(dl);
for(int j=0x20; j<=0x27; j++)
write_max6954(j, '/');
delay(dl);
for(int j=0x20; j<=0x27; j++)
write_max6954(j, '-');
delay(dl);
}
*/
/*
// larson scanning
for (int i=0; i<1; i++) {
control_intensity(0, 1, 2, 3, 0, 0, 0, 0, CS1);
write_string("**** ",0);
delay(dl);
control_intensity(0, 0, 1, 2, 3, 0, 0, 0, CS1);
write_string(" **** ",0);
delay(dl);
control_intensity(0, 0, 0, 1, 2, 3, 0, 0, CS1);
write_string(" **** ",0);
delay(dl);
control_intensity(0, 0, 0, 0, 1, 2, 3, 0, CS1);
write_string(" **** ",0);
delay(dl);
control_intensity(0, 0, 0, 0, 0, 1, 2, 3, CS1);
write_string(" ****",0);
delay(dl);
control_intensity(0, 0, 0, 0, 0, 0, 3, 2, CS1);
write_string(" ***",0);
delay(dl);
control_intensity(0, 0, 0, 0, 0, 3, 2, 1, CS1);
write_string(" ***",0);
delay(dl);
control_intensity(0, 0, 0, 0, 3, 2, 1, 0, CS1);
write_string(" ****",0);
delay(dl);
control_intensity(0, 0, 0, 3, 2, 1, 0, 0, CS1);
write_string(" **** ",0);
delay(dl);
control_intensity(0, 0, 3, 2, 1, 0, 0, 0, CS1);
write_string(" **** ",0);
delay(dl);
control_intensity(0, 3, 2, 1, 0, 0, 0, 0, CS1);
write_string(" **** ",0);
delay(dl);
control_intensity(3, 2, 1, 0, 0, 0, 0, 0, CS1);
write_string("**** ",0);
delay(dl);
control_intensity(2, 3, 0, 0, 0, 0, 0, 0, CS1);
write_string("*** ",0);
delay(dl);
control_intensity(1, 2, 3, 0, 0, 0, 0, 0, CS1);
write_string("*** ",0);
delay(dl);
}
*/
/*
// 0000000011111111222222223333333344444444555555556666666677777777888888889999999900000000
char e1[] = " This message to you How are doing miss ";
char e2[] = " is a from me you today Abigail?";
int elength = strlen(e1);
int x = 0;
while (1) {
write_string1(&e1[x]);
write_string2(&e2[x]);
for (int i=0; i<=9; i++){
control_intensity(i,i,i,i,i,i,i,i);
delay(65);
}
delay(2000);
for (int i=9; i>=0; i--){
control_intensity(i,i,i,i,i,i,i,i);
delay(65);
}
x+=8;
if (x >= elength) {
x = 0;
write_string(" ");
delay(2000);
}
}
*/
/*
while (1) {
if (keyboard.available()) {
c = get_keypress();
if (c == PS2_BACKSPACE) {
expression[expr_size-1]= ' ';
expr_size--;
}
else if (c == PS2_UPARROW) {
b++;
if (b>15)
b = 15;
control_intensity(b,b,b,b,b,b,b,b);
}
else if (c == PS2_DOWNARROW) {
b--;
if (b<0)
b = 0;
control_intensity(b,b,b,b,b,b,b,b);
}
else if (expr_size < EXPRSIZE-1) {
expression[expr_size] = c;
expr_size++;
}
window_start = 0;
if (expr_size-LCDCOLS > 0){
window_start = expr_size-LCDCOLS;
}
window_end = window_start+16;
if (window_end > (EXPRSIZE-1)){
window_end = EXPRSIZE-1;
}
write_string1(&expression[0]);
write_string2(&expression[8]);
}
}
*/
}
void processSyncMessage() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
time_t requestSync()
{
//Serial.print(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}
// vim: ft=cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment