Demo näide

#include <stdio.h>
#include <string.h>
#include <avr/io.h>
//#include <util/delay.h>
#include <homelab/adc.h>
#include <homelab/module/lcd_gfx.h>
#include <homelab/module/segment_display.h>
#include <homelab/module/motors.h>
#include <homelab/module/encoders.h>
#include <homelab/module/sensors.h>
#include <homelab/module/zigbee.h>
#include <homelab/delay.h>
#include "homelab/common.h"
#include "homelab/pin.h"
 
#define ULTRASONIC_SPEED_OF_SOUND    33000	//cm/s
#define ZIGBEE_MAX_NODES 4					//maksimum number zigbee mooduleid võrgus + broadcast aadress + 1
 
usart port = USART(1);						//USART liidese määramine
zigbee_node_t nodelist[ZIGBEE_MAX_NODES];	//teiste leitavate zigbee moodulite aadresside massiiv
 
void SPI_init(void);
void SPI_write(unsigned char data);
 
 
 
/////////////////////////   Peaprogramm  /////////////////////////////
 
int main(void)
{
    char adc0[16],adc1[16],adc2[16],caption0[16],caption1[16];
    unsigned char new_value=0, old_value = 1,new_value0=0, old_value0 = 1,new_value2=0, old_value2 = 1,i,lcd_taust=0;
    int servopos=1, dcpos=1,nupp=0,nupp1=0,dcpulse=0, count=0, adr=0; 
 
///////////////////////   Init   ///////////////////
 
 //bipolar_init();
    lcd_gfx_init();
    lcd_gfx_clear();
    adc_init(ADC_REF_AVCC, ADC_PRESCALE_8);
    segment_display_init();
    pin leds[3]    = { PIN(C, 3), PIN(C, 4), PIN(C, 5) };
    pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) };
    pin led_red = PIN(C, 5);
    pin sensors = PIN(G, 0);
	pin buzzer = PIN(G, 5);
    pin srf05 = PIN(F,2);
 
    pin_setup_output(sensors);
	pin_setup_output(buzzer);
 
    for (i = 0; i < 3; i++)
    {
        pin_setup_output(leds[i]);
        pin_setup_input(buttons[i]);
		dcmotor_init(i);
    }
    usart_init_async(port,USART_DATABITS_8,USART_STOPBITS_ONE,USART_PARITY_NONE,USART_BAUDRATE_ASYNC(9600));	// USART liidese seadistamine
 
	dcmotor_init(3);
	unipolar_init(0);
	unipolar_init(1);
	servomotor_init(0);
	servomotor_init(1);
	encoder_init(0);
	encoder_init(1);
 
