Monday, 10 February 2014

Create button from Control ( customize button ) " Using C# "

Just Save as c# class after making your own changes & and add as user control...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowEvents
{
    enum MouseState {none, down, up, over, leave };

    public class PictureButton : Control
    {
        MouseState mouseState;
        public Color _color { get; set; }
        Color _colorBase { get; set; }
         Color _colorBorder { get; set; }

        SolidBrush _brush { get; set; }
        SolidBrush _brushBase { get; set; }
        SolidBrush _brushBorder { get; set; }

        SolidBrush _brushTmp = new SolidBrush(Color.Black);


        public PictureButton()
        {

            mouseState = MouseState.none;
            _colorBase =  Color.Green;
            _colorBorder = Color.FromArgb(150, _colorBase);
            _color = Color.FromArgb(200, _colorBorder);

            _brushBase = new SolidBrush(_colorBase);
            _brush = new SolidBrush(_color);
            _brushBorder = new SolidBrush(_colorBorder);

            this.DoubleBuffered = true;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            mouseState = MouseState.down;
            Invalidate();
           
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            mouseState = MouseState.up;
            Invalidate();
        }

        protected override void OnMouseHover(EventArgs e)
        {
            mouseState = MouseState.over;
            Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            mouseState = MouseState.none;
            Invalidate();
        }

        // Override the OnPaint method to draw the background image and the text.
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(ClientRectangle);
            this.Region = new Region(path);

            switch(mouseState)
            {
                case MouseState.none:
                    g.FillRectangle(_brushBorder, ClientRectangle);
                    g.FillRectangle(_brushBase, ClientRectangle.X + 4, ClientRectangle.Y + 4, ClientRectangle.Width - 8, ClientRectangle.Height - 8);
                    break;
                case MouseState.down:
                    using (PathGradientBrush gradient = new PathGradientBrush(path))
                    {
                   //     gradient.CenterPoint = new PointF(ClientSize.Width / 2, ClientSize.Height / 2);
                   //     gradient.CenterColor = Color.Honeydew;
                    //    gradient.SurroundColors = new Color[] { _brushUp.Color };
                        g.FillRectangle(_brushBase, ClientRectangle);
                    }
                    break;
                case MouseState.up:
                    using (PathGradientBrush gradient = new PathGradientBrush(path))
                    {
                        g.FillRectangle(_brushBorder, ClientRectangle);
                        g.FillRectangle(_brushBase, ClientRectangle.X + 4, ClientRectangle.Y + 4, ClientRectangle.Width - 8, ClientRectangle.Height - 8);
                    }
                    break;
                case MouseState.over:
                    using (PathGradientBrush gradient = new PathGradientBrush(path))
                    {
                        g.FillRectangle(_brushBorder, ClientRectangle);
                        g.FillRectangle(_brushBase, ClientRectangle.X + 4, ClientRectangle.Y + 4, ClientRectangle.Width - 8, ClientRectangle.Height - 8);
                    }
                    break;
            }

            // Draw the text if there is any.
            if (this.Text.Length > 0)
            {
                SizeF size = e.Graphics.MeasureString(this.Text, this.Font);

                // Center the text inside the client area of the PictureButton.
                e.Graphics.DrawString(this.Text,
                    this.Font,
                    new SolidBrush(this.ForeColor),
                    (this.ClientSize.Width - size.Width) / 2,
                    (this.ClientSize.Height - size.Height) / 2);
            }

            base.OnPaint(e);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // PictureButton
            //
            this.Text = " = ";
            this.ResumeLayout(false);
        }
    }
}

Saturday, 15 December 2012

