Tuesday, December 28, 2010

Python files in openERP

All the necessary files needed for creating the module and connecting with the  view lies under the base module. Base module contain a folder called osv in which all the functions and classes needed for creating a model is defined here. So first of all Import fields and osv from osv.

To define a new object, you have to define a new Python class then instantiate it. This class must inherit from the osv class in the osv module.The first line of the object definition will always be of the form:
An object is defined by declaring some fields with predefined names in the class. Two of them are required (_name and _columns), the rest are optional. The predefined fields are:
  • _auto
  • _columns (required)
  • _constraints
  • _sql_constraints
  • _defaults
  • _inherit
  • _inherits
  • _log_access
  • _name (required)
  • _order
  • _rec_name
  • _sequence
  • _sql
  • _table
In the database, tables will be created with the table name given in the _name field (all the dot"." will be replaced by underscore"_" in table creation)

Fields for the database table is specified inside the "_columns". Columns is an associative array(dictionary) in which we specify the field name as the key and field type as the value.

Types of fields
Basic types
boolean:
A boolean (true, false).
Syntax:
fields.boolean('Field Name' [, Optional Parameters]),
 
integer:
An integer.
Syntax:
fields.integer('Field Name' [, Optional Parameters]),
 
float:
A floating point number.
Syntax:
fields.float('Field Name' [, Optional Parameters]),
char:
A string of limited length. The required size parameter determines its size.
Syntax:
fields.char(
        'Field Name',
        size=n [,
        Optional Parameters]), # where ''n'' is an integer.
text:
A text field with no limit in length.
Syntax:
fields.text('Field Name' [, Optional Parameters]),
 
date:
A date.
Syntax:
fields.date('Field Name' [, Optional Parameters]),
 
datetime:
Allows to store a date and the time of day in the same field.
Syntax:
fields.datetime('Field Name' [, Optional Parameters]),
 
binary:
A binary chain. We can import(upload) our files using binary.
selection:
A field which allows the user to make a selection between various predefined values.
Syntax:
fields.selection((('n','Unconfirmed'), ('c','Confirmed')),
                   'Field Name' [, Optional Parameters]),
 
Relational fields

  • one2many and many2one
    • An example is relationship between employee and department. A department may have so many employees and employees will belong to only one department.
    • When defining a many2one relationship in open erp, at first there must have a one2many relationship. ie the constructor which have the one2many relation must be called before calling the constructor having many2one relation.

    • {"employee":fields.one2many("employee.class","department_id","Employees")}

In class department we define the field as dictionary values, “employee” as the key, “fields.one2many()” as the value with 3parameters. First parameter is the other class name(here employee.class ), then the key name of the other class which has the many2one as its value(here employee_id), then the field name.
    • {"department_id":fields.many2one("department.class","Department"),}

            In class employee we define the field as dictionary values, “department_id” as the key, “field.many2one()” as the value with 2parameters. First parameter is the other class name(here is the department.class) then the field name.

  • many2many
    • Example is the relationship between user and group. Users may belong to so many groups and a group may have so many users.
    • For showing a many2many relationship, we have to create a new relationship table in the database with fields user_id and group_id.
    • If we want a many2many relation between two classes say class abc and class xyz then we have to first define both classes. Now add the many2many relationship in the class which secondly created( here class xyz). Now define a new class which inherit the properties of the class abc. Define the many2many relationship here in the inherited class.
  
    • { 'user_ids': fields.many2many('group.class','user_group_rel', 'user_id','group_id','Users')}
    • The parameters are
      • other class name
      • relationship table name
      • my_relation_id
      • other_relation_id
      • field name

OpenERP(open Enterprise Resourse Planning)

        OpenERP is an open source comprehensive suite of business applications including Sales, CRM(Customer Relationship management), Project management, Warehouse management, Manufacturing, Accounting and Human Resources. OpenERP has separate client and server components. XML-RPC interfaces are available. It is based on a strong MVC architecture, flexible workflows, a dynamic GUI, an XML-RPC interface, and customizable reporting system with convenient OpenOffice.org integration.

An OpenERP system has three main components :
  1. PostgreSQL dataserver which has all the databases
  2. OpenERP application server that has all of the enterprise logic
  3. web server, a distinct application called the Open Object client-web, which allows you to connect to OpenERP from any web browser. This is not needed if the user connect through a GTK client.
         The server part is written in Python. Business functionality is organized into "modules". A module is a folder with a pre-defined structure containing Python code and XML files. A module defines data structure, forms, reports, menus, procedures, workflows etc. The client is thin as it contains no business logic. 