///////////////////////////  Programmi tsükkel //////////////////////////////// 
    while (true)
    {
        new_value0 = pin_get_value(buttons[0]);
        if ((new_value0) && (!old_value0))
        {
            //nupp = PINC & 0x07;
            nupp++;
            if (nupp>4) nupp=0;
            pin_toggle(leds[0]);
			lcd_gfx_clear();
			for (i = 0; i < 10; i++)
    		{
			pin_set(buzzer);
			hw_delay_ms(1);
			pin_clear(buzzer);
			hw_delay_ms(1);
			}
 
        }
        old_value0 = new_value0;
 
        //pin_clear(buzzer);
		//sprintf(t,"%d",nupp);
 
        if (nupp==0){
            sprintf(caption0, "   Robotic     ");
            sprintf(caption1, "  HomeLab v5   ");
            sprintf(adc0, "S1 MODULES   ");
            sprintf(adc1, "S2 DEVICES   ");
            sprintf(adc2, "S3 FUNCTIONS ");
        }
////////////////////////// Kasutajaliidese moodul /////////////////////////////////////////////////////
 
        else if (nupp==1){
                new_value = pin_get_value(buttons[1]);
                if ((new_value) && (!old_value))
                {
                    //bit_invert(nupp1,0);
					nupp1=1;
                    lcd_gfx_clear();
                }
                old_value = new_value;
 
                new_value2 = pin_get_value(buttons[2]);
                if ((new_value2) && (!old_value2))
                {
                    //bit_invert(nupp2,0);
                    lcd_gfx_clear();
					nupp1=2;
                }
                old_value2 = new_value2;
 
                if (nupp1==1) {
					pin_clear(leds[1]);
					pin_set(leds[2]);
					lcd_taust=1;
                    sprintf(caption1, "       S2   ");
                }
                else if (nupp1==2) {
					pin_set(leds[1]);
					pin_clear(leds[2]);
					lcd_taust=0;
                    sprintf(caption1, "       S3   ");
                }
                else {
					pin_set(leds[1]);
					pin_set(leds[2]);
                    sprintf(caption1, "       S1   ");
					nupp1=0;
                }                
				//segment_display_write(loendur++ % 10);
 
				sprintf(adc0, "S1: %d ", pin_get_value(buttons[0]));
                sprintf(adc1, "S2: %d ", pin_get_value(buttons[1]));
                sprintf(adc2, "S3: %d ", pin_get_value(buttons[2]));
				sprintf(caption0, "   UI module  ");
                hw_delay_ms(100); 
        }
////////////////////////// Andurite moodul /////////////////////////////////////////////////////
 
        else if (nupp==2){
                new_value = pin_get_value(buttons[1]);
                if ((new_value) && (!old_value))
                {
                    bit_invert(nupp1,0);
                    lcd_gfx_clear();
                }
                old_value = new_value;
 
                if (nupp1) {
                    pin_clear(sensors);
                    pin_clear(leds[0]);
                    sprintf(caption1, "   on-board   ");
                    sprintf(adc0, "Sound: %d ADC  ", adc_get_average_value(0, 4));
                    sprintf(adc1, "Light: %d ADC  ", adc_get_average_value(1, 4));
                    sprintf(adc2, "Temp.: %d C    ", thermistor_calculate_celsius(adc_get_average_value(2, 4)));
                }
                else {
                    pin_set(sensors);
                    pin_set(leds[0]);
                    sprintf(caption1, "   external   ");
                    sprintf(adc0, "IR: %d cm     ", ir_distance_calculate_cm(GP2Y0A21YK, adc_get_average_value(3, 4)) );
                    sprintf(adc1, "UH: %d cm    ", ultrasonic_measure_srf05(srf05));
                    sprintf(adc2, "EX: %d ADC    ", adc_get_average_value(1, 4));
                }
                //segment_display_write(adc_get_average_value(3, 4) * 10 / 1024);
                sprintf(caption0, "Sensor module ");
                hw_delay_ms(500); 
        }
///////////////////////// Mootorite moodul  /////////////////////////////////////////////////////////7
        else if (nupp==3){
 
                sprintf(caption0, "   Motors    ");
                sprintf(caption1, "   module    ");
                sprintf(adc0, "DC    %d dir   ", dcpulse );
                sprintf(adc1, "Servo %d pos   ", servopos );
                sprintf(adc2, "Step. %d dir   ", dcpos );
 
                servopos++;
                if (servopos>10) { 
                    servopos=-10;  
                    dcpos=dcpos*-1;  
                    pin_toggle(leds[0]);            
                }
 
                dcmotor_drive(0,dcpos);
				dcmotor_drive(1,dcpos);
				dcmotor_drive(2,dcpos);
				dcmotor_drive(3,dcpos);
 
				dcpulse=encoder_get_pulses(0);
 
                servomotor_position(0,servopos*10);
				servomotor_position(1,servopos*10);
 
				unipolar_halfstep(1, dcpos, 100, 3);
				unipolar_halfstep(0, dcpos, 100, 3);
 
 
        }
////////////////////////////// Kommunikatsioonimoodul  ////////////////////////////////////////////////////7
        else if (nupp==4){
 
			new_value = pin_get_value(buttons[1]);
			if ((new_value) && (!old_value))
            {
            	count = 0;			//keskmine nupp alustab otsast peale ZigBee otsimist
                lcd_gfx_clear();
            }
            old_value = new_value;
 
			sprintf(caption0, "Communication");
            sprintf(caption1, "   module    ");
            sprintf(adc0, "              ");
            sprintf(adc1, "              ");
            sprintf(adc2, "              ");
 
			if (count == 0)
			{
				lcd_gfx_clear();
	    		lcd_gfx_goto_char_xy(0, 0);
	    		lcd_gfx_write_string("Communication");
				lcd_gfx_goto_char_xy(3, 1);
				lcd_gfx_write_string("module");
				lcd_gfx_goto_char_xy(3, 3);
				lcd_gfx_write_string("wait...");
 
	    		zigbee_find_nodes(port, nodelist, ZIGBEE_MAX_NODES);	//Otsib ja salvestab ümbritsevate ZigBee moodulite info
				lcd_gfx_goto_char_xy(0, 2);
				lcd_gfx_write_string("Found:        ");
				lcd_gfx_goto_char_xy(0, 3);
				lcd_gfx_write_string("              ");
				zigbee_lcd_show_nodes(3, nodelist, ZIGBEE_MAX_NODES);	//Kirjutab leitud ZigBee moodulite info LCD-le
				hw_delay_ms(2000);
 
				// Kuvab ekraanil ühendamise teate
				lcd_gfx_goto_char_xy(0, 2);
			    lcd_gfx_write_string("              ");
				lcd_gfx_goto_char_xy(0, 3);
			    lcd_gfx_write_string("Connecting... ");
			    lcd_gfx_goto_char_xy(0, 4);
			    lcd_gfx_write_string((nodelist + adr)->address64l);	//kuvab ainult aadressi tagumise osa (8 tähemärki)
 
				zigbee_set_destination(port, &nodelist[adr]);			//Seadistab ZigBee saatma infot esimesele leitud mooduli aadressile
				count = 1;
			}
			else
			{
				sprintf(adc0, "Connected     ");		//ühendus loodud
				sprintf(adc1, (nodelist + adr)->address64l);	//millega
 
		        if (usart_has_data(port))	// loe kas USART'st andmed tulnud
		        {
		            pin_toggle(leds[usart_read_char(port) - 'A']);	// loe bait, teisenda leds massiivi indeksiks, ja muuda väljundit.
		        }
				else						//kui ei ole andmeid, siis ise saadab
				{
					usart_send_char(port, 'A');		//lülitab ümber teisel moodulil LED-i
					hw_delay_ms(1000);				//vilkumiseks
				}
			}
        }
 
        else {
            pin_toggle(led_red);
            hw_delay_ms(500); 
        }
////////////////  OUTPUT to LCD ////////////////////////////////////////////////////////        
 
        //lcd_gfx_goto_char_xy(0, 2);
        //lcd_gfx_write_string(t);
		segment_display_write(nupp);
		lcd_gfx_backlight(lcd_taust);
        lcd_gfx_goto_char_xy(0, 0);
        lcd_gfx_write_string(caption0);
        lcd_gfx_goto_char_xy(0, 1);
        lcd_gfx_write_string(caption1);
        lcd_gfx_goto_char_xy(0, 3);
        lcd_gfx_write_string(adc0);
        lcd_gfx_goto_char_xy(0, 4);
        lcd_gfx_write_string(adc1);
        lcd_gfx_goto_char_xy(0, 5);
        lcd_gfx_write_string(adc2);
 
    }
}
 
void SPI_init(void)		// Set MOSI and SCK output, all others input
{
	SPCR |= ((1<<SPE)|(1<<MSTR)|(1<<SPR1));	//enable SPI, Master, clock rate fck/4
	SPSR |= (1<<SPI2X);						//2x speed, kokku SPI kell fck/32
}
 
void SPI_write(unsigned char data)
{
	SPDR = data;
	while (!(SPSR & 0x80));		// wait for data transfer to be completed
}