Thread: What is the difference between declaring global or static local variables ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    Sorry, I should have put the whole code so you know what I need the function pointer for.

    Line# 97 & 122.

    I just want this function pointer to take care of all the functions in this code, I don't want to have multiple ones.

    Its job is at line# 90.

    The function pointer is called whenever the task lock is ON means there still work to do, when it's finished the lock is OFF.

    Here is the code:

    Code:
    /*    Title:    function pointers    Date:    Thu, 30 July 2020
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <string.h>
    #include <stdbool.h>
    #include <malloc.h>
    #include <math.h>
    
    // enumerations
    enum TASK_STATES{CMD, DATA, STRING};
    enum TASK_STATES state;
    
    // delay variables
    uint32_t dly_st, delay_period;
    bool dly_lock, lock;
    bool write_lock;
    uint16_t i, counter, counts;
    
    // command / data variables
    uint8_t str_buf;
    const char *str1 = {"hi how are you ?"};
    // basic pixel variables
    static uint8_t y,x, task_buffer[4];
    static bool row2_4, graphics;
    
    // functions
    void task_switcher(void);
    void task1(void);
    void task2(void);
    void write_chars(uint8_t row, uint8_t col, const char *str);
    void graphics_set_mode(bool mode);
    void transmit(uint8_t cmd);
    void delay_routine(void);
    
    // function pointers
    void (*fun_ptr)(void);      // main function pointer
    
    int main(){
    
        write_chars(0,0,str1);
    
        return 0;
    }
    
    void task_switcher(void){
        if(!dly_lock){// && lock
    //////////////////////////////////////////////////////
    // lcd transmisstion commands/data
            switch(state){
                case CMD:
                    if(counter<2){
                        transmit(task_buffer[counter]);
                        counter++;
                    }
                    else if(counts == 2){write_lock = 0;counts = 0;}
                    else{state = DATA;}
                break;
                case DATA:
                    if(counter<4){
                        transmit(task_buffer[counter]);
                        counter++;
                    }
                    else{
                        write_lock = 0;
                        counter = 0;
                        state = CMD;
                        printf("\n");
                    }
                break;
    
    
                case STRING:
                    if(i<16){
                        transmit(str_buf);
                        i++;
                        state = STRING;
                    }
                    else{
                        write_lock = 0;
                    }
    
    
                break;
            }
        }
        else {delay_routine();}           // first check delay flag
        if(write_lock){task_switcher();}  // still between cmd1-data2
        else if(lock){fun_ptr();}         // method2 function pointer
        else if(!write_lock){lock = 0;}   // finished writes ? : clear lock
    }
    
    
    void write_chars(uint8_t row, uint8_t col, const char *str){
        if(!lock){
            fun_ptr = &write_chars;               // <<------------- here
            state = CMD;
            if(graphics){graphics_set_mode(0);}
            i=0;
            counts = strlen(str);
            lock = 1;
        }
        // identify row/column location
        else{
            if        (row == 1)task_buffer[0] = (0x80 + (col - 1));
            else if    (row == 2)task_buffer[0] = (0x90 + (col - 1));
            else if    (row == 3)task_buffer[0] = (0x88 + (col - 1));
            else if    (row == 4)task_buffer[0] = (0x98 + (col - 1));
        }
    
    
        printf("%d",strlen(str));
    
    
    }
    
    
    // graphics mode
    void graphics_set_mode(bool mode){
        if(!lock){
            fun_ptr = &graphics_set_mode;   // <<------------- and here
            state = CMD;
            task_buffer[0] = 1;
            write_lock = 0;
            lock = 1;
        }
        if(mode){
            task_buffer[0] = 11;
            task_buffer[1] = 22;
            graphics = 1;
        }
        else{
            task_buffer[0] = 33;
            task_buffer[1] = 44;
            graphics = 0;
        }
        write_lock = 1;
        task_switcher();
    }
    
    
    void task2(void){
        if(!lock){
            fun_ptr = &task2;
            state = CMD;
            x = 0; y = 0; counter = 0;
            lock = 1; row2_4 = 0; write_lock = 0;
        }
    
    
        if(y<2 && !row2_4){         // 1st half of lcd
            task_buffer[0] = y;   task_buffer[1] = x;
            task_buffer[2] = 'E';     task_buffer[3] = 'F';
        }
    
    
        if(y<4 && row2_4){         // 2nd half of lcd
            task_buffer[0] = y;   task_buffer[1] = x;
            task_buffer[2] = 'G'; task_buffer[3] = 'H';
        }
        x++; if(x>7){y++;x=0;}
        if(y>1){row2_4 = 1;}
        if(y>3){row2_4 = 0;lock = 0;}
    
    
        write_lock = 1;
        task_switcher();
    }
    
    
    void task1(void){
        if(!lock){
            fun_ptr = &task1;
            state = CMD;
            x = 0; y = 0; counter = 0;
            lock = 1; row2_4 = 0; write_lock = 0;
        }
    
    
        if(y<2 && !row2_4){         // 1st half of lcd
            task_buffer[0] = y;   task_buffer[1] = x;
            task_buffer[2] = 'A'; task_buffer[3] = 'B';
        }
    
    
        if(y<4 && row2_4){         // 2nd half of lcd
            task_buffer[0] = y;   task_buffer[1] = x;
            task_buffer[2] = 'C'; task_buffer[3] = 'D';
        }
        x++; if(x>7){y++;x=0;}
        if(y>1){row2_4 = 1;}
        if(y>3){row2_4 = 0;lock = 0;}
    
    
        write_lock = 1;
        task_switcher();
    }
    
    
    // command tx
    void transmit(uint8_t cmd){
        if(state == CMD)      {printf("%d\t",cmd);}
        else                   {printf("%c\t",cmd);}
    }
    
    
    // delay routine
    void delay_routine(void){if(dly_lock){dly_lock = 0;}}
    Last edited by wolfrose; 08-04-2020 at 07:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do the global and local variables use the RAM?
    By YuminZhao in forum C++ Programming
    Replies: 2
    Last Post: 09-04-2011, 10:45 AM
  2. Declaring Global Variables
    By renanmzmendes in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2008, 01:50 PM
  3. Local vs Global Variables
    By BENCHMARKMAN in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2007, 05:17 AM
  4. Replies: 2
    Last Post: 10-02-2004, 10:12 AM
  5. Replies: 5
    Last Post: 06-01-2002, 11:24 PM

Tags for this Thread