Working 
        I am using eclipse IDE for editing and using openERP version 5.0.14. We can download the server and client package from www.openerp.com , unzip the files and start the server. To start the server first go the openerp-server folder, then to the bin folder. There you can find the openerp-server.py 

     will run the server. Same process for running the client. We can find a file named openerp-client.py inside the bin folder present in the client. Provide the "-v" option to get the complete details of the module fields when mouse pointer pointed to it. Then a client interface with a pop-up window for login. If the database is not present or server is not yet started a error message will be shown. In openERP for the database creation postgresql is used. If there is no error then interface with all the functionalies will be shown.
This is the login window.
        Open eclipse and select the work space. The server code must be inside the workspace folder found in the home folder. After opening the eclipse go to the folder /bin/addons inside our openerp-server-5.x.x. The addons folder contains all the modules created by the openerp team. We have to add our module in this folder. Create a new directory and inside this directory we have to create all supporting files needed for our module.

Basic files needed for a module are
  1. __init__.py  -->In this file we have to specify the all the files needed for our module. That is we have to import all the needed *.py file names and folder names inside this init file.
  2. __terp__.py  -->This is the configuration file.Here we have to specify all the files and depending modules,demo files, author name, version etc.
  3. *.py file . --> In the .py file we define our model, and controller.
  4. *.xml file.--->The view part is defined in .xml files.
    ____________________________________________________________________________

Saturday, November 20, 2010

DRUPAL Installation guide

Installation guide for Ubuntu 10.04 users.
Requirements
        Drupal requires 3 things. A web server and we are using Apache web server, a database and we are using MySQL database and PHP5. Before installing drupal we have to install these three things. We can install apache2, php5, and mysql separately.


Much more easier way is by installing LAMP(Linux Apache Mysql Php) server.

If tasksel is not installed then you have to install it separately.

In installing apache2 if you get an error like this apache2: Could not determine the server's fully qualified domain name, using 127.0.0.1 for ServerName then


Apache2 has the concept of sites, which are separate configuration files that Apache2 will read. This file is available in /etc/apache2/sites-available/default. Edit this file and change the DocumentRoot and Directory specified here to the directory where you want to install drupal (I am installing drupal to /home/user/drupal/html. So i provide this path to this position).  Save this file and restart our apache web server.


        Create a new username and password in mysql and then create a database.  Complete privilege is given to this database. The user_name, password and database_name are needed in configuring drupal.

        After completing the installation of mysql, php and apache we can install drupal. Download the latest release of drupal from http://drupal.org/ then untar the file.




        Then move the contents from the directory drupal-x.x to the directory where you want to install drupal. I am installing drupal to /home/user/drupal/html

 

Now change the current directory to drupal installed directory. Create the configuration file and give write permissions to the configuration file.



Now open your browser and type http://localhost/. Here you have to provide the information such as language, site name, database name, username and password to complete the installation. After the successful completion of this installation a new welcome screen will be provided to you.

A new installation of Drupal have only a very basic configuration with only a few active modules and minimal user access rights. Log in as admin to enable and configure services. For example:

General Settings           Administer > Site configuration > Site information
Enable Modules            Administer > Site building > Modules
Configure Themes        Administer > Site building > Themes
Set User Permissions   Administer > User management > Permissions

By configuring the services you will be able to design a web server.

Thursday, November 11, 2010

How to develop a Game using python?

          Everyone like to play games. If we start to play a computer games sometimes we don't quit the game until we finish it. Game can be developed in  any language. I am using python for developing a game. Usual python language can be used for developing textual games. For developing graphical games we need a special module in python  called pygame.

       As the first phase of developing the graphical game we have to make a game plan and then import the pygame module and necessary modules needed for our game. 
 




Then set up the pygame using pygame.init(). If we want to use sleep function or clock then we have to initiate them also.


Next step is to set up our output window. For this we are using pygame.display.set_mode().  We can also provide a caption for our window using pygame.display.set_caption()


Now set up the color variables. Each color can be created by a tuple with three elements. The three elements are red, blue and green each starting from 0 to 255. To create a black color we have to provide tuple as (0, 0, 0) and for white (255, 255, 255). And thus we can create any color.


If we want to add a text, then first define the font. Then provide the text. TextRect will help to select a rectangle space where our text will be printed. 


We can fill a background color using

Then we can define all the geometrical figures needed for our game.


Five arguments for drawing a line. First is window surface, second color, third coordinate (x1, y1), fourth coordinate (x2, y2) and finally the width of the line. Like this we can also draw circles, rectangles, ellipse etc.

The keyword blit is used to print the text inside a rectangle which we had already created. pygame.display.update() function is needed to print every operations that have been done. Without this function nothing will be printed. This is because when we create a new object it take a small delay in drawing each object to the screen. So the update function helps to print at the end of definitions. If our game has a game loop - an infinite loop, then this update function will be inside this loop. A user can provide request using pygame.event.get(). On each loop each event is processed and produce a output according to the event.




Monday, November 8, 2010

