/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / New touchscreen filter

Username:     
Password:     
             

Forum

# 1   2010-03-29 14:13:50 New touchscreen filter

ntrf.zns
Member
From: Belgorod, Russia
Registered: 2009-11-01
Posts: 134

New touchscreen filter

I wrote a more accurate median filter for toucscreen. Median filter works by selecting the middle value among the sorted array. It helps to reject voltage spikes and keeps values steady. In conjunction with my touchscreen accuracy fix this gives a pixel perfect accuracy when drawing!

here is the fix for touchscreen.c file:

Code:

u32 MedianFilter(const u16* values)
{
    u16 Sorted[ADC_NB_SAMPLES]; //We only need so sort half of array
    u16 v;
    u32 i, j;

    Sorted[0] = values[0];
    
    for(i = 1; i < ADC_NB_SAMPLES; i++)
    {
        v = values[i*ADC_NB_CHANNELS];
        j = i;
        for(; j > 0; j--){
            if(v > Sorted[j-1] ) break;
            Sorted[j] = Sorted[j-1];
        }
        Sorted[j] = v;
    }
    
    return Sorted[(ADC_NB_SAMPLES+1) >> 1];
}

void TOUCHSCR_Handler()
    {
    static s32 divider_coord = 0;
    s32 i;
    s32 vX = 0;
    s32 vY = 0;
    s32 vT = 0;
    s32 vZ = 0;
    s32 vRef = 0;
    s32 X, Y, T, q;

    // Don't execute, if it's not time
    if ( ( divider_coord++ % Max_cnt_ts) != 0 ) return;

    vRef = MedianFilter(ADC_ConvertedValue + 2);
    vX   = MedianFilter(ADC_ConvertedValue + OFS_CHANNEL_TS + 0);
    vT   = MedianFilter(ADC_ConvertedValue + OFS_CHANNEL_TS + 1);
    vY   = MedianFilter(ADC_ConvertedValue + OFS_CHANNEL_TS + 2);
    vZ   = MedianFilter(ADC_ConvertedValue + OFS_CHANNEL_TS + 3);
    
    // Conversion voltage => points
    q =    (4096-vT);
    X =    (4000*(vX-vZ)) /q;
    Y =    (4000*(vT-vY)) /q;
    T =    (4000*(vY-vX)) /q;

    // See if touch press
    TOUCHSCR_Info.TouchPress = ( vT < TS_Sensibility );
...

The insertion sort algorithm is quite slow but safe for DMA (algorithm is on-line and in-place). Anyone can offer a better algorithm for this process?

P.S.: The same filter could be used for VBAT and temp-sensor.

Last edited by ntrf.zns (2010-03-29 14:15:07)

Offline

 

# 2   2010-05-20 00:11:25 New touchscreen filter

ShadowPhoenix
Member
Registered: 2009-02-11
Posts: 57

Re: New touchscreen filter

Cool, hope it gets to 3.9

Offline

 

# 3   2010-05-20 06:24:30 New touchscreen filter

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: New touchscreen filter

It has been implemented yet into the new version 3.81

Offline

 

# 4   2010-05-20 07:11:31 New touchscreen filter

ShadowPhoenix
Member
Registered: 2009-02-11
Posts: 57

Re: New touchscreen filter

yrt :

It has been implemented yet into the new version 3.81

Fantastic! Thanks! I remember being very disgusted with touchscreen accuracy a year ago when I was playing with my primer2.

Oh, by the way, is any sort of malloc or memory allocation work in progress?

Last edited by ShadowPhoenix (2010-05-20 07:23:09)

Offline

 

# 5   2010-05-20 11:52:29 New touchscreen filter

ntrf.zns
Member
From: Belgorod, Russia
Registered: 2009-11-01
Posts: 134

Re: New touchscreen filter

Memory alloocation is very application sensitive issue. You need to specify your requirements for memory allocation. It will be easier to implement if you know the limits:
- minimum/typical/maximum allocation size
- memory loss per allocation
- availability of memsize operation
- availability of realloc (usefull if you're working with dynamic string buffers, but slow and causes fragmentation)

Different solutions can be applied here:
- Arena allocator - usefull for small frequent allocations without realoc
- Multi-Pool allocator - usefull when you need a fast realloc, but high memory waste.
- Linear allocator - slow allocation procedure and requires manual garbage collector to merge regions, but lowest memory loss.
- Split allocation - good for allocation of (2^n-1) bytes, otherwise causes memory loss.

Offline

 

# 6   2010-05-20 11:52:37 New touchscreen filter

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: New touchscreen filter

We are thinking about multitasking and RTOS functionalities to add to CircleOS, but not for the next version, which will be dedicated to new primers support.

Offline

 

# 7   2010-05-20 12:05:29 New touchscreen filter

ntrf.zns
Member
From: Belgorod, Russia
Registered: 2009-11-01
Posts: 134

Re: New touchscreen filter

2 yrt: I'm wondering how exactly you're going to implement global memory for multiple applications. Without MMU i't impossible to get a clean solution for separate program memory. It's either application-level multitasking or huge and slow move of the memory chunk.

Plus there is a problem of simultaneous access to resources like USB Endpoints and SD card. It's posible to make them shared.

Offline

 

# 8   2010-05-20 21:53:18 New touchscreen filter

ShadowPhoenix
Member
Registered: 2009-02-11
Posts: 57

Re: New touchscreen filter

Global memory is very easy if we implement system calls. As for MMU, it'd be nice to have, but not necessary if you can guarantee that none of the programs will write to non-malloc-ed area.  Now instead of using global space you just malloc the needed space and the OS will simply give you the needed chunk. Then we can very easily implement signals to pass shared information.

Offline

 

# 9   2010-05-20 22:00:35 New touchscreen filter

ntrf.zns
Member
From: Belgorod, Russia
Registered: 2009-11-01
Posts: 134

Re: New touchscreen filter

And what about all those existing projects for CircleOS? They don't support malloced memory, so you'll have to toss them away as incompatible. Besides in every book about embedded programming you can find somthing like "Use global variables as much as posible".
Due to the number of changes required i don't consider this solution as appliable.

Last edited by ntrf.zns (2010-05-20 22:26:39)

Offline

 

Board footer