linked list operations sample code with functionalities (createList, display, delete by data, delete all, reverse list insertAt(int position, insertAtStart)

#include <stdio.h>
#include <stdlib.h>

struct node
{

    int info;
    struct node * link;
};

struct node * start=NULL;
void createList(int);
void display(void);
void insertAtStart(int data);
void insertAt(int position, int data);
void delByData(int data);
int del(void);
void reverseList(void);

int main()
{
    int choice=0, n, m, i, position;

    while(1)
    {

    printf("1.Create list\n");
    printf("2.Display\n");
    printf("3.Quit\n");
    printf("4.Insert at start\n");
    printf("5.Insert at position\n");
    printf("6.del by data\n");
    printf("7.del list\n");
    printf("8.Reverse list\n");

    scanf("%d", &choice);

    switch(choice)
    {
        case 1:
            system("clear");
  //          start = NULL;
            printf("How many nodes u want\n");
            scanf("%d", &n);
            for(i=0 ; i<n ; i++)
            {
                printf("enter element\n");
                scanf("%d", &m);
                createList(m);
            }
            break;
        case 2:
            system("clear");
            display();
            break;
        case 3:
            del();
            exit(0);
        case 4:
            system("clear");
            printf("enter element\n");
            scanf("%d", &m);
            insertAtStart(m);
            break;
        case 5:
            system("clear");
            printf("enter element position\n");
            scanf("%d", &position);
            printf("enter element data\n");
            scanf("%d", &m);
            insertAt(position, m);
            break;
        case 6:
            system("clear");
            printf("enter element data to delete\n");
            scanf("%d", &position);
            delByData(position);
            break;
        case 7:
            system("clear");
            if(del())
            {
                printf("successfully list deleted\n");
            }
            break;
        case 8:
            system("clear");
            reverseList();
            break;
        default:
            system("clear");
            printf("wrong choice\n");
            break;
    }
    }

    return 0;
}

void reverseList(void)
{
    struct node *p1, *p2, *p3;

    p1 = start;
    p2 = p1->link;
    p3 = p2->link;

    p1->link = NULL;
    p2->link = p1;

    while(p3 != NULL)
    {
        p1 = p2;
        p2 = p3;
        p3 = p3->link;
        p2->link = p1;
    }
    start = p2;
}

int del(void)
{
    struct node *tmp = start, *q;
    while(tmp != NULL)
    {
        q = tmp;
        tmp = tmp->link;
        free(q);
    }
    return 1;
}

void delByData(int data)
{
    struct node * tmp, *q;
    tmp = start;

    while(tmp->link != NULL)
    {
        if(tmp->link->info != data)
        {
            tmp  = tmp->link;
        }
        else
        {
            q = tmp->link;
            tmp->link = tmp->link->link;
            free(q);
            return;
        }
    }
}

void createList(int data)
{
    struct node *tmp, *q;
    tmp = malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=NULL;

    if(start==NULL)
    {
        start = tmp;
    }
    else
    {
        q = start;
        while(q->link!=NULL)
            q=q->link;
        q->link = tmp;
    }
}

void display(void)
{
    struct node *ptr=start;
    while(ptr != NULL)
    {
        printf("%d ", ptr->info);
        ptr = ptr->link;
    }
    printf("\n\n");
}

void insertAtStart(int data)
{
    struct node * tmp;
    tmp = malloc(sizeof(struct node));
    tmp->link = start;
    tmp->info = data;
    start = tmp;
}

void insertAt(int position, int data)
{
    struct node * tmp, * ptr = start;
    int count=1;

    while(ptr != NULL)
    {
        if(count != position)
        {
            ptr = ptr->link;
            count++;
        }
        else if(position > 1)
        {
            tmp = malloc(sizeof(struct node));
            tmp->link = ptr->link;
            ptr->link = tmp;
            tmp->info = data;
            return;
        }
        else if(position == 1)
        {
            tmp = malloc(sizeof(struct node));
            tmp->link = start;
            start = tmp;
        }
    }
    printf("no such position found..\n");
}

Friday, 24 August 2012

1ms TIMER0 led blinky(LPC21xx)




#include "LPC214x.H"
//CCLK=12xM=12x5=60MHZ
//PCLK=CCLK (VPBDIV=0x01)

#define LIMIT_1_MS 59999     //PCLK * X    = 1ms, ie X = 1ms/60mHz, ie X = 60000
void Timer_ISR(void) __irq;

unsigned int ms_count, flag = 0;
int main()
{
VPBDIV = 0X01;
//int timer0
T0TCR = 0x00;
T0TC = 0x0;
T0PC = 0x0; //The Prescale Counter is incremented on every PCLK. When it reaches the value
            //stored in the Prescale Register, the Timer Counter is incremented and
            //the Prescale Counter is reset on the next PCLK.
T0PR = 0x0;    //Prescale Register    set to zero
T0MR0 = LIMIT_1_MS;  //Match Register 0. MR0 can be enabled through the
                //MCR to reset the TC, stop both the TC and PC,
                //and/or generate an interrupt every time MR0 matches the TC.
T0MCR = 0x03;    //bit 0- Interrupt on MR0: an interrupt is generated when MR0 matches the value in the TC.
                //bit 1- Reset on MR0: the TC will be reset if MR0 matches it.

//interrupt
VICVectCntl11 = 0x00000024;
VICVectAddr11 = (unsigned int) Timer_ISR;
VICIntEnable = 0x00000010;

T0TCR = 0x01;

IO0DIR = 0x80000000;

    while(1)
    {
    }
}

void Timer_ISR(void) __irq
{
     if(ms_count == 1000)
    {
        if(flag == 0)
        {
            IO0CLR = 0x80000000;
            flag = 1;
            }
        else if(flag == 1)
        {
            IO0SET = 0x80000000;
            flag = 0;   
            }
        ms_count = 0;
    }

    ms_count++;
    T0IR = 0x01;
    VICVectAddr = 0x00;
}

Wednesday, 22 August 2012

Simple SPI0 - LPC21


led module can access using SPI0

Ref: BlueBoard_schematics_V1R1-3(LPC2148)


------------------------------------------------------------
#include <LPC214X.H>

void spiInit(void);
void spiSend(unsigned short);
void delay(void);

#define SPIF (1<<7)
#define DATA 0xFF


int main()
{
int i=1;
spiInit();

while(1) /*rotating led*/
{
spiSend(i);
delay();

i <<= 1;
if(i==0x0100)
i=1;
}
}

void spiInit()
{
PINSEL0=0x01500; /*MOSI0-bit 13:12 = 01, MISO0-bit 11:10 = 01, SCK0-bit 9:8 = 01 ,(SSEL0-not enabled, it is generated from code)*/ // ..,0001,0101,0000,0000 => 0x1500
IO0SET = 1<<7;   /* chip sel kept high*/
IO0DIR |= 1<<7; /* chip sel dir to output*/
S0SPCCR=0x8; /* SPI Clock Counter Register. This register controls the frequency of a master’s SCK0 */
S0SPCR=0x20; /*SPI Control Register - Device selected as master */
}

void spiSend(unsigned short data)
{
IOCLR0 = 1<<7; // enable slave select pin SSEL0
S0SPDR = data;
while (!(S0SPSR & SPIF)); /*SPI Status Register-SPI transfer complete flag. When 1, this bit indicates when a SPI
data transfer is complete */
IOSET0 = 1<<7; // disable slave select pin SSEL0
}

void delay(void)
{
int i, j;
  for(i=0; i<3000; i++)
  for(j=0; j<300; j++);
}
-----------------------------------------------------------------------------

External Interrupt - (LPC2148)

External Interrupt - BlueBoard_schematics_V1R1-3(LPC2148)

aim - when ext pin press buzzer rings



----------------------------------------------
eint2.h
----------------------------------------------


#ifndef __EINT2_H_
#define __EINT2_H_

#define EINT2_PINSEL PINSEL0
#define EINT2_PIN (1<<31)
#define EINT2_CHNL (1<<16)
#define LED_PIN (1<<25) //buzzer
#define LED_DIR IO1DIR
#define LED_CLR IO1CLR
#define LED_SET IO1SET

void initGPIO(void);
void initEINT2(void);
void beep(void);
void delay(void);
void lowBeep(void);
void lowDelay(void);

#endif


--------------------------------------------------
eint2.c
--------------------------------------------------

#include "Eint2.h"
#include <LPC214X.H>

void initGPIO(void)
{
LED_DIR |= LED_PIN; //setup buzzer
LED_SET = LED_PIN;
}

void ExtInt_ISR(void) __irq
{
EXTINT   = 0x00000004;
lowBeep();
VICVectAddr = 0x00;
}

void initEINT2(void)
{
EINT2_PINSEL |= EINT2_PIN;
VICDefVectAddr = (unsigned)ExtInt_ISR;
VICIntEnable |= EINT2_CHNL;
}

void beep()
{      
LED_CLR |= LED_PIN;
lowDelay();
LED_SET |= LED_PIN;
lowDelay();
}

void lowBeep()  //buzzer beeping
{      
LED_CLR |= LED_PIN;
lowDelay();
LED_SET |= LED_PIN;
lowDelay();
}

void delay()
{
int i=0, j=0;
for(i=0;i<3000;i++)
for(j=0;j<2000;j++);
}

void lowDelay()
{
int i=0, j=0;
for(i=0;i<3000;i++)
for(j=0;j<50;j++);
}


-------------------------------------------------------------------------
main.c
-------------------------------------------------------------------------

#include <LPC214X.H>
#include "Eint2.h"

main()
{
initGPIO();
initEINT2();

lowBeep();
while(1)
{
// beep();
}
}
-----------------------------------------------------------------------------

LPC2148- lcd interface sample code




---------------------------------------------------------------------------
lcd.c
------------------------------------------
#include <LPC214X.H>
#include "lcd.h"

void lowDelay()
{
int i=0,j=0;
for(i=0;i<3000;i++)
for(j=0;j<1000;j++);
}
void lowDelay1()
{
int i=0,j=0;
for(i=0;i<3000;i++)
for(j=0;j<10;j++);
}

void delay(int count)
{
  int j=0,i=0;
  for(j=0;j<count;j++)
  {
    /* At 60Mhz, the below loop introduces
    delay of 1000 us */
    for(i=0;i<350;i++);
  }
}

void lowBeep()
{      
IO1CLR |= (1<<25);
lowDelay1();
IO1SET |= (1<<25);
lowDelay();
}

void wait()
{
IO1CLR |= LCD_RS;
IO1SET |= LCD_RW|LCD_EN;
delay(1000);
while(IO0PIN & (1<<13));
IO1CLR |= LCD_EN;
}

void lcdWriteCmd(unsigned char cmd)
{
unsigned int tmp;
unsigned char mask = 0xf;
IO1CLR |= LCD_RS|LCD_RW;


delay(100);
tmp = (cmd>>4) & 0x0000000f;
IO0CLR |= mask<<10;
IO0SET |= tmp<<10;
IO1SET |= LCD_EN;
delay(10);
IO1CLR |= LCD_EN;
// wait();

delay(100);
tmp = (cmd) & 0x0000000f;
IO0CLR |= mask<<10;
IO0SET |= tmp<<10;
IO1SET |= LCD_EN;
delay(10);
IO1CLR |= LCD_EN;
// wait(); //
}

void lcdWriteDat(unsigned char cmd)
{
unsigned int tmp;
unsigned char mask = 0xf;
IO1SET |= LCD_RS;
IO1CLR |= LCD_RW;


delay(100);
tmp = (cmd>>4) & 0x0000000f;
IO0CLR |= mask<<10;
IO0SET |= tmp<<10;
IO1SET |= LCD_EN;
delay(10);
IO1CLR |= LCD_EN;
// wait();

delay(100);
tmp = (cmd) & 0x0000000f;
IO0CLR |= mask<<10;
IO0SET |= tmp<<10;
IO1SET |= LCD_EN;
delay(10);
IO1CLR |= LCD_EN;
// wait(); //
}

void strOut(char * data)
{
while(*data)
{
lcdWriteDat(* data);
data++;
}
}

void initLcd()
{
IO1DIR |= LCD_EN|LCD_RW|LCD_RS ;
IO0DIR |= D7_LCD|D6_LCD|D5_LCD|D4_LCD;

delay(1000);
  lcdWriteCmd(0x28);     /*   4-bit interface, two line, 5X7 dots.        */
  lcdWriteCmd(0x01);     /*   LCD clear                                   */
  lcdWriteCmd(0x02);     /*   cursor home                                 */
  lcdWriteCmd(0x06);     /*   cursor move direction                       */
  lcdWriteCmd(0x0C);    /*   display on      */
//   lcdWriteCmd(0x0F);
}
--------------------------------------------------------------------------------------------------
lcd.h
-----------------------------------------------------------------------
#ifndef __LCD__H__
#define __LCD__H__

#define LCD_EN (1<<22)
#define LCD_RW (1<<23)
#define LCD_RS (1<<24)
#define D7_LCD (1<<13)
#define D6_LCD (1<<12)
#define D5_LCD (1<<11)
#define D4_LCD (1<<10)


void initLcd(void);
void lcdWriteCmd(unsigned char cmd);
void lcdWriteDat(unsigned char cmd);
void strOut(char *);
void lowDelay(void);
void lowDelay1(void);
void delay(int count);
void wait(void);

void lowBeep(void);

#endif
------------------------------------------------------------------------------------------------------
main.c
---------------------------
#include "lcd.h"
#include <LPC214X.H>


main()
{
  
initLcd();  
delay(1000*10);
   
IO1DIR |= (1<<25);

lowBeep();
lcdWriteCmd(0x01);
delay(1000*10);
strOut("Hello LCD...");
delay(1000*100);
  while(1)
{
// delay(1000*6);
// lcdWriteCmd(0x10);
// lcdWriteDat(0x2d);
//
// delay(1000*6);
// lcdWriteCmd(0x10);
// lcdWriteDat(0x7c);
//
// delay(1000*6);
// lcdWriteCmd(0x10);
// lcdWriteDat(0x2f);
 
}
}
------------------------------------------------------------------------