MSP430 -Note on Timer_A

        Timers essential to almost all embedded applications. They generate a fixed period events. Timers replaces the delay loops which allows CPU to sleep.

         Watchdog timer can be found on all msp430 devices. If we enable the watchdog timer our program  keeps resetting itself. So we disable the watchdog timer and uses the Timer_A.

Timer_A
        Timer is a Asynchronous 16-bit timer/counter. It has a 16-bit timer/counter register, TAR increments or decrements with each rising edge of the clock signal. The timer can generate an interrupt when it overflows. TAR can be cleared by setting TACLR bit. It also clears the clock divider and count direction for up/down mode. The timer clock can be sourced from ACLK, SMCLK, or externally via TACLK or INCLK. The clock source is selected with the TASSELx bits. The selected clock source may be passed directly to the timer or divided by 2, 4, or 8, using the IDx bits. The timer clock divider is reset when TACLR is set.

TIMER_A Control Register

TASSELx  :  
    They are 8th and 9th bits of the register.These two bits select the source of the clock for the timer. 

IDx  :
     They are 6th and 7th bis of the register. These bits select the divider for the input clock.
MCx  :


TACLR  :
     Bit 2 of the register. This is Timer_A clear. Setting this bit resets TAR, the clock divider, and the count direction. The TACLR bit is automatically reset and is always read as zero.

TAIE  :
    Bit 1 of the register. This is Timer_A interrupt enable. Setting this bit enables TAIFG interrupt request.

TAIFG  :
    Bit 0 of the register. This is Timer_A interrupt flag. If this bit is set then interrupt is pending.

Sunday, November 7, 2010

Translation of common C constructs to MSP430 assembly code

Local Variable

                                                   
        Consider a C program which contain only one statement, assigning a local variable i to 100. The assembly code also contain only one statement. Move the value 100 to the stack.

Static Variables
         In this C program a variable i is declared as static and assign a value 100 to it. Then another value 200 assign to this value. In the assembly code we can find that the value 100 is stored in RAM and when assigning value 200, this value is replaced.
        In the assembly code mov #200, &i.1194 moves the value 200 to the address of &i.1194. If we check the address of i.1194 we can find that it is the address of RAM. And importantly the variable declared as static is defined outside the main in assembly code.
Function with Parameters

        This is the C program. There is a function fun which accepts two integers and return their sum. In the main we are calling fun(1,2).
Now go through the assembly code. First consider the main:
Here the value 1 and 2 are moved to registers r15 and r14 respectively. Then calling the address of fun. In the fun:
first moving the value in register r15 to the address in r4( r4 contain the address of stack).Then taking another position of stack to store the value of r14 and adding these two values of stack and store it in r15.Then returning this address to the main.


POINTER TO INTEGER
        This is the C program. An integer i is assigned to 10 and a pointer to an integer pointing to the address of i. Then assigning a value 20 to i using the pointer. The assembly code is given below.
        R4 contain the starting address of stack. At first the value 10 is moved to  2 positions next to the address in r4. Then the value in r4 is copied to r15. Adding 2 with the address of r15 gives the address of the variable i and moving the value 20 to the address stored in r15.

POINTER TO FUNCTION

        This is the C program. In this there is a pointer to a function 'fun' which returns void and taking no arguments. The function fun is not doing anything.
 Following is the assembly code.
        Here r4 contain the address of stack. To this address the address of the function fun is copied. The address in r4 is then moved to register r15. Then calling r15 will actually calls the function fun. Since there is nothing to do in the fun it just pushes the return address and then pop the return address and go back to main.

Thursday, November 4, 2010

