local lampOld = nil
local lFrame = 0

return function(cx,cy,r)
    rSnap=r//1

    if lampOld == nil then
        lampOld={}
        for dx=-rSnap,rSnap,2 do
            local x=cx+dx
            lampOld[x] = {}
            for dy=-rSnap,rSnap,2 do
                local y=cy+dy
                --lampOld[x][y] = pix(x,y)
            end
        end
    end


    --[[ mirrored]]
    for dx = 0,rSnap do
        for dy=0,rSnap,2 do
            local x,y=cx+dx,cy+dy
            local nx,ny=cx-dx,cy-dy
            local d=0.7-(((dx)^2+(dy)^2)^.5)/r -- 1.0 -> 0.7 for speed up
            local d=d-.2+math.abs(math.sin(d*20))*.4 -- some rings
            local c1 = 0
            local c2 = 0
            local c3 = 0
            local c4 = 0
            if d>0 then
                d=d^.6  -- Taper
                local xDot=dx/(r*.2)
                local yDot=dy/(r*.2)
                local s=.7+((math.sin(xDot*math.pi*2)+math.sin(yDot*math.pi*2))/2)*.3 -- Led stippling
                local p=pix(x,y)
                local pny=pix(x,ny)
                local pnx=pix(nx,y)
                local pnxy=pix(nx,ny)
                local pBlend1=(1+p)/2
                local pBlend2=(1+pny)/2
                local pBlend3=(1+pnx)/2
                local pBlend4=(1+pnxy)/2
                c1=(s*d*pBlend1)*15 -- Scale colour
                c2=(s*d*pBlend2)*15 -- Scale colour
                c3=(s*d*pBlend3)*15 -- Scale colour
                c4=(s*d*pBlend4)*15 -- Scale colour
                c1=R.clamp(c1,0,15)
                c2=R.clamp(c2,0,15)
                c3=R.clamp(c3,0,15)
                c4=R.clamp(c4,0,15)
                c1=c1-(math.random()>(c1-c1//1) and 1 or 0) -- Blended fuzz
                c2=c2-(math.random()>(c2-c2//1) and 1 or 0) -- Blended fuzz
                c3=c3-(math.random()>(c3-c3//1) and 1 or 0) -- Blended fuzz
                c4=c4-(math.random()>(c4-c4//1) and 1 or 0) -- Blended fuzz
                pix(x,y,c1)
                pix(x,ny,c2)
                pix(nx,y,c3)
                pix(nx,ny,c4)
                end


        end
    end
    --[[ original
    for dx=-rSnap,rSnap do
        for dy=-rSnap,rSnap do
            local x,y=cx+dx,cy+dy
            local p=pix(x,y)
            local d=1-(((dx)^2+(dy)^2)^.5)/r
            local d=d-.2+math.abs(math.sin(d*20))*.4 -- some rings
            if d>0 then
                d=d^.6  -- Taper
                local xDot=dx/(r*.2)
                local yDot=dy/(r*.2)
                local s=.7+((math.sin(xDot*math.pi*2)+math.sin(yDot*math.pi*2))/2)*.3 -- Led stippling
                local pBlend=(1+p)/2
                local c=(s*d*pBlend)*15 -- Scale colour
                c=R.clamp(c,0,15)
                c=c-(math.random()>(c-c//1) and 1 or 0) -- Blended fuzz
                pix(x,y,c)
            end
        end
    end
    --]]

    --[[ update every 4th pixel
    
    for dx=-rSnap+lFrame%2,rSnap,2 do
        for dy=-rSnap+lFrame%4/2,rSnap,2 do
            --local x,y=R.clamp(cx+dx,0,239),R.clamp(cy+dy,0,135)
            local x,y=cx+dx,cy+dy
            local p=lampOld[x][y] or 0
            local d=1-(((dx)^2+(dy)^2)^.5)/r
            local d=d-.2+math.abs(math.sin(d*20))*.4 -- some rings
            if d>0 then
                d=d^.6  -- Taper
                local xDot=dx/(r*.2)
                local yDot=dy/(r*.2)
                local s=.7+((math.sin(xDot*math.pi*2)+math.sin(yDot*math.pi*2))/2)*.3 -- Led stippling
                local pBlend=(1+p)/2
                local c=(s*d*pBlend)*15 -- Scale colour
                c=R.clamp(c,0,15)
                c=c-(math.random()>(c-c//1) and 1 or 0) -- Blended fuzz
                lampOld[x][y] = c
                pix(x,y,c)
            end
        end
    end
    --]]

    lFrame = lFrame+1
end
