This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
remiX's profile picture

Printing ccSensor data on monitor

Started by remiX, 04 November 2012 - 01:27 AM
remiX #1
Posted 04 November 2012 - 02:27 AM
Hey everyone, I have a program that gets reading's of MFSU, stores it and prints it on a monitor. But it always print decimal number.

For example:

I have it to count how many MFSU's, MFE's and BatBoxes there are around the sensor and it works great but the text on the monitor is different to the screen:

Pictures
SpoilerText on computer
Spoiler

Text on monitor
Spoiler

How do I change it so it doesn't do that :S
KaoS #2
Posted 04 November 2012 - 03:49 AM
just string.sub(val,-2) to cut out the last 2 digits
remiX #3
Posted 04 November 2012 - 07:08 AM
In my code I converted the strings into numbers, would string.sub still work with numbers? Will test it later :D/>/>
KaoS #4
Posted 04 November 2012 - 09:55 AM
it should auto convert them back onto strings and shorten them
remiX #5
Posted 08 November 2012 - 06:56 AM
Sorry for late reply :D/>/> Well that works thanks :P/>/>

Now another quest, the total value for EU is also failing:

Pictures
SpoilerComputer
Spoiler

Monitor
Spoiler

How do I make the numbers show properly?
KaoS #6
Posted 08 November 2012 - 07:32 AM
please post your code
remiX #7
Posted 08 November 2012 - 07:34 AM
Spoiler
os.unloadAPI("sensors")
os.unloadAPI("sensorsUI")
os.loadAPI("/rom/apis/sensors")
os.loadAPI("/rom/apis/sensorsUI")

local writeAt=sensorsUI.writeAt
local sizeX, sizeY = term.getSize()
mon = peripheral.wrap("top")
mX, mY = mon.getSize()
text = {}
file = fs.open("sensorBorder.txt", "r")
repeat
    line = file.readLine()
    table.insert(text, line)
until line == nil
file.close()
mon.clear()
for i = 1, #text do
    mon.setCursorPos(1, i)
    mon.write(text[i])
end

function monW(x, y, str)
    mon.setCursorPos(x, y)
    mon.write(str)
end

function cp(c, x, y) if c==1 then term.clear() end term.setCursorPos(x,y) end

function select(x,y,title,tData)
    local r=nil;
    writeAt(x,y,title..": "..#tData);
    
    for i,v in pairs(tData) do
        writeAt(x+2,y+1+i,v);
    end
    
    local done=false;
    local isel=1;
    local vsel=1;
    writeAt(x+1,y+1+isel,"*")
    repeat
        evt,k = os.pullEvent()
        if evt=="key" then
            writeAt(x+1,y+1+isel," ")
            if k == 200    then    --up
                isel=isel-1
            elseif k== 208 then        --down
                isel = isel+1
            elseif k == 28 or k ==57 then    -- selection made    
                r = tData[isel];
                done=true;
            end
            if isel<1 then isel=#tData
            elseif isel > #tData then isel=1 end
            writeAt(x+1,y+1+isel,"*")
            
        end
    until done
    return r
end
term.clear()
-- Get and Select a sensor
Sensors = sensors.getSensors("right")
dataSensor = select(1, 1, "Available Sensors", Sensors)

-- Get and Select a probe
Probes = sensors.getProbes("right",dataSensor)
dataProbe = select(1, #Sensors+4, "Available Probes",Probes)

-- Get and Select a target
Targets = sensors.getAvailableTargetsforProbe("right",dataSensor,dataProbe)
--dataTarget = select(1, 1, "Available Targets", Targets)

while true do
Targets = sensors.getAvailableTargetsforProbe("right",dataSensor,dataProbe)
    total_max_storage = 0
    total_curr_storage = 0
    total_tier_BatBox = 0
    total_tier_MFE = 0
    total_tier_MFSU = 0
    local maxStorage = {}
    local currStorage = {}
    local tier = {}
    
    for i = 1, #Targets do
        dataReadings = sensors.getSensorReadingAsDict("right",dataSensor,Targets[i],dataProbe)
        table.insert(maxStorage, tonumber(dataReadings.maxStorage))
        table.insert(currStorage, tonumber(dataReadings.energy))
        table.insert(tier, tonumber(dataReadings.tier))
    end
    cp(1, 1, 1)
    for i = 1, #maxStorage do
        total_max_storage = total_max_storage + maxStorage[i]
    end
    for i = 1, #currStorage do
        total_curr_storage = total_curr_storage + currStorage[i]
    end
    for i = 1, #tier do
        if tier[i] == 1 then total_tier_BatBox = total_tier_BatBox + 1
        elseif tier[i] == 2 then total_tier_MFE = total_tier_MFE + 1
        elseif tier[i] == 3 then total_tier_MFSU = total_tier_MFSU + 1 end
    end
    if total_curr_storage > total_max_storage then total_curr_storage = total_max_storage end
    print("Total storage: "..total_max_storage)
    print("Total Current Storage: "..total_curr_storage)
    print("Tiers:nMFSU: "..total_tier_MFSU.."nMFE: "..total_tier_MFE.."nBatBox: "..total_tier_BatBox)
    monW(11, 11, total_max_storage)
    monW(11, 14, total_curr_storage)
    monW(45, 10, string.sub(total_tier_MFSU,-2))
    monW(45, 11, string.sub(total_tier_MFE,-2))
    monW(45, 12, string.sub(total_tier_BatBox,-2))
    sleep(10)
end
KaoS #8
Posted 08 November 2012 - 10:12 AM
I see… I think it is just converting the number into scientific notation to decrease the size of the number. try


monW(11, 11, tostring(total_max_storage))

as the print function converts the parameter to string before writing to the screen, it is displaying correctly but the monitor is not. just give the monitor what print converts it to and they will hopefully be identical. to be honest with you I am not hopeful on that count but here is one that should work fine


local function convert(num)
  local temp=num
  local count=0
  while temp%10==0 do
	count=count+1
	temp=temp/10
  end
  return temp..string.rep('0',count)..' '
end

monW(11, 11, convert(total_max_storage))
monW(11, 14, convert(total_curr_storage))

another thing that is just me being my usual irritating self:

for your monitor writing at CDS function I would recommend returning the cursor to the original position after writing


function monW(x, y, str)
  local cur={mon.getCursorPos()}
  mon.setCursorPos(x, y)
  mon.write(str)
  mon.setCursorPos(unpack(cur))
end
remiX #9
Posted 08 November 2012 - 10:56 AM

monW(11, 11, tostring(total_max_storage))

This works :DD Thanks man!