Jump Table - An optimization Technique

        
         Consider a C program which contain a switch statement with 4 cases. Each case contain consecutive integers. When compiling the compiler will go through the complete code and manages to keep a count of similar codes and consecutive values. If this count is greater than a limit(say 5) then the compiler will try to simplify the code by using some optimization techniques.
        Jump table is one of the optimization technique used by the compiler. Jump Table transfers the program control from one point to another based on the branch instruction table. If the count is less than the limit then the compiler will generate assembly for each code.
       Consider a C program which contain switch statement with 15 cases. Now the compiler will go through the whole code and will generate a jump table. 
       Now we can consider the assembly code.

        Here the value to be checked is first moved to stack (r4 contain the address of stack). If this value is beyond the limit of switch statement then exit else save the value in the stack to a register. Then rotate left(rla)the value in the register and add with the address of the jump table(#.L18). By rotating and adding we will get the corresponding address of the label from the jump table. Then moving the value in the address specified in the register to another register(say r6) and calling branch of this register will jump to the corresponding label.

        The above figure is the jump table which contain all the address of the labels. The addresses of labels are saved in the consecutive memory location starting from the memory location of the address of .L18.
        If the register contain the address of .L11 then the control will go to .L11
and replaces the value to the stack and exits.

Wednesday, November 3, 2010

My assembly code to implement 'strcpy' and 'strcmp' in MSP430

         Here i am trying to implement 'strcpy' any 'strsmp' functions defined in KandR. First i am creating a dummy C code just containing main() and opening and closing braces and compiling the C code with -S option for creating the assembly code.
$msp430-gcc -mmcu=msp430x2013 filename.c -S
      Now we get a assembly code file with same filename as that of the c file with a change in extension as .s
       After inserting all the necessary assembly code to the.s file we can create the a.out file using
$msp430-gcc -mmcu=msp430x2013 filename.s

1.Strcpy

         First of all i am clearing the register r7 and after completing the copy r7 is set. This is for showing the completion of the process. Now i am moving an address of RAM  (0x200) to r5 which is the source and another address of RAM (0x210) to r6 which is the destination. Now we can start the loop with a testing that the value at memory location specified in r5 is 0 or not. If it is not zero then move the value at address specified in r5 to the address specified in r6.  Then increment r5 and r6 values (addresses). Else move zero to the memory location specified in r6 and move 1 to r7 for indicating the completion.

2.Strcmp


         Here if the compare fails then red LED blinks. R8 have the same role of r7 in strcpy. By moving 1 to the address 0x022 makes the pin 1 for output mode. 0x022 is address of P1DIR. Then i am moving the value 0 to the address 0x021 making the pin1 disabled. Now i am moving an address of RAM  (0x200) to r5 which is the source and another address of RAM (0x210) to r6 which is the destination. Now we can start the loop with a comparing that the value at memory location specified in r5 is equal to the value at memory location specified in r6 or not. If it is equal then check whether the value at address specified in r5 is equal to 0 or not. If not equal to zero then we can increment r5 and r6 or else we can move to the last step move the value at address specified in r7 to r8. If the first compare fails then move a value 1 to the address specified in r7 which blinks the red LED indicating that compare returns non-zero value.

MSP430 instruction set

        The MSP430 instruction set consists of 27 core instructions. Additionally, it supports 24 emulated instructions. The core instructions are unique opcodes decoded by the CPU, while emulated instructions need assemblers and compilers to generate their mnemonics.

There are three core-instruction formats. They are
-->Double operand
-->Single operand
-->Program flow control-Jump

Byte and word instructions can be accessed using .B or .W. If the extension is no provided then the instruction is interpreted as a word instruction

Double operand instructions

        
Single Operand Instructions


Program flow control


Emulated Instructions


         Some more core instructions and emulated instructions are there, which are not specified here.

Tuesday, November 2, 2010

MSP430G2231

        MSP430 is a product of Texas Instruments. MSP430 has a wide range of devices and we are using MSP430G2231 (MSP430 LaunchPad development tool). It is one of the smallest MSPs with only 14 pins. It has a flash memory of 2KB and a RAM of 128B. 
        
        The MSP430 CPU has a 16-bit RISC architecture. The CPU is integrated with 16 registers that provide reduced instruction execution time. The register-to-register operation execution time is one cycle of the CPU clock. Here 4 of the registers, R0-R3, are dedicated as program counter, stack pointer, status register and constant generator respectively and the remaining registers are general-purpose registers. Peripherals are connected to the CPU using data, address and control buses and can be handled with all instructions. The instruction set consists of the original 51 instructions with three formats and seven addressing modes and each instruction can operate on word and byte data. Instruction formats and addressing mode description are given below.




        MSP430 has one address space shared with special function registers, peripherals, RAM and flash memory. Code access arealways performed oneven addresses. Data can be accessed as bytes or words.

        The flash memory can be programmed via the JTAG port. The CPU can perform single-byte and single-word writes to the flash memory. Now we can move to the programming of MSP430.

        We can connect MSP430 launch pad to PC via USB. When connecting to PC, open a terminal and type the command 'dmesg' which shows all the connections that has been established. It shows a message that USB device is connected through some port. Kernel does not support the connectivity of this device. we do have only user space support. So we have to install a package called libusb for this connection. JTAG (Joint Test Architecture Group) helps in controlling the chip. All the modern PCs have the JTAG support. 

      After connecting to the PC and all necessary check has been done we can run the mspdebug. Here we have to specify the device type and MSP430G2231 belongs to rf2500.

        It will start the debugging of msp430 launch pad and will show a device name as MSP430F2013 which is compatible with MSP430G2231.  Now we have initiated a communication and JTAG resets the chip.

MSP430 commands
dis 0xf800 ->which is used to display the address. 0xf800 is the starting address of flash memory. The codes shown here are the actual machine codes.
erase -->will erase the flash memory and will occupy with 'ff'.

regs -->shows all the register  values.
mw 0x200 -->can be used to write to any memory location.
prog a.out can be used to burn the code in the executable file a.out.

        Now we can burn a program to msp430.