Raspberry Pi mit Internetradio und Airplay

Hier der einige Tipps zum Aufbau eines Raspberry Pis ohns Monitor, Tastatur und Maus mit den Fähigkeiten remote eine Oberfläche bereit zu stellen, Internetradio wieder zu geben und als Airplay Station zu dienen. Achso und nebneher noch einen Webserver (LAMP) laufen lassen.

Was braucht man? Raspberry Pi, Stromversorgung dazu, WLAN-Stick, USB-Soundkarte (der Soundausgang am Raspi taugt nicht).

Raspberry für VNC einrichten, WLAN und Sound USB Stick.

MDP installieren: Internetradio (etliche gute hints) und hier. Kleine Hürden hier: Eirkennen der USB-Soundkarte und Lautstärkeeinstellung derselben, da muss man ein wenig nachlesen. Ausprobieren per von MDP über SSH oder VNC.

Shairport (Airport client) installieren: Gute Anleitung hier und hier. Ich hatte Probleme ein Perl-Modul nach zu rüsten. Die Lösung dazu findet man hier. (Problem: “Warning: Cannot install NET::SDP, don’t know what it is.”. Lösung:

sudo cpan
install NJH/Net-SDP-0.07.tar.gz
exit

Das Internetradio kann man perfekt mit der App MPoD fernsteuern. Airplay ist ja sowieso an Bord.

Grove Tick Tock Shield Kit

Giving the Tick Tock Shield Kit by Grove a try, I made same changes to the real time clock code. First I wanted to automaticaly change the brightness not only at the start of the code. And second I wanted the date to displayed to. Now it displays alternately with the temperature. (To set the date, do this once when hour and minutes are set, see comment there. This should be enough, no need to alter with the buttons). Had to change the library too. So find all the code here. RealTimeClock2:

/****************************************************************************/
//  Demo function:
//
//  Author:Frankie.Chu
//  Date:23 September, 2012
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
//  Modified record:
//  Johannes, added function for auto brighness and showing date 11/2013
//
/*******************************************************************************/

#include "Wire.h"
#include <TimerOne.h>
#include <MsTimer2.h>
#include <EEPROM.h>
#include "TM1636.h"
#include "TickTockShield.h"

#define ON 1
#define OFF 0

//definitions of global variables
boolean flag_update;//1 = update the time on the 4-digit display
unsigned char halfsecond;//each time the timer2 interrupts,halfsecond plus one.
boolean flag_clockpoint = 1;//change between 0 and 1 every 500ms. If it is 1,
//the clockpoint is on.Else the clockpoint is off.
boolean flag_2s;//2 seconds flag.1 = to the end of 2 seconds so as to set 
//flag_display_time
boolean flag_10s;//10 seconds flag.1 = to the end of 10 seconds so as to 
//display temperature
boolean flag_display_time = 1;//1 = can display time that get from the RTC
uint8_t counter_times = 20;//20*halfsecond = 10seconds.
//If it is 20,to count for 10s,so display time for 10s.
//If it is 4,to count for 2s,so display the temperture for 2s
boolean flag_500ms_blink;//when adjusting the time or the alarm,the hour or minute
//displayed on the 4-digit display will blink every 500ms.
boolean flag_scan_again;//1 = can scan key again at the state of SYSTEM_NORAML
//To avoid the mistake of entering the state of SYSTEM_ADJUSTING 
//again when pressing MENU for 3s to confirm the setting.
uint8_t counter_500ms;//when counter_500 reach 6,flag_scan_again is set 1.

boolean flag_temp_shown; // added by Johannes

/*status definitions for the tick shield control system*/
uint8_t system_states;
#define SYSTEM_NORAML    0 //default status,and the system will turn to "normal" status 
#define SYSTEM_ADJUSTING 1 //triggered when key MENU is pressed at the "normal" status.
#define SYSTEM_ALARMING  2

//--Declare a TickTockShield Class object--//
TickTockShield ticktockshield;

extern int8_t disp[4];//external global array defined in TickTockShield.cpp

extern byte g_hand;//external global variable.
//the clock hand, HOUR shows that it is adjusting the hour, 
//MINUTE shows that it is adjusting the minute.

void setup() 
{
#ifdef DEBUG
  Serial.begin(9600);
#endif
  ticktockshield.init();

  /*Run the 4 LEDs from left to right*/
  ticktockshield.runLED();
  if(isFirstLoad())//if it is the first time to load the firmware?
  {
    ticktockshield.setAlarm(12,0);//Yes,the alarm clock is initialized to 12:00
    //and the data in the EEPROM.
  }
  else ticktockshield.getAlarm();//No,read the alarm clock stored in EEPROM
  /*When system starts, adjust the brightness of the digital tube 
   			according to the ambient light intensity.*/
  uint8_t lightlevel; 
  lightlevel = ticktockshield.getLightLevel();
  ticktockshield.adjustBrightness(lightlevel);
  /*Read the ambient temperature and display on the digital tube.*/
  ticktockshield.displayTemperature(ticktockshield.getTemperature());
  delay(1000);
  MsTimer2::set(500, Timer2ISR);//Timer2 generates an interrupt every 500ms
  MsTimer2::start();//Timer2 starts counting
}
void loop() 
{
  clockStart();
}
/****************************************************************/
/*Function:Implement the function of a clock with an alarm clock, and */
/*			the buttons can adjust the clock.*/
void clockStart()
{
  if(system_states == SYSTEM_NORAML)//if it is normal states?
  {
    /*Yes, the clock will update every 500ms,and check whether the Menu is pressed */
    /*to enter SYSTEM_ADJUSTING state and check if it should be enter SYSTEM_ALARMING state.*/
    if(flag_update)//if update flag is not zero?
    { 
      flag_update = 0;
      if(flag_clockpoint)
      {
        tm1636.point(POINT_ON);
      }
      else tm1636.point(POINT_OFF); 
      ticktockshield.getTime();
      if(ticktockshield.isAlarmEnable())
      {
        tm1636.point(POINT_ON);
        ticktockshield.displayTime();
        system_states = SYSTEM_ALARMING;
        return;
      }
      if(flag_display_time)ticktockshield.displayTime();

      if(flag_2s)
      {
        flag_2s = 0;
        flag_display_time = 1;

        counter_times = 20;
        halfsecond = 0;
      }
      if(flag_10s)
      {
        flag_10s = 0;
        flag_display_time = 0;

        if(flag_temp_shown == 0){
          tm1636.point(POINT_OFF);
          ticktockshield.displayTemperature(ticktockshield.getTemperature());
          flag_temp_shown = 1;
        } 
        else {

          // Johannes: Set the new brightness; bug fixed 7 - lightlevel as parameter
          uint8_t lightlevel; 
          lightlevel = ticktockshield.getLightLevel();
          ticktockshield.adjustBrightness(7 - lightlevel);

          tm1636.point(POINT_ON);
          ticktockshield.displayDate(); 
          flag_temp_shown = 0;

        }

        counter_times = 4;
        halfsecond = 0;

        /*
        int8_t temp[4];
         temp[0] = ticktockshield.g_dayOfMonth/10;
         temp[1] = g_dayOfMonth%10;
         temp[2] = g_month/10;
         temp[3] = g_month%10;
         	tm1636.display(temp);
         */
      }
      if((flag_scan_again)&&(KEY_MENU == ticktockshield.scanKey()))
      {
        ticktockshield.writeToAdjustArea();
        ticktockshield.processKey();
        system_states = SYSTEM_ADJUSTING;
      }

    }
  }
  else if(system_states == SYSTEM_ADJUSTING)
  {
    ticktockshield.scanKey();
    ticktockshield.processKey();
    ticktockshield.processSystemStatus();
    if(ticktockshield.getQuitReq())
    {
      system_states = SYSTEM_NORAML;
      counter_500ms = 0;
      flag_scan_again = 0;
    }
    else flag_scan_again = 1;
  }
  else if(system_states == SYSTEM_ALARMING)
  {
    /*It will sound alarm for a minute untill the "MENU" key is pressed*/
    if(ticktockshield.isAlarmEnable())
    {
      ticktockshield.alarming();
    }
    else 
    {
      ticktockshield.turnOffAlarm();
      system_states = SYSTEM_NORAML;
    }
    if(KEY_MENU == ticktockshield.scanKey())
    {
      ticktockshield.turnOffAlarm();
      system_states = SYSTEM_NORAML;
    }
  }
}

//--------------------------------------
boolean isFirstLoad()
{
  unsigned char mark[] = "ALARM";
  unsigned char temp_data[5];
  for(unsigned char i = 0;i < 5;i ++)
  {
    temp_data[i] = EEPROM.read(i);
    if(temp_data[i] != mark[i])
    {
      EEPROM.write(0,mark[0]);
      EEPROM.write(1,mark[1]);
      EEPROM.write(2,mark[2]);
      EEPROM.write(3,mark[3]);
      EEPROM.write(4,mark[4]);
      return true;
    }
  }
  return false;
}

/*Function:It is timer 2 interrupt service routine.Timer2 generates an interrupt*/
/*  every 500ms.And every time it interrupts,this function will be executed.*/
void Timer2ISR()
{
  halfsecond ++;
  if(halfsecond  == counter_times)
  {
    halfsecond  = 0;
    if(counter_times == 4)flag_2s = 1;
    else if(counter_times == 20)flag_10s = 1;
  }
  flag_update = 1;
  flag_clockpoint = (~flag_clockpoint) & 0x01;//change between 0 and 1 every 500ms.
  flag_500ms_blink = ~flag_500ms_blink;
  if(ticktockshield.isAdjustingTime())
  {
    if(g_hand == HOUR)
    {
      if(flag_500ms_blink)
      {
        disp[0] = INDEX_BLANK;
        disp[1] = INDEX_BLANK;
      }
    }
    else
    {
      if(flag_500ms_blink)
      {
        disp[2] = INDEX_BLANK;
        disp[3] = INDEX_BLANK;
      }
    }
    tm1636.display(disp);
  }
  counter_500ms ++;
  if(counter_500ms == 6)
  {
    counter_500ms = 0;
    flag_scan_again = 1;
  }

}

TickTockShield.h:

#ifndef TICKTOCKSHIELD_H_
#define TICKTOCKSHIELD_H_

#include "TM1636.h"

#define DS1307_I2C_ADDRESS 0x68

//debug configuration
#define DEBUG 0
//-------pin definition of keys---------------//
#define KEY_MENU	11
#define KEY_UP		10
#define KEY_DOWN	9

//-------pin definition of LEDs---------------//
#define LED_CLOCK_ADJUST	5
#define LED_ALARM_ADJUST	4
#define LED_ALARM_ENABLE	3
#define LED_BRIGHT_ADJUST	2

#define ALARM_BUZZER		6

//-------pin definition of sensors-------------//
#define TEMPERATURE_SENSOR	A0
#define LIGHT_SENSOR		A1

#define LED_ON	1
#define LED_OFF 0

#define HOUR	0
#define MINUTE	1
/*Direction of the LEDs to run*/
#define LEFT_TO_RIGHT 0
#define RIGHT_TO_LEFT 1
//-------Status bit definition of the Status flag----//
/*volatile typedef enum{
  SS_NO_INPUT = 0,//no keys input
  SS_CLOCKH_ADJUST,//adjust the hour of the clock
  SS_CLOCKM_ADJUST,	//adjust the minute of the clock
  SS_ALARMH_ADJUST,	//adjust the hour of the alarm
  SS_ALARMM_ADJUST,	//adjust the minute of the alarm
  SS_ALARM_ENABLE,  //to enable or disable the alarm
  SS_BRIGHT_ADJUST,	//adjust the brightness of the 4-digit display
  SS_CONFIRM_EXIT,  //It will confirm the setting and exit after pressing the MENU button for more than 3s
  					//or after pressing it again at the status of adjust brightness.
  SS_QUIT,			//It will exit without saving any setting if no button is pressed within 5s.
}keyStatusType;*/
//extern keyStatusType system_status;

#define SS_NO_INPUT 		0//no keys input
#define SS_CLOCKH_ADJUST	1//adjust the hour of the clock
#define SS_CLOCKM_ADJUST	2//adjust the minute of the clock
#define SS_ALARMH_ADJUST	3//adjust the hour of the alarm
#define SS_ALARMM_ADJUST	4//adjust the minute of the alarm
#define SS_ALARM_ENABLE  	5//to enable or disable the alarm
#define SS_BRIGHT_ADJUST	6//adjust the brightness of the 4-digit display
#define SS_CONFIRM_EXIT  	7//It will confirm the setting and exit after pressing the MENU button for more than 3s
							 //or after pressing it again at the status of adjust brightness.
#define SS_QUIT				8//It will exit without saving any setting if no button is pressed within 5s.

#define KEY_PRESSED 1
#define KEY_RELEASE 2

#define RESISTOR_CONNECT_THERMISTOR	10000
struct AlarmStruct
{
  byte hour;
  byte minute;
  boolean flag_enable;
};
class TickTockShield
{
public:
  void init();
  void setLed(unsigned char led_status, int pinLED);
  void turnOffLED();
  void turnOnLED();
  void runLED(byte = 1, byte = LEFT_TO_RIGHT);
  int16_t scanKey();
  void processKey();
  void processSystemStatus();
  void quit();
  void saveChanges();
  inline void modifyAlarmFlag();
  void adjustClock(uint8_t hand);
  void adjustAlarm(uint8_t hand);
  void adjustBrightness();
  void adjustBrightness(uint8_t grayscale);
  int8_t getTemperature();
  void displayTemperature(int8_t temperature);
  float getLightIntensity();
  uint8_t getLightLevel();
  void ringOn();
  void ringOff();
  void alarming();
  void turnOffAlarm();
  boolean isAlarmEnable();
  boolean compareWithAlarm();
  inline void alarmDisable();
  void setAlarm(struct AlarmStruct alarm_);
  void setAlarm(uint8_t hour,uint8_t minute,uint8_t = 0);
  void getAlarm();

  void writeToAdjustArea();
  void writeToNormalArea();
  void writeTime();
  void getTime();
  void displayTime();
  void displayDate();  //added by Johannes
  void display(int8_t DispData []);
  void clearTime(uint8_t hand);
  byte decToBcd(byte val);
  byte bcdToDec(byte val);
  byte getQuitReq(){return flag_require_quit;}
  boolean isAdjustingTime();
private:
  /*Globle variables in the class*/
  byte g_second, g_minute, g_hour;
  /*when the clock is being adjusted, these two variables save the temporary data.*/
  byte g_minute_temp,g_hour_temp;
  byte g_dayOfWeek;
  byte g_dayOfMonth, g_month, g_year;
  AlarmStruct alarm;
  byte g_brightness;
  byte g_brightness_temp;
  /*when the alarm is being adjusted, this variable save the temporary data.*/
  AlarmStruct alarm_temp;
  int16_t pre_pin_number;//pin number of the key pressed before
  int16_t key_pin_pressed;//pin number of the key pressed now
  uint8_t tempKeyValue;
  uint8_t stateMenu;
  uint8_t stateUp;
  uint8_t stateDown;

  uint8_t system_status;
  byte flag_require_quit;
  boolean flag_adjust_time;
  boolean flag_alarm_over;

  void keyInit();
  void ledInit();
  void sensorInit();
  void buzzerInit();
};
extern TM1636 tm1636;

#endif

TickTockShield.cpp

#include "TickTockShield.h"
#include <Arduino.h>
#include <EEPROM.h>
#include <Wire.h>
#include <TimerOne.h>
#include "TM1636.h"

TM1636 tm1636(7,8);
void timerIsr();
/*global variables also used in timer interrupt serve routine */
uint8_t key_menu_status;
uint16_t count_10ms_pressed;
uint16_t count_10ms_release;
boolean flag_pressed_3s;
boolean flag_release_10s;
boolean alarm_on_off;

int8_t disp[4];
int8_t disp_buff[4];
byte g_hand;

//--------------------------------//
void TickTockShield::init()
{
	g_brightness = BRIGHT_TYPICAL;
  tm1636.set(g_brightness);
  tm1636.init();
  keyInit();
  ledInit();
  sensorInit();
	buzzerInit();
  Wire.begin();
	pre_pin_number = -1;
	Timer1.initialize(100000); // set a timer of length 100000 microseconds 
		//(or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here
}

void TickTockShield::keyInit()
{
  //Set all three keys to be input and internal pull-up

  pinMode(KEY_MENU, INPUT);
  digitalWrite(KEY_MENU, HIGH);
  pinMode(KEY_UP, INPUT);
  digitalWrite(KEY_UP, HIGH);
  pinMode(KEY_DOWN, INPUT);
  digitalWrite(KEY_DOWN, HIGH);
}
void TickTockShield::ledInit()
{
  pinMode(LED_CLOCK_ADJUST, OUTPUT);
  pinMode(LED_ALARM_ADJUST, OUTPUT);
  pinMode(LED_BRIGHT_ADJUST, OUTPUT);
  pinMode(LED_ALARM_ENABLE, OUTPUT);
	turnOffLED();
}
void TickTockShield::sensorInit()
{
  pinMode(TEMPERATURE_SENSOR, INPUT);
  pinMode(LIGHT_SENSOR, INPUT);
}
void TickTockShield::buzzerInit()
{
	pinMode(ALARM_BUZZER, OUTPUT);
  pinMode(ALARM_BUZZER, OUTPUT);
}
void TickTockShield::setLed(unsigned char led_status, int pinLED)
{
	digitalWrite(pinLED, led_status);
}
/********************************/
void TickTockShield::turnOffLED()
{
	setLed(LOW,LED_CLOCK_ADJUST);
	setLed(LOW,LED_ALARM_ADJUST);
	setLed(LOW,LED_BRIGHT_ADJUST);
	setLed(LOW,LED_ALARM_ENABLE);
}
/********************************/
void TickTockShield::turnOnLED()
{
	setLed(HIGH,LED_CLOCK_ADJUST);
	setLed(HIGH,LED_ALARM_ADJUST);
	setLed(HIGH,LED_BRIGHT_ADJUST);
	setLed(HIGH,LED_ALARM_ENABLE);
}

/*******************************/
void TickTockShield::runLED(byte speed, byte direction)
{
	if((speed > 0)&&(speed < 11))//If the value of speed is valid?
	{
		turnOffLED();
		uint8_t shifter = 0x01;
		if(LEFT_TO_RIGHT == direction)
		{
			for(uint8_t i = 0;i < 4;i ++)
			{
				if(shifter&0x01) setLed(LED_ON,  LED_CLOCK_ADJUST);
				else						 setLed(LED_OFF, LED_CLOCK_ADJUST);
				if(shifter&0x02) setLed(LED_ON,  LED_ALARM_ADJUST);
				else					   setLed(LED_OFF, LED_ALARM_ADJUST);
				if(shifter&0x04) setLed(LED_ON,  LED_ALARM_ENABLE);
				else						 setLed(LED_OFF, LED_ALARM_ENABLE);
				if(shifter&0x08) setLed(LED_ON,  LED_BRIGHT_ADJUST);
				else						 setLed(LED_OFF, LED_BRIGHT_ADJUST);
				shifter <<= 1;
				delay(500/speed);
			}
		}
		else
		{
			for(uint8_t i = 0;i < 4;i ++)
			{
				if(shifter&0x01) setLed(LED_ON,  LED_BRIGHT_ADJUST);
				else						 setLed(LED_OFF, LED_BRIGHT_ADJUST);
				if(shifter&0x02) setLed(LED_ON,  LED_ALARM_ENABLE);
				else					   setLed(LED_OFF, LED_ALARM_ENABLE);
				if(shifter&0x04) setLed(LED_ON,  LED_ALARM_ADJUST);
				else						 setLed(LED_OFF, LED_ALARM_ADJUST);
				if(shifter&0x08) setLed(LED_ON,  LED_CLOCK_ADJUST);
				else						 setLed(LED_OFF, LED_CLOCK_ADJUST);
				shifter <<= 1;
				delay(500/speed);
			}
		}
		turnOffLED();
	}
}
//--------------------------------//
//-Return:the pin number of the key pressed
//--------------------------------//
//
int16_t TickTockShield::scanKey()
{
	int16_t pin_number = 0;
	static boolean pre_key_menu_level = HIGH;
	boolean cur_key_menu_level;
	if(digitalRead(KEY_MENU) == LOW)
	{
		delay(30);
		if(digitalRead(KEY_MENU) == LOW)
		{
			cur_key_menu_level = LOW;
			pin_number = KEY_MENU;
		}
		else cur_key_menu_level = HIGH;
	}
	else
	{
		cur_key_menu_level = HIGH;
	}
	if(pre_key_menu_level > cur_key_menu_level)
	{

		key_menu_status = KEY_PRESSED;
		if(system_status < 7)system_status ++;
		count_10ms_pressed = 0;
		flag_pressed_3s = 0;
	#ifdef DEBUG
		Serial.print("system_status = ");
	  Serial.println(system_status);
		Serial.println("Key_Menu pressed");
	#endif
	}
	else if(pre_key_menu_level < cur_key_menu_level)
	{
		key_menu_status = KEY_RELEASE;
		count_10ms_release = 0;
		flag_release_10s = 0;
	#ifdef DEBUG
		Serial.println("Key_Menu release");
  #endif
	}
	pre_key_menu_level = cur_key_menu_level;

	if(digitalRead(KEY_UP) == LOW)
	{
		delay(20);
		if(digitalRead(KEY_UP) == LOW)
		{
			pin_number = KEY_UP;
			count_10ms_release = 0;
			flag_release_10s = 0;
		#ifdef DEBUG
			Serial.println("Key_UP pressed");
		#endif
		}

	}
	else if(digitalRead(KEY_DOWN) == LOW)
	{
		delay(20);
		if(digitalRead(KEY_DOWN) == LOW)
		{
			pin_number = KEY_DOWN;
			count_10ms_release = 0;
			flag_release_10s = 0;
		#ifdef DEBUG
			Serial.println("KEY_DOWN pressed");
		#endif
		}
	}
	if(pin_number == 0)pin_number = -1;
	key_pin_pressed = pin_number;
	return key_pin_pressed;
}
/*************************************************/
/*Function:*/
/*Return:   void*/
void TickTockShield::processKey()
{
	if((key_pin_pressed > 0)&&(pre_pin_number != key_pin_pressed))
	{
		ringOn();
		delay(100);
		ringOff();
	}
	pre_pin_number = key_pin_pressed;
	if(flag_pressed_3s)
	{
		flag_pressed_3s = 0;
		system_status = SS_CONFIRM_EXIT;
	#ifdef DEBUG
		Serial.print("system_status = ");
	  Serial.println(system_status);
		Serial.println("Press for 3s...");
	#endif
	}
	if(flag_release_10s)
	{
		flag_release_10s = 0;
		system_status = SS_QUIT;
	#ifdef DEBUG
		Serial.print("system_status = ");
	  Serial.println(system_status);
		Serial.println("Release for 5s");
	#endif
	}	
}
void TickTockShield::processSystemStatus()
{
	switch(system_status)
	{
		case SS_NO_INPUT:			
													break;										
		case SS_CLOCKH_ADJUST:adjustClock(HOUR);
													break;	
		case SS_CLOCKM_ADJUST:adjustClock(MINUTE);
													break;	
		case SS_ALARMH_ADJUST:adjustAlarm(HOUR);
													break;	
		case SS_ALARMM_ADJUST:adjustAlarm(MINUTE);
													break;	
		case SS_ALARM_ENABLE: flag_adjust_time = 0;
													modifyAlarmFlag();
													break;	
		case SS_BRIGHT_ADJUST:adjustBrightness();
													break;	
		case SS_CONFIRM_EXIT: saveChanges();
													system_status = SS_NO_INPUT;
													break;	
		case SS_QUIT:					quit();
													system_status = SS_NO_INPUT;
													break;
		default:break;	
	}

}
void TickTockShield::quit()
{
	flag_require_quit = 1;
	flag_adjust_time = 0;
	tm1636.set(g_brightness);
	turnOffLED();
	if(alarm.flag_enable)
		setLed(HIGH,LED_ALARM_ENABLE);
	else setLed(LOW,LED_ALARM_ENABLE);

}
void TickTockShield::saveChanges()
{
	g_hour   = g_hour_temp;
	g_minute = g_minute_temp;

    // added by Johannes
    // do this once to set the date
 //   g_dayOfMonth = 3;
 //   g_month = 11;

	writeTime();

	alarm.hour        = alarm_temp.hour;
	alarm.minute      = alarm_temp.minute;
	alarm.flag_enable = alarm_temp.flag_enable;
	setAlarm(alarm);

	g_brightness 			= g_brightness_temp;
	tm1636.set(g_brightness);

	flag_alarm_over = 0;
	quit();
}
inline void TickTockShield::modifyAlarmFlag()
{
	turnOffLED();
	if(key_pin_pressed == KEY_UP)
	{
		alarm_temp.flag_enable = ~alarm_temp.flag_enable;
	}
	if(alarm_temp.flag_enable)
	{
		setLed(HIGH,LED_ALARM_ENABLE);
	}
	else
	{
		setLed(LOW,LED_ALARM_ENABLE);
	}
	disp[0] = alarm_temp.hour/10;
	disp[1] = alarm_temp.hour%10;
	disp[2] = alarm_temp.minute/10;
	disp[3] = alarm_temp.minute%10;
	tm1636.display(disp);
}
void TickTockShield::adjustClock(uint8_t hand)
{
	turnOffLED();
	setLed(HIGH,LED_CLOCK_ADJUST);
	if(hand == HOUR)
	{
		g_hand = HOUR;
		if(key_pin_pressed == KEY_DOWN)
		{
			if(g_hour_temp> 0)
				g_hour_temp--;
		}
		else if(key_pin_pressed == KEY_UP)
		{
			if(g_hour_temp< 23)
				g_hour_temp++;
			else g_hour_temp = 0;
		}
	}
	else
	{
		g_hand = MINUTE;
		if(key_pin_pressed == KEY_DOWN)
		{
			if(g_minute_temp> 0)
				g_minute_temp--;
		}
		else if(key_pin_pressed == KEY_UP)
		{
			if(g_minute_temp< 59)
				g_minute_temp++;
			else g_minute_temp = 0;
		}
	}
	disp[0] = g_hour_temp/10;
	disp[1] = g_hour_temp%10;
	disp[2] = g_minute_temp/10;
	disp[3] = g_minute_temp%10;
//	tm1636.display(disp);
}
void TickTockShield::adjustAlarm(uint8_t hand)
{
	turnOffLED();
	setLed(HIGH,LED_ALARM_ADJUST);
	if(hand == HOUR)
	{
		g_hand = HOUR;
		if(key_pin_pressed == KEY_DOWN)
		{
			if(alarm_temp.hour> 0)
				alarm_temp.hour--;
		}
		else if(key_pin_pressed == KEY_UP)
		{
			if(alarm_temp.hour< 23)
				alarm_temp.hour++;
			else alarm_temp.hour = 0;
		}
	}
	else
	{
		g_hand = MINUTE;
		if(key_pin_pressed == KEY_DOWN)
		{
			if(alarm_temp.minute> 0)
				alarm_temp.minute--;
		}
		else if(key_pin_pressed == KEY_UP)
		{
			if(alarm_temp.minute< 59)
				alarm_temp.minute++;
			else alarm_temp.minute = 0;
		}
	}
	disp[0] = alarm_temp.hour/10;
	disp[1] = alarm_temp.hour%10;
	disp[2] = alarm_temp.minute/10;
	disp[3] = alarm_temp.minute%10;
//	tm1636.display(disp);
}
/***********************************/
void TickTockShield::adjustBrightness()
{
	turnOffLED();
	setLed(HIGH,LED_BRIGHT_ADJUST);
	if(key_pin_pressed == KEY_DOWN)
	{
		if(g_brightness_temp> 0)
			g_brightness_temp--;
	}
	else if(key_pin_pressed == KEY_UP)
	{
		if(g_brightness_temp< 7)
			g_brightness_temp++;
	}
	tm1636.set(g_brightness_temp);
	disp[0] = alarm_temp.hour/10;
	disp[1] = alarm_temp.hour%10;
	disp[2] = alarm_temp.minute/10;
	disp[3] = alarm_temp.minute%10;
	tm1636.display(disp);
}
void TickTockShield::adjustBrightness(uint8_t grayscale)
{
	g_brightness = grayscale;
	tm1636.set(g_brightness);
}
/****************************************************************/
/*Return:int8_t,Temperature that range from -40 to 125 degrees.					*/
int8_t TickTockShield::getTemperature()
{
	float temperature,resistance;
	int a;
	int B = 3975;
	a = analogRead(TEMPERATURE_SENSOR);
	resistance   = (float)(1023-a)*RESISTOR_CONNECT_THERMISTOR/a; 
	temperature  = 1/(log(resistance/RESISTOR_CONNECT_THERMISTOR)/B+1/298.15)-273.15;
	return (int8_t)temperature;
}
/**********************************************************************/
/*Function:   Display the temperature on the 4-digit display										*/
/*Parameter:-int8_t temperature,Temperature that range from -40 to 125 degrees. */
/*Return:	  void																												*/
void TickTockShield::displayTemperature(int8_t temperature)
{
  int8_t temp[4];
  if(temperature < 0)
	{
		temp[0] = INDEX_NEGATIVE_SIGN;
		temperature = abs(temperature);
	}
	else if(temperature < 100)temp[0] = INDEX_BLANK;
	else temp[0] = temperature/100;
	temperature %= 100;
	temp[1] = temperature / 10;
	temp[2] = temperature % 10;
	temp[3] = 12;	          //index of 'C' for celsius degree symbol.
	tm1636.display(temp);
}

float TickTockShield::getLightIntensity()
{
  int sensorValue = analogRead(LIGHT_SENSOR);
  float rsensor;
  rsensor=(float)(1023-sensorValue)*10/sensorValue;
	return rsensor;
}

uint8_t TickTockShield::getLightLevel()
{
	uint16_t resistance;
	uint8_t light_level;
	resistance = (uint16_t)getLightIntensity();
	if(resistance < 10) light_level = 0;
	else if(resistance < 50)light_level = 1;
	else if(resistance < 80)light_level = 2;
	else if(resistance < 110)light_level = 3;
	else if(resistance < 140)light_level = 4;
	else if(resistance < 170)light_level = 5;
	else if(resistance < 200)light_level = 6;
	else light_level = 7;
	return light_level;
}

void TickTockShield::ringOn()
{
	digitalWrite(ALARM_BUZZER, HIGH);
}
void TickTockShield::ringOff()
{
	digitalWrite(ALARM_BUZZER, LOW);
}
void TickTockShield::alarming()
{
	if(alarm_on_off)ringOn();
	else ringOff();
}
void TickTockShield::turnOffAlarm()
{
	ringOff();
	flag_alarm_over = 1;
}
boolean TickTockShield::isAlarmEnable()
{
	if(compareWithAlarm())
	{
		if(!flag_alarm_over)

			return true;
	}
	else flag_alarm_over = 0;
	return false;
}
/*******************************/
boolean TickTockShield::compareWithAlarm()
{
	if(alarm.flag_enable > 0)
	{
		if((alarm.hour == g_hour)&&(alarm.minute == g_minute))
		{
			return true;
		}
	}
	return false;
}

inline void TickTockShield::alarmDisable()
{}
void TickTockShield::setAlarm(struct AlarmStruct alarm_)
{
	EEPROM.write(5,alarm_.hour);
	EEPROM.write(6,alarm_.minute);
	EEPROM.write(7,alarm_.flag_enable);

}
void TickTockShield::setAlarm(uint8_t hour,uint8_t minute,uint8_t flag_enable)
{
	EEPROM.write(5,hour);
	EEPROM.write(6,minute);
	EEPROM.write(7,flag_enable);
}

/************************************************************/
/*Function:Read the alarm clock information from the built-in EEPROM  */
/*Return:   void																							   */
void TickTockShield::getAlarm()
{
	alarm.hour        = EEPROM.read(5);
	alarm.minute      = EEPROM.read(6);
	alarm.flag_enable = EEPROM.read(7);
#ifdef DEBUG
	Serial.print("alarm = ");
  Serial.print(alarm.hour);
	Serial.print(" : ");
	Serial.print(alarm.minute);
#endif
	if(alarm.flag_enable)
		setLed(HIGH,LED_ALARM_ENABLE);
	else setLed(LOW,LED_ALARM_ENABLE);
}
/***********************************************************/
/*Function:write data to the global variables in Ajust Area 					  */
/*Return:   void																							   */
void TickTockShield::writeToAdjustArea()
{
	g_hour_temp       = g_hour;
	g_minute_temp     = g_minute;
	g_brightness_temp = g_brightness;
	alarm_temp.hour        = alarm.hour;
	alarm_temp.minute      = alarm.minute;
	alarm_temp.flag_enable = alarm.flag_enable;
	flag_require_quit = 0;
	flag_adjust_time = 1;
	pre_pin_number = -1;
	tm1636.point(POINT_ON);
	turnOffLED();
}

/***********************************************************/
/*Function:write data to the global variables in Normal Area    			  */
/*Return:   void																							   */
void TickTockShield::writeToNormalArea()
{
	g_hour       = g_hour_temp;
	g_minute     = g_minute_temp;
	g_brightness = g_brightness_temp;
	alarm.hour        = alarm_temp.hour;
	alarm.minute      = alarm_temp.minute;
	alarm.flag_enable = alarm_temp.flag_enable;
}

/************************************************************/
/*Frunction: Write the time that includes the date to the RTC chip			*/
/*Return:	  void																							*/
void TickTockShield::writeTime()
{
	Wire.beginTransmission(DS1307_I2C_ADDRESS);
	Wire.write((byte)0x00);
	Wire.write(decToBcd(g_second)); 	 // 0 to bit 7 starts the clock
	Wire.write(decToBcd(g_minute));
	Wire.write(decToBcd(g_hour)); 	 // If you want 12 hour am/pm you need to set
									// bit 6 (also need to change readDateDs1307)
	Wire.write(decToBcd(g_dayOfWeek));
	Wire.write(decToBcd(g_dayOfMonth));
	Wire.write(decToBcd(g_month));
	Wire.write(decToBcd(g_year));
	Wire.endTransmission();
}
/************************************************/
/*Frunction: Read time from RTC											*/
/*Parameter:A 2-byte array that tells the hour and			*/ 
//			minute at the end of the function.

void TickTockShield::getTime()
{
    // Reset the register pointer
	Wire.beginTransmission(DS1307_I2C_ADDRESS);
	Wire.write((byte)0x00);
	Wire.endTransmission();  
	Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
	// A few of these need masks because certain bits are control bits
	g_second	   = bcdToDec(Wire.read() & 0x7f);
	g_minute	   = bcdToDec(Wire.read());
	g_hour	     = bcdToDec(Wire.read() & 0x3f);// Need to change this if 12 hour am/pm
	g_dayOfWeek  = bcdToDec(Wire.read());
	g_dayOfMonth = bcdToDec(Wire.read());
	g_month      = bcdToDec(Wire.read());
	g_year	     = bcdToDec(Wire.read());
}
void TickTockShield::displayTime()
{
  unsigned char time[4];
  time[0] = g_hour/10;
  time[1] = g_hour%10;
  time[2] = g_minute/10;
  time[3] = g_minute%10;
  tm1636.display((int8_t*)time);
}
void TickTockShield::displayDate()
{
    unsigned char time[4];
    time[0] = g_dayOfMonth/10;
    time[1] = g_dayOfMonth%10;
    time[2] = g_month/10;
    time[3] = g_month%10;
    tm1636.display((int8_t*)time); //added by Johannes
}

void TickTockShield::display(int8_t DispData [])
{
	tm1636.display(DispData);
}

void TickTockShield::clearTime(uint8_t hand)
{
	unsigned char time[4];
	if(hand == HOUR)
	{
		time[0] = INDEX_BLANK;
  	time[1] = INDEX_BLANK;
  	time[2] = g_minute/10;
  	time[3] = g_minute%10;
	}
	else 
	{
		time[0] = g_hour/10;
	  time[1] = g_hour%10;
	  time[2] = INDEX_BLANK;
	  time[3] = INDEX_BLANK;
	}
	tm1636.display((int8_t*)time);
}
// Convert normal decimal numbers to binary coded decimal
byte TickTockShield::decToBcd(byte val)
{
	return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte TickTockShield::bcdToDec(byte val)
{
	return ( (val/16*10) + (val%16) );
}

boolean TickTockShield::isAdjustingTime()
{
	if(flag_adjust_time)return true;
	else return false;
}
/// --------------------------
/// Custom ISR Timer Routine
/// --------------------------
void timerIsr()
{
	alarm_on_off = (~alarm_on_off) & 0x01;
  if(key_menu_status == KEY_PRESSED)
 	{
 		count_10ms_pressed ++;
		if(count_10ms_pressed == 30)
		{
			flag_pressed_3s = 1;
			key_menu_status = 0;
		}
 	}
	else if(key_menu_status == KEY_RELEASE)
	{
		count_10ms_release ++;
		if(count_10ms_release == 100)
		{
			flag_release_10s = 1;
			key_menu_status = 0;
		}
	}
}

Just a short test of the Grove I2C Color Sensor

Just made a short test of the grove color sensor. Tested some Lego. Red, green and blue works well as you can see, but didn’t manage to measure the difference between red, orange and yellow. Here is the short movie:

And here the source:

/*
Groove Color Sensor and DX LCD Shield

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:

#include 
#include 
#define COLOR_SENSOR_ADDR  0x39//the I2C address for the color sensor 
#define REG_CTL 0x80
#define REG_TIMING 0x81
#define REG_INT 0x82
#define REG_INT_SOURCE 0x83
#define REG_ID 0x84
#define REG_GAIN 0x87
#define REG_LOW_THRESH_LOW_BYTE 0x88
#define REG_LOW_THRESH_HIGH_BYTE 0x89
#define REG_HIGH_THRESH_LOW_BYTE 0x8A
#define REG_HIGH_THRESH_HIGH_BYTE 0x8B
#define REG_BLOCK_READ 0xCF
#define REG_GREEN_LOW 0xD0
#define REG_GREEN_HIGH 0xD1
#define REG_RED_LOW 0xD2
#define REG_RED_HIGH 0xD3
#define REG_BLUE_LOW 0xD4
#define REG_BLUE_HIGH 0xD5
#define REG_CLEAR_LOW 0xD6
#define REG_CLEAR_HIGH 0xD7
#define CTL_DAT_INIITIATE 0x03
#define CLR_INT 0xE0
//Timing Register
#define SYNC_EDGE 0x40
#define INTEG_MODE_FREE 0x00
#define INTEG_MODE_MANUAL 0x10
#define INTEG_MODE_SYN_SINGLE 0x20
#define INTEG_MODE_SYN_MULTI 0x30

#define INTEG_PARAM_PULSE_COUNT1 0x00
#define INTEG_PARAM_PULSE_COUNT2 0x01
#define INTEG_PARAM_PULSE_COUNT4 0x02
#define INTEG_PARAM_PULSE_COUNT8 0x03
//Interrupt Control Register 
#define INTR_STOP 40
#define INTR_DISABLE 0x00
#define INTR_LEVEL 0x10
#define INTR_PERSIST_EVERY 0x00
#define INTR_PERSIST_SINGLE 0x01
//Interrupt Souce Register
#define INT_SOURCE_GREEN 0x00
#define INT_SOURCE_RED 0x01
#define INT_SOURCE_BLUE 0x10
#define INT_SOURCE_CLEAR 0x03
//Gain Register
#define GAIN_1 0x00
#define GAIN_4 0x10
#define GAIN_16 0x20
#define GANI_64 0x30
#define PRESCALER_1 0x00
#define PRESCALER_2 0x01
#define PRESCALER_4 0x02
#define PRESCALER_8 0x03
#define PRESCALER_16 0x04
#define PRESCALER_32 0x05
#define PRESCALER_64 0x06

int readingdata[20];
int i,green,red,blue,clr,ctl;
double X,Y,Z,x,y,z;

#include 

// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);   //DX Shield uses other pins

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
  Wire.begin(); // join i2c bus (address optional for master)
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
  lcd.print(millis()/1000);

  setTimingReg(INTEG_MODE_FREE);//Set trigger mode.Including free mode,manually mode,single synchronizition mode or so.
  setInterruptSourceReg(INT_SOURCE_GREEN); //Set interrupt source 
  setInterruptControlReg(INTR_LEVEL|INTR_PERSIST_EVERY);//Set interrupt mode
  setGain(GAIN_1|PRESCALER_4);//Set gain value and prescaler value
  setEnableADC();//Start ADC of the color sensor
  while(1)
 { 
  lcd.setCursor(0, 0);
  lcd.print("             ");
  lcd.setCursor(0, 0);
  lcd.print(millis()/1000);

   readRGB();
   calculateCoordinate();

  lcd.setCursor(0, 1);
  lcd.print("                ");  

  if (x<0.25) {
   //blue 
  lcd.setCursor(0, 1);
  lcd.print("blau");
  }
  if (x>0.25 && x<0.36) {
   //blue 
  lcd.setCursor(0, 1);
  lcd.print("gruen");
  }

  if (x>=0.36 ) {
   //blue 
  lcd.setCursor(0, 1);
  lcd.print("rot");
  }

   delay(1000);
   clearInterrupt();  
 }
}

/************************************/
void setTimingReg(int x)
{
   Wire.beginTransmission(COLOR_SENSOR_ADDR);
   Wire.write(REG_TIMING);
   Wire.write(x);
   Wire.endTransmission();  
   delay(100); 
}
void setInterruptSourceReg(int x)
{
   Wire.beginTransmission(COLOR_SENSOR_ADDR);
   Wire.write(REG_INT_SOURCE);
   Wire.write(x);
   Wire.endTransmission();  
   delay(100);
}
void setInterruptControlReg(int x)
{
   Wire.beginTransmission(COLOR_SENSOR_ADDR);
   Wire.write(REG_INT);
   Wire.write(x);
   Wire.endTransmission();  
   delay(100);
}
void setGain(int x)
{
   Wire.beginTransmission(COLOR_SENSOR_ADDR);
   Wire.write(REG_GAIN);
   Wire.write(x);
   Wire.endTransmission();
}
void setEnableADC()
{

   Wire.beginTransmission(COLOR_SENSOR_ADDR);
   Wire.write(REG_CTL);
   Wire.write(CTL_DAT_INIITIATE);
   Wire.endTransmission();  
   delay(100);  
}
void clearInterrupt()
{
   Wire.beginTransmission(COLOR_SENSOR_ADDR);
   Wire.write(CLR_INT);
   Wire.endTransmission(); 
}
void readRGB()
{
  Wire.beginTransmission(COLOR_SENSOR_ADDR);
   Wire.write(REG_BLOCK_READ);
   Wire.endTransmission();

   Wire.beginTransmission(COLOR_SENSOR_ADDR);
   Wire.requestFrom(COLOR_SENSOR_ADDR,8);
   delay(500);
   if(8<= Wire.available())    // if two bytes were received 
  { 
    for(i=0;i<8;i++)
    {
      readingdata[i]=Wire.read();
      //Serial.println(readingdata[i],BIN);
     }
  }
  green=readingdata[1]*256+readingdata[0];
  red=readingdata[3]*256+readingdata[2];
  blue=readingdata[5]*256+readingdata[4];
  clr=readingdata[7]*256+readingdata[6];

}

void calculateCoordinate()
{
  X=(-0.14282)*red+(1.54924)*green+(-0.95641)*blue;
  Y=(-0.32466)*red+(1.57837)*green+(-0.73191)*blue;
  Z=(-0.68202)*red+(0.77073)*green+(0.56332)*blue;
  x=X/(X+Y+Z);
  y=Y/(X+Y+Z);
  if((X>0)&&(Y>0)&&(Z>0))
  {
    /*
    Serial.println("The x,y value is");
	Serial.print("(");
    Serial.print(x,2);
	Serial.print(" , ");
    Serial.print(y,2);
	Serial.println(")");
	Serial.println("Please reference the figure(Chromaticity Diagram) in the wiki ");
  	Serial.println("so as to get the recommended color.");
  */
    lcd.setCursor(4, 0);
  lcd.print(x);
  lcd.setCursor(10, 0);
  lcd.print(y);

  }
 else
 {
 //Serial.println("Error,the value overflow");
  lcd.setCursor(4, 0);
  lcd.print("Error");

 }
}