﻿B4R=true
Group=Default Group
ModulesStructureVersion=1
Type=StaticCode
Version=3.7
@EndOfDesignText@


Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'Public variables can be accessed from all modules.
	Dim NumOfCHs As Int
	Dim chnl As Int
	Dim num As Int
	Dim p(9) As Pin
End Sub

Sub Initialize(NumOfChannelsUpTo9 As Int)
	If NumOfChannelsUpTo9 > 9 Then
		Log("Channels selected > 9. Setting to 9.")
		NumOfCHs = 9
	Else
		NumOfCHs = NumOfChannelsUpTo9
	End If
		
	
	p(0).Initialize(p(0).A0, p(0).MODE_INPUT_PULLUP)
	p(1).Initialize(p(1).A1, p(1).MODE_INPUT_PULLUP)
	p(2).Initialize(p(2).A2, p(2).MODE_INPUT_PULLUP)
	p(3).Initialize(p(3).A3, p(3).MODE_INPUT_PULLUP)
	p(4).Initialize(p(4).A4, p(4).MODE_INPUT_PULLUP)
	p(5).Initialize(p(5).A5, p(5).MODE_INPUT_PULLUP)
	p(6).Initialize(p(6).A6, p(6).MODE_INPUT_PULLUP)
	p(7).Initialize(p(7).A7, p(7).MODE_INPUT_PULLUP)
	'p(8).Initialize(p(8).A8, p(8).MODE_INPUT_PULLUP)
	
	
	'RunNative("stpanalogpins", NumOfChannelsUpTo9)
End Sub


Sub LogChannels
	
	'Set the values according to analogReadResolution and limits
	'************************************************************
	Dim UpperLimit As Int = 6
	Dim LowerLimit As Int = 0
	Dim AnalogResolution As Int = 1024
	'************************************************************
	
	For ii = 1 To 9
		If  NumOfCHs > ii-1 Then
			chnl = ii
			num = MapRange(p(ii-1).AnalogRead,0, AnalogResolution, LowerLimit, UpperLimit)
			RunNative("logch", Null)
		Else
			Exit
		End If
	Next
	
	RunNative("logchnl", Null)
End Sub


#if C
#include <stdlib.h> // div, div_t

void logch(B4R::Object* o){
    static const char asciiDigits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    div_t n;
	
	int i = b4r_serialoscilloscope::_num;
		
	if (b4r_serialoscilloscope::_chnl > 1) {
		Serial.write(",");
	}
	
    int print = 0;
    if(i < 0) {
        Serial.write('-');
        i = -i;
    }
    if(i >= 10000) {
        n = div(i, 10000);
        Serial.write(asciiDigits[n.quot]);
        i = n.rem;
        print = 1;
    }
    if(i >= 1000 || print) {
        n = div(i, 1000);
        Serial.write(asciiDigits[n.quot]);
        i = n.rem;
        print = 1;
    }
    if(i >= 100 || print) {
        n = div(i, 100);
        Serial.write(asciiDigits[n.quot]);
        i = n.rem;
        print = 1;
    }
    if(i >= 10 || print) {
        n = div(i, 10);
        Serial.write(asciiDigits[n.quot]);
        i = n.rem;
    }
    Serial.write(asciiDigits[i]);
}


void logchnl(B4R::Object* o){
	Serial.write('\r');
}
#End If
