Announcement

Collapse
No announcement yet.

Waiting data received in serial buffer caused keyboard scanning disturbed.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Waiting data received in serial buffer caused keyboard scanning disturbed.

    I tested the keyboard routine and a serial routine separately, both are worked. But when I put both functions into single loop, the keyboard does not worked. Waiting data received in serial buffer caused keyboard scanning disturbed. How to scanning keyboard without distrubed by serial routine? Is possible to use multitasking? Thanks.

    Code:
    #CONST
           MAX_TRIED         5
           NOZZLE_QTY        8
           RX_TMOUT          2
    #END
    
    func readKeyboard()
        if(isKeyboardVisibled == TRUE)
            if(currentKeyboard== NUMERIC)
                hndl                 := numericKeyboardHandle;
                iKeyboard1           := iNumKeyboard1;
                oKeyboard1           := oNumKeyboard1;
                iKeyboard1keystrokes := iNumKeyboard1keystrokes;
            else if(currentKeyboard == QWERTY)
                hndl                 := qwertyKeyboardHandle;
                iKeyboard1           := iQweKeyboard1;
                oKeyboard1           := oQweKeyboard1;
                iKeyboard1keystrokes := iQweKeyboard1keystrokes;
            endif
            keyTouched := img_Touched(hndl, -1) ;
            if ((keyTouched >= iKeyboard1) && (keyTouched <= iKeyboard1 + oKeyboard1[KbButtons]))
                kbDown(hndl, iKeyboard1, oKeyboard1, iKeyboard1keystrokes, keyTouched-iKeyboard1, keyboardHandler);
            endif
            if (oKeyboard1[KbDown] != -1)
                kbUp(hndl, iKeyboard1, oKeyboard1);
            endif
        endif
        return keyTouched;
    endfunc
    
    func touchScanning()
        touchedStatus := touch_Get(TOUCH_STATUS);
        touchedX := touch_Get(TOUCH_GETX);
        touchedY := touch_Get(TOUCH_GETY);
    
        if(touchedStatus==TOUCH_PRESSED)
            // read touch coordinate and status
        else if(touchedStatus==TOUCH_MOVING)
            // read touch coordinate and status
        else if(touchedStatus==TOUCH_RELEASED)
            // read keyboard in here
            if(readKeyboard()>0)
                 // process data
            end func
        endif
    endfunc
    
    // Problem maybe in here (waiting data received in serial buffer caused keyboard scanning disturbed)
    func reqRealtimeStatus(var nozzleId)
        serout1(STX);               BCC := 0;
        serout1(TOUCH_REALTIME);    BCC ^= TOUCH_REALTIME;
        serout1(nozzleId + SA);     BCC ^= (nozzleId + SA);
        serout1(ETX);               BCC ^= ETX;
        serout1(BCC);
    
        mem_Set(packetIn, NULL, sizeof(packetIn));
        n    := 0;
        tmOut:= RX_TMOUT;
        while(tmOut--)
            if(com1_Count()>0)
                tmp := serin1();
                if(tmp>=0 && tmp<=0xFF)
                    packetIn[n] := tmp&0xFF;
                    if(n==0 && packetIn[n]==STX)
                        tmOut:= RX_TMOUT;
                        BCC  := 0;
                        n++;
                        continue;
                    else if(n>0 && packetIn[0]==STX)
                        tmOut:= RX_TMOUT;
                        if(n==17 && packetIn[n-1]==ETX)
                            if(packetIn[n]==BCC)
                                // realtime
                                if(packetIn[1]==TOUCH_REALTIME)
                                    if(packetIn[3]>=SA+0 && packetIn[3]<=SA+7)
                                        params[_2] := packetIn[2];                    // NozzleQty
                                        params[_3] := packetIn[3]-SA;                 // NZ_ID (0,2,4,6,8)
                                        params[_4] := packetIn[4];                    // NZ_ACTUAL_STATUS
                                        params[_5] := packetIn[5];                    // NZ_PRODUCT_ID
                                        params[_6] := packetIn[6] <<8 | packetIn[7];  // NZ_SELLING_PRICE
                                        params[_7] := packetIn[8] <<8 | packetIn[9];  // NZ_VOLUME_MSB
                                        params[_8] := packetIn[10]<<8 | packetIn[11]; // NZ_VOLUME_LSB
                                        params[_9] := packetIn[12]<<8 | packetIn[13]; // NZ_AMOUNT_MSB
                                        params[_A] := packetIn[14]<<8 | packetIn[15]; // NZ_AMOUNT_LSB
                                        serout1(ACK);
                                        return packetIn[3]-SA; // NZ_ID
                                    endif
                                endif
                            endif
                        endif
                        BCC ^= packetIn[n];
                        n++;
                    endif
                    if(n>sizeof(packetIn)-1) break;
                endif
            endif
        wend
        serout1(NAK);
        return -1;
    end func
    
    func serialScanning()
       var i, j, nozzleId;
    
        for(i:=0; i<NOZZLE_QTY; i++)
            for(j:=0; j<MAX_TRIED; j++)
                nozzleId := reqRealtimeStatus(i);
                if(nozzleId>=0 && nozzleId<nozzleQty)
                    // process data (show data on screen)
                    break;
                endif
            next
         next
    endfunc
    
    func main()
        // com init
        COM1_RX_pin(PA4);
        COM1_TX_pin(PA5);
        com1_Init(packetIn, SERIN_BUFFER*2, 0);
        com_SetBaud(COM1, BAUDRATE);
      
        // enable touch
        touch_Set(TOUCH_ENABLE);
    
        while(1)
            touchScanning();
            serialScanning();
        wend
    endfunc
    Last edited by rwin; 5th July 2015, 01:40 PM.

  • #2
    I can't spot anything obviously wrong with what you have posted and there isn't enough code there to try and run it.

    I have concerns about the size of nozzleQty and MAX_TRIED. You are going to loop 10 * nozzleQty * MAX_TRIED through the inner tmOut code so I wonder how long it will actually spend in there (set a timer at the start and print its value at the end to see).

    Why not structure your code around com_Count() and not even bother 'looking' if there is nothing there?

    Also serialScanning() seems to use global variables i + j you aren't using them somewhere else are you?
    Mark

    Comment


    • #3
      I add the code. NozzleQty := 8; and MAX_TRIED := 5; and var i, j.

      I tried adding break while com_Count == 0, problem isn't solved. RX_TMOUT also serves to limit the looping(Max 2 looping wait) if there is no data ready in serial buffer.

      Comment


      • #4
        It would be better to use file, zip project and attach that if you would like me to try and run the code.

        Note that the serout will 'pause' the program for the time to write since you don't have a tx buffer, regardless you code seems to be written in a way that requires that data to be send immediately before the 'read'. Perhaps things can be adjusted to make them a bit more autonomous.

        Your keyboard code doesn't seem to process 'keyup', or should I say it appears to process it immediately after the key is touched. This can lead to repeated characters as you need to wait for the release to debounce the keyboard.

        Is this what the "keyboard scanning disturbed" you mention is?
        Mark

        Comment


        • #5
          Thank you mark. The file size to big, I sent to your email.

          You code seems to be written in a way that requires that data to be send immediately before the 'read'? yes, serial polling mode. uLCD-43DT as master and rabbit microcontroller as slave. Master polling the slave to request/get data.

          Your keyboard code doesn't seem to process 'keyup', or should I say it appears to process it immediately after the key is touched. This can lead to repeated characters as you need to wait for the release to debounce the keyboard? yes, sometime repeat characters printed while keyboard pressed. How to resolved this problem?

          Keyboard scanning disturbed?
          A. The keyboard does not work if the communication with rabbit mcu disconnected.
          B. If communication with the rabbit is formed:
          - 1.The numeric keyboard work and the qwerty keyboard isn't working
          - 2.The numeric keyboard appears with a red box with a cross in it, qwerty keyboard is still not working.
          C.If I close the serial routine, then the numeric and qwerty keyboard works, sometimes appear character repeatedly.

          Comment


          • #6
            Problem solved, Thank you.

            Comment

            Working...
            X