Recent Posts

Pages: 1 2 [3] 4 5 ... 10
21
Scripting Languages / CBASIC - OpenWeather
« Last post by John on May 09, 2021, 08:19:10 pm »
This example uses the ScriptBasic cURL extension module to fetch the current weather data. It also uses the ScriptBasic LIKE / MATCH functions to parse the returned JSON response.

https://openweathermap.org/

CBASIC offers the a cross platform solution using ScriptBasic's high level C functions to supplement what C doesn't provide native. One can call standard C libraries, ScriptBasic C extension modules and ScriptBasic native C based language functions. It also supports ScriptBasic's variant based thread safe variable structure.

Code: C
  1. // gcc weather.c -IC:\sbgcc\source libscriba.dll -lpthread -o weather
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS arg;
  14.   DIM pSbData AS argr;
  15.   DIM int AS fnsn;
  16.  
  17.   pProgram = scriba_new(malloc,free);
  18.   scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");
  19.   scriba_SetFileName(pProgram, "weather.sb");
  20.   scriba_LoadSourceProgram(pProgram);
  21.   scriba_Run(pProgram, "");
  22.  
  23.   argr = scriba_NewSbArgs(pProgram,"u");
  24.   arg = scriba_NewSbArgs(pProgram, "s", "Anacortes,US");
  25.   fnsn = scriba_LookupFunctionByName(pProgram, "main::get_weather");
  26.   scriba_CallArgEx(pProgram, fnsn, argr, 1, arg);
  27.   PRINT("%s\n\n", argr[0].v.s);
  28.   DIM char AS mask[] = "*temp\":*,*";
  29.   arg = scriba_NewSbArgs(pProgram, "s s", argr[0].v.s, mask);
  30.   fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_like");
  31.   scriba_CallArgEx(pProgram, fnsn, argr, 2, arg);
  32.   arg = scriba_NewSbArgs(pProgram, "i", 2);
  33.   fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_match");
  34.   scriba_CallArgEx(pProgram, fnsn, argr, 1, arg);
  35.   PRINT("Anacortes Temperature is %s F\n", argr[0].v.s);
  36.  
  37.   scriba_DestroySbArgs(pProgram, arg, 2);
  38.   scriba_DestroySbArgs(pProgram, argr, 1);
  39.   scriba_destroy(pProgram);
  40.  
  41.   RETURN_FUNCTION(0);
  42. END_FUNCTION  
  43.  

weather.sb
Code: Script BASIC
  1. ' OpenWeather - Curl Example
  2.  
  3. IMPORT sbembed.sbi
  4. IMPORT curl.sbi
  5.  
  6. FUNCTION Get_Weather(place)
  7.   ch = curl::init()
  8.   curl::option(ch, "URL", "http://api.openweathermap.org/data/2.5/weather?q=" & place & "&units=imperial&appid=MY_API_KEY")
  9.   curl::option(ch, "CUSTOMREQUEST", "GET")
  10.   response = curl::perform(ch)
  11.   curl::finish(ch)
  12.   Get_Weather = response
  13. END FUNCTION
  14.  

LIKE / MATCH FUNCTIONS
Code: Script BASIC
  1. ' LIKE
  2. FUNCTION sb_like(basestr, mask)
  3.   ok = basestr LIKE mask
  4.   sb_like = ok
  5. END FUNCTION
  6.  
  7. ' MATCH
  8. FUNCTION sb_match(segment)
  9.   sb_match = JOKER(segment)
  10. END FUNCTION
  11.  


PS C:\ScriptBASIC\cbasic> Measure-Command { .\weather.exe | Out-Host }
{"coord":{"lon":-122.6127,"lat":48.5126},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":52.61,"feels_like":50.9,"temp_min":51.8,"temp_max":54,"pressure":1024,"humidity":71},"visibility":10000,"wind":{"speed":1.9,"deg":286,"gust":3.2},"clouds":{"all":1},"dt":1620617857,"sys":{"type":1,"id":3524,"country":"US","sunrise":1620563861,"sunset":1620617767},"timezone":-25200,"id":5785657,"name":"Anacortes","cod":200}

Anacortes Temperature is 52.61 F

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 296
Ticks             : 2962516
TotalDays         : 3.42883796296296E-06
TotalHours        : 8.22921111111111E-05
TotalMinutes      : 0.00493752666666667
TotalSeconds      : 0.2962516
TotalMilliseconds : 296.2516


46,312  bytes - weather.exe

22
Scripting Languages / CBASIC - VB6
« Last post by John on May 09, 2021, 04:44:49 pm »
This is an example I did in Oxygen Basic which uses the ScriptBasic COM/OLE extension module. This is the CBASIC version of it.

Code: C
  1. // gcc vblogin.c -IC:\sbgcc\source libscriba.dll sbwgui.o -lpthread -o vblogin
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS ArgData;
  14.   DIM pSbData AS FunctionResult;
  15.   DIM int AS sb_call = 1;
  16.   DIM int AS sb_get  = 2;
  17.   DIM int AS sb_let  = 4;
  18.   DIM int AS sb_set  = 8;
  19.   DIM char AS src[] = "IMPORT sbembed.sbi\nIMPORT \"callback.sb\"\n";
  20.  
  21.   pProgram = scriba_new(malloc,free);
  22.   scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");
  23.   scriba_SetFileName(pProgram, "none");
  24.   scriba_LoadProgramString(pProgram, src, strlen(src));
  25.   scriba_Run(pProgram, "");
  26.  
  27.   FunctionResult = scriba_NewSbArgs(pProgram,"u");
  28.   DIM int AS fn1 = scriba_LookupFunctionByName(pProgram, "main::sb_create");
  29.   DIM int AS fn2 = scriba_LookupFunctionByName(pProgram, "main::sb_cbn");
  30.   DIM int AS fn3 = scriba_LookupFunctionByName(pProgram, "main::sb_release");
  31.   DIM int AS fn4 = scriba_LookupFunctionByName(pProgram, "main::set_callback");
  32.  
  33.   ArgData = scriba_NewSbArgs(pProgram, "s", "O2VB.Login");
  34.   scriba_CallArgEx(pProgram, fn1, FunctionResult, 1, ArgData);
  35.   DIM unsigned long AS vbobj = FunctionResult[0].v.l;
  36.   ArgData = scriba_NewSbArgs(pProgram, "i", vbobj);
  37.   scriba_CallArgEx(pProgram, fn4, FunctionResult, 1, ArgData);
  38.   ArgData = scriba_NewSbArgs(pProgram, "i s i", vbobj, "ShowOCXForm", sb_call);
  39.   scriba_CallArgEx(pProgram, fn2, FunctionResult, 3, ArgData);
  40.   DIM unsigned long AS ufunc = scriba_LookupVariableByName(pProgram, "main::userid");
  41.   scriba_GetVariable(pProgram, ufunc, AT ArgData);
  42.   PRINT("%s\n", ArgData[0].v.s);
  43.   ArgData = scriba_NewSbArgs(pProgram, "i", vbobj);
  44.   scriba_CallArgEx(pProgram, fn3, FunctionResult, 1, ArgData);
  45.  
  46.   scriba_DestroySbArgs(pProgram, ArgData, 3);
  47.   scriba_DestroySbArgs(pProgram, FunctionResult, 1);
  48.   scriba_destroy(pProgram);
  49.  
  50.   RETURN_FUNCTION(0);
  51. END_FUNCTION  
  52.  


callback.sb
Code: Script BASIC
  1. ' CBASIC SB Callback Functions
  2.  
  3. FUNCTION OK_Button_Click(user)
  4.   userid = user
  5.   OK_Button_Click = TRUE
  6. END FUNCTION
  7.  
  8. FUNCTION Set_Callback(vbobj)
  9.   oCollection = CBN(vbobj, "CallBackHandlers", sb_get)
  10.   CBN(oCollection, "Add", sb_call, ADDRESS(OK_Button_Click()), "Login_Form.OK_Button_Click")
  11. END FUNCTION
  12.  

VB6 Login Form
Code: Visual Basic
  1. Option Explicit
  2.  
  3. Private Declare Function ext_SBCallBack Lib "COM.dll" Alias "SBCallBack" (ByVal EntryPoint As Long, ByVal arg As Long) As Long
  4. Private Declare Function ext_SBCallBackEx Lib "COM.dll" Alias "SBCallBackEx" (ByVal EntryPoint As Long, ByRef v As Variant) As Variant
  5.  
  6. Private m_owner As Login
  7. Public LoginSucceeded As Boolean
  8.  
  9. Function ShowMain_Form(owner As Login) As Long
  10.     On Error Resume Next
  11.     Set m_owner = owner
  12.     Me.Show 1
  13.     Set m_owner = Nothing
  14.     ShowMain_Form = 0
  15.     Unload Me
  16. End Function
  17.  
  18. Private Function TriggerCallBack(nodeID As Long, argValue As Long) As Long
  19.     TriggerCallBack = ext_SBCallBack(nodeID, argValue)
  20. End Function
  21.  
  22. Private Function TriggerCallBackEx(nodeID As Long, v() As Variant)
  23.     TriggerCallBackEx = ext_SBCallBackEx(nodeID, v)
  24. End Function
  25.  
  26. Private Sub Cancel_Button_Click()
  27.     LoginSucceeded = False
  28.     Me.Hide
  29. End Sub
  30.  
  31. Private Sub OK_Button_Click()
  32.     Dim nodeID As Long
  33.     Dim arg(0) As Variant
  34.     Dim rtnVal As Long
  35.     If Password_Text = "OxygenBasic" Then
  36.         LoginSucceeded = True
  37.         nodeID = m_owner.CallBackHandlers("Login_Form.OK_Button_Click")
  38.         arg(0) = Login_Form.User_Text
  39.         rtnVal = TriggerCallBackEx(nodeID, arg)
  40.         Me.Hide
  41.     Else
  42.         MsgBox "Invalid Password, try again!", , "Login"
  43.         Password_Text.SetFocus
  44.         SendKeys "{Home}+{End}"
  45.     End If
  46. End Sub
  47.  

VB6 Login Class
Code: Visual Basic
  1. Public CallBackHandlers As New Collection
  2.  
  3. Public Function ShowOCXForm() As Long
  4.     ShowOCXForm = Login_Form.ShowMain_Form(Me)
  5. End Function
  6.  


C:\ScriptBASIC\cbasic>vblogin
John

C:\ScriptBASIC\cbasic>

23
Scripting Languages / CBASIC - Windows
« Last post by John on May 08, 2021, 05:57:04 pm »
I have used CBASIC extensively building ScriptBasic extension modules. Most of this work was done under Linux. I have decided to expanding CBASIC to work with Windows embedding ScriptBasic as a support library. The following example uses the ScriptBasic REPLACE function from CBASIC. (gcc with libscriba.dll) A Linux example of this can be found in a previous post in this thread. (RPi 4B - Ubuntu 64)

Code: C
  1. // gcc replace.c -IC:\sbgcc\source libscriba.dll -lpthread -o replace
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS ArgData;
  14.   DIM unsigned long AS fnsn;
  15.   DIM SbData AS FunctionResult;
  16.   pProgram = scriba_new(malloc,free);
  17.   scriba_SetFileName(pProgram, "sbfunctions");
  18.   scriba_LoadSourceProgram(pProgram);
  19.   scriba_Run(pProgram, "");
  20.   ArgData = scriba_NewSbArgs(pProgram,"s s s i i", "Hello World", "Hello", "Goodbye", 1, 1);
  21.   fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_replace");
  22.   scriba_CallArgEx(pProgram, fnsn, AT FunctionResult, 5, ArgData);
  23.   PRINT("%s\n", FunctionResult.v.s);
  24.   scriba_destroy(pProgram);
  25.   RETURN_FUNCTION(0);
  26. END_FUNCTION
  27.  

sbfunctions
Code: Script BASIC
  1. ' sbfunctions - ScriptBasic CBASIC function wrappers
  2.  
  3. FUNCTION sb_replace(basestr, searchstr, replacestr, occurances, startpos)
  4.   sb_replace = REPLACE(basestr, searchstr, replacestr, occurances, startpos)
  5. END FUNCTION
  6.  


C:\ScriptBASIC\cbasic>replace
Goodbye World

05/08/2021  05:39 PM            45,402 replace.exe


24
Open Forum / Oxygen Basic
« Last post by John on May 07, 2021, 04:05:54 pm »
I have created a Read Only archive of the O2 site I hosted for Charles. When he finds the time to restore his backup of his site I will remove the o2.allbasic.info sub-domain.

Quote
A Plesk xml.tar backup file cannot be restored in Plesk for Windows Server. A Plesk xml.zip backup file cannot be restored in Plesk for Linux.

I can appreciate what Charles is going through taking care of his mother. I also took care of a family member that needed 24/7 monitoring and every thing done for them.  It nearly broke me.

Oxygen Basic Home Page

Oxygen Basic Read Only Forum

25
Open Forum / Happy Birthday BASIC
« Last post by John on May 02, 2021, 02:07:40 pm »
The BASIC programming language is 57 years old today.

26
NOT BASIC / Re: Sudoku solver with GUI
« Last post by John on February 28, 2021, 02:07:46 am »
Great to see you back posting to the forum!

Hope you're doing well.
27
NOT BASIC / Sudoku solver with GUI
« Last post by jalih on February 28, 2021, 01:19:26 am »
Hi all,

I wrote a Sudoku solver with gui using 8th programming language. It shows how Nuklear based gui works and how easy it is to pass results from the solver thread to main gui thread, no locks are necessary.

Use mouse to select Sudoku grid cell. Selection can be moved with cursor keys and number keys 0 - 9 modify grid cell value.

Attached is a sample Sudoku.

Source + binaries for some platforms

28
Scripting Languages / Re: CBASIC
« Last post by John on February 25, 2021, 11:35:31 pm »
This is an example of the IUP GTK WebBrowser control.

Code: C
  1. // gcc webbrowser.c -liup -liupweb -o webbrowser
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <iup.h>
  7. #include <iupweb.h>
  8. #include "cbasic.h"
  9.  
  10. LOCAL FUNCTION int navigate_cb(Ihandle* self, char* url)
  11. BEGIN_FUNCTION
  12.   PRINT("NAVIGATE_CB: %s\n", url);
  13.   (void)self;
  14.   IF (strstr(url, "download")!=NULL) THEN
  15.     RETURN_FUNCTION(IUP_IGNORE);
  16.   END_IF
  17.   RETURN_FUNCTION(IUP_DEFAULT);
  18. END_FUNCTION
  19.                    
  20. LOCAL FUNCTION int error_cb(Ihandle* self, char* url)
  21. BEGIN_FUNCTION
  22.   PRINT("ERROR_CB: %s\n", url);
  23.   (void)self;
  24.   RETURN_FUNCTION(IUP_DEFAULT);
  25. END_FUNCTION
  26.  
  27. LOCAL FUNCTION int completed_cb(Ihandle* self, char* url)
  28. BEGIN_FUNCTION
  29.   PRINT("COMPLETED_CB: %s\n", url);
  30.   (void)self;
  31.   RETURN_FUNCTION(IUP_DEFAULT);
  32. END_FUNCTION
  33.  
  34. LOCAL FUNCTION int newwindow_cb(Ihandle* self, char* url)
  35. BEGIN_FUNCTION
  36.   PRINT("NEWWINDOW_CB: %s\n", url);
  37.   IupSetAttribute(self, "VALUE", url);
  38.   RETURN_FUNCTION(IUP_DEFAULT);
  39. END_FUNCTION
  40.  
  41. LOCAL FUNCTION int back_cb(Ihandle* self)
  42. BEGIN_FUNCTION
  43.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  44.   IupSetAttribute(web, "BACKFORWARD", "-1");
  45.   RETURN_FUNCTION(IUP_DEFAULT);
  46. END_FUNCTION
  47.  
  48. LOCAL FUNCTION int forward_cb(Ihandle* self)
  49. BEGIN_FUNCTION
  50.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  51.   IupSetAttribute(web, "BACKFORWARD", "1");
  52.   RETURN_FUNCTION(IUP_DEFAULT);
  53. END_FUNCTION
  54.  
  55. LOCAL FUNCTION int stop_cb(Ihandle* self)
  56. BEGIN_FUNCTION
  57.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  58.   IupSetAttribute(web, "STOP", NULL);
  59.   RETURN_FUNCTION(IUP_DEFAULT);
  60. END_FUNCTION
  61.  
  62. LOCAL FUNCTION int reload_cb(Ihandle* self)
  63. BEGIN_FUNCTION
  64.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  65.   IupSetAttribute(web, "RELOAD", NULL);
  66.   RETURN_FUNCTION(IUP_DEFAULT);
  67. END_FUNCTION
  68.  
  69. LOCAL FUNCTION int load_cb(Ihandle* self)
  70. BEGIN_FUNCTION
  71.   DIM Ihandle* AS txt  = (Ihandle*)IupGetAttribute(self, "MY_TEXT");
  72.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  73.   IupSetAttribute(web, "VALUE", IupGetAttribute(txt, "VALUE"));
  74.   RETURN_FUNCTION(IUP_DEFAULT);
  75. END_FUNCTION
  76.  
  77. SUB WebBrowserTest(void)
  78. BEGIN_SUB
  79.   DIM Ihandle AS *txt, *dlg, *web;
  80.   DIM Ihandle AS *btLoad, *btReload, *btBack, *btForward, *btStop;
  81.   IupWebBrowserOpen();              
  82.   web = IupWebBrowser();
  83.   dlg = IupDialog(IupVbox(IupHbox(btBack = IupButton("Back", NULL),
  84.         btForward = IupButton("Forward", NULL),
  85.         txt = IupText(""),
  86.         btLoad = IupButton("Load", NULL),
  87.         btReload = IupButton("Reload", NULL),
  88.         btStop = IupButton("Stop", NULL),
  89.         NULL),
  90.         web, NULL));
  91.   IupSetAttribute(dlg, "TITLE", "IupWebBrowser");
  92.   IupSetAttribute(dlg, "MY_TEXT", (char*)txt);
  93.   IupSetAttribute(dlg, "MY_WEB", (char*)web);
  94.   IupSetAttribute(dlg, "RASTERSIZE", "800x600");
  95.   IupSetAttribute(dlg, "MARGIN", "10x10");
  96.   IupSetAttribute(dlg, "GAP", "10");
  97.   IupSetAttribute(txt, "VALUE", "https://allbasic.info/forum");
  98.   IupSetAttribute(web, "VALUE", IupGetAttribute(txt, "VALUE"));
  99.   IupSetAttributeHandle(dlg, "DEFAULTENTER", btLoad);
  100.   IupSetAttribute(txt, "EXPAND", "HORIZONTAL");
  101.   IupSetCallback(btLoad, "ACTION", (Icallback)load_cb);
  102.   IupSetCallback(btReload, "ACTION", (Icallback)reload_cb);
  103.   IupSetCallback(btBack, "ACTION", (Icallback)back_cb);
  104.   IupSetCallback(btForward, "ACTION", (Icallback)forward_cb);
  105.   IupSetCallback(btStop, "ACTION", (Icallback)stop_cb);
  106.   IupSetCallback(web, "NEWWINDOW_CB", (Icallback)newwindow_cb);
  107.   IupSetCallback(web, "NAVIGATE_CB", (Icallback)navigate_cb);
  108.   IupSetCallback(web, "ERROR_CB", (Icallback)error_cb);
  109.   IupSetCallback(web, "COMPLETED_CB", (Icallback)completed_cb);
  110.   IupShow(dlg);
  111. END_SUB
  112.  
  113. MAIN
  114. BEGIN_FUNCTION
  115.   IupOpen(0, 0);
  116.   WebBrowserTest();
  117.   IupMainLoop();
  118.   IupClose();
  119.   RETURN_FUNCTION(EXIT_SUCCESS);
  120. END_FUNCTION
  121.  


jrs@RPi-Dev:~/sb/cbasic$ gcc webbrowser.c -liup -liupweb -o webbrowser
jrs@RPi-Dev:~/sb/cbasic$ ./webbrowser
NAVIGATE_CB: https://allbasic.info/forum
NAVIGATE_CB: https://allbasic.info/forum/
COMPLETED_CB: https://allbasic.info/forum/
HISTORY ITEMS
Current  00: (null)
jrs@RPi-Dev:~/sb/cbasic$

29
Scripting Languages / Re: CBASIC
« Last post by John on February 21, 2021, 06:57:00 pm »
This is an example of using CBASIC with the Raspberry Pi Pico micro controller.

Code: C
  1. // RPi Pico onboard LED blink
  2.  
  3. #include "pico/stdlib.h"
  4. #include "pico/binary_info.h"
  5. #include "cbasic.h"
  6.  
  7. CONST uint AS LED_PIN = 25;
  8.  
  9. MAIN
  10. BEGIN_FUNCTION
  11.   bi_decl(bi_program_description("First Blink"));
  12.   bi_decl(bi_1pin_with_name(LED_PIN, "On-board LED"));
  13.   gpio_init(LED_PIN);
  14.   gpio_set_dir(LED_PIN, GPIO_OUT);
  15.   DEF_WHILE(1)
  16.   BEGIN_WHILE
  17.     gpio_put(LED_PIN, 0);
  18.     sleep_ms(250);
  19.     gpio_put(LED_PIN, 1);
  20.     sleep_ms(1000);
  21.   WEND
  22. END_FUNCTION
  23.  


My original Pico kit I bought from Vilros didn't have the headers soldered in place. My attempt to install them trashed the board. 😥 I purchased the following as a replacement.

DIYmall ESP32-PICO-KIT
30
Scripting Languages / Re: CBASIC
« Last post by John on February 21, 2021, 12:39:33 am »
I was able to get my ScriptBasic IDE/Debugger going. I haven't converted it to CBASIC yet but I thought I would share a screen shot.
Pages: 1 2 [3] 4 5 